import os
from dataclasses import dataclass


@dataclass
class Settings:
    # Hotkeys (Windows format for `keyboard` lib)
    shortcut_capture: str = "ctrl+shift+1"
    shortcut_send: str = "ctrl+shift+2"
    shortcut_action3: str = "ctrl+shift+3"
    shortcut_reset: str = "ctrl+shift+4"
    shortcut_quit: str = "ctrl+shift+5"
    shortcut_toggle_mode: str = "ctrl+shift+6"

    # OpenAI
    model: str = "Google Gemini_2.5"
    prompt: str = (
        "You are a helpful assistant. Analyze the images and answer clearly."
    )
    retries: int = 3
    request_timeout_s: int = 60
    # Configurable API base and key. If left empty, env vars are used.
    # Typical base: https://api.openai.com/v1
    endpoint_base: str = "https://api.muzhen.org/v1"
    api_key: str = "falconfang"

    # Typing and clipboard behavior
    type_interval_s: float = 0.015

    # Data storage
    app_dir: str = os.path.join(
        os.environ.get("LOCALAPPDATA", os.path.expanduser("~/.local/share")),
        "BgVisionAgent",
    )
    captures_dir_name: str = "captures"
    response_file_name: str = "response.txt"
    log_file_name: str = "agent.log"
    suppress_hotkeys: bool = True

    def __post_init__(self):
        # Allow hotkey overrides via environment variables for easy customization
        # Example: set BG_AGENT_SHORTCUT_SEND="ctrl+shift+enter"
        env_map = {
            "BG_AGENT_SHORTCUT_CAPTURE": "shortcut_capture",
            "BG_AGENT_SHORTCUT_SEND": "shortcut_send",
            "BG_AGENT_SHORTCUT_ACTION3": "shortcut_action3",
            "BG_AGENT_SHORTCUT_RESET": "shortcut_reset",
            "BG_AGENT_SHORTCUT_QUIT": "shortcut_quit",
            "BG_AGENT_SHORTCUT_TOGGLE_MODE": "shortcut_toggle_mode",
        }
        for env, attr in env_map.items():
            val = os.environ.get(env)
            if val:
                setattr(self, attr, val)

        # Optional: allow disabling suppression via env
        sup = os.environ.get("BG_AGENT_SUPPRESS_HOTKEYS")
        if sup is not None:
            v = str(sup).strip().lower()
            self.suppress_hotkeys = v in {"1", "true", "yes", "on"}


def ensure_dirs(cfg: Settings) -> None:
    os.makedirs(cfg.app_dir, exist_ok=True)
    os.makedirs(os.path.join(cfg.app_dir, cfg.captures_dir_name), exist_ok=True)


def data_paths(cfg: Settings):
    captures_dir = os.path.join(cfg.app_dir, cfg.captures_dir_name)
    response_path = os.path.join(cfg.app_dir, cfg.response_file_name)
    log_path = os.path.join(cfg.app_dir, cfg.log_file_name)
    return captures_dir, response_path, log_path
