from PySide6.QtCore import QObject, Signal


class AppSignals(QObject):
    """Central Qt signal bus for all inter-service communication."""

    # App state machine
    state_changed = Signal(str)          # "idle"|"listening"|"transcribing"|"ready"|"error"

    # Transcription pipeline
    transcription_ready = Signal(str)    # Raw text from Whisper
    correction_ready = Signal(str)       # Final text -> Clipboard

    # Model download
    download_progress = Signal(float)    # 0.0 - 1.0
    download_status = Signal(str)        # "Lade Whisper small (45%)..."

    # Errors
    error_occurred = Signal(str)         # Error message for UI

    # Audio devices
    device_list_updated = Signal(list)   # Available microphones

    # Hotkey → AudioService bridge (thread-safe via call_soon_threadsafe)
    recording_should_start = Signal()
    recording_should_stop = Signal()

    # Audio level for VU meter
    level_updated = Signal(float)        # RMS value

    # Mic test state for Settings voice test
    mic_test_state_changed = Signal(str)   # "idle"|"recording"|"playing"

    # File conversion → tray notification (title, message)
    tray_message = Signal(str, str)

    # Live transcription (streaming dictation): committed text delta to type
    live_text_committed = Signal(str)

    # Meeting recorder (system-audio + mic capture → transcript)
    meeting_state = Signal(str)          # "idle"|"recording"|"processing"
    meeting_elapsed = Signal(int)        # seconds since recording started
    meeting_progress = Signal(float)     # transcription progress 0.0 - 1.0
    meeting_done = Signal(str)           # path of the finished transcript (.md)



# Global singleton instance - created once in app.py
import threading

_instance: AppSignals | None = None
_lock = threading.Lock()


def get_signals() -> AppSignals:
    global _instance
    if _instance is None:
        with _lock:
            if _instance is None:
                _instance = AppSignals()
    return _instance
