"""Shared mutable state for the daemon process."""

from __future__ import annotations

import asyncio
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Literal

if TYPE_CHECKING:
    import aiohttp.web
    from meeting_helper.buffer import SentenceBuffer


class AppState:
    """Thread-safe shared state for the running daemon."""

    def __init__(self) -> None:
        self.asyncio_loop: asyncio.AbstractEventLoop | None = None
        self.sentence_buffer: SentenceBuffer | None = None
        self.ws_clients: set[aiohttp.web.WebSocketResponse] = set()

        # Processing status (drives tray icon + overlay status dot)
        self.status: Literal["idle", "listening", "processing"] = "idle"

        # Last full AI response (sent to late-joining viewers)
        self.last_response: str = ""

        # Conversation history: list of {"role": "user"|"assistant", "content": ...}
        # Cleared between meetings, kept within a meeting across queries.
        self.conversation_history: list = []

        self.started_at: float = 0.0
        self.audio_active: bool = False
        self.shutdown_event: asyncio.Event | None = None

        # Meeting-specific
        self.meeting_info: object | None = None  # MeetingInfo from meeting_detector
        self.session_active: bool = False
        self.overlay_proc: object | None = None  # subprocess.Popen
        self.overlay_visible: bool = False

        # Current system prompt (can be rebuilt mid-session via workspace change)
        self.system_prompt: str = ""


# Module-level singleton
state = AppState()
