#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2026 Jiri Vyskocil
# SPDX-License-Identifier: Apache-2.0
# terok:container — this file is deployed into task containers, not used on the host.

"""Unified toad launcher for OpenCode-based ACP agents.

Dispatches on argv[0] to determine which ACP wrapper to invoke.
E.g. blablatoad → blablador-acp, kisskitoad → kisski-acp.
"""

import json
import os
import shutil
import sys
from pathlib import Path

TOAD_THEME = "dracula"
TOAD_CONFIG = Path.home() / ".config" / "toad" / "toad.json"

# Map from toad wrapper name to ACP command
_PROG_TO_ACP = {
    "blablatoad": "blablador-acp",
    "kisskitoad": "kisski-acp",
}


def _set_toad_theme(theme: str) -> None:
    """Ensure toad's config has the desired UI theme."""
    config: dict = {}
    if TOAD_CONFIG.exists():
        try:
            config = json.loads(TOAD_CONFIG.read_text())
        except (json.JSONDecodeError, OSError):
            config = {}
    if not isinstance(config, dict):
        config = {}
    if not isinstance(config.get("ui"), dict):
        config["ui"] = {}
    if config["ui"].get("theme") == theme:
        return
    config["ui"]["theme"] = theme
    TOAD_CONFIG.parent.mkdir(parents=True, exist_ok=True)
    TOAD_CONFIG.write_text(json.dumps(config, indent=4) + "\n")


def main() -> None:
    """Entry point for OpenCode toad wrappers."""
    prog = os.path.basename(sys.argv[0])
    acp_agent = _PROG_TO_ACP.get(prog)
    if not acp_agent:
        print(f"error: unknown toad wrapper: {prog}", file=sys.stderr)
        sys.exit(1)
    toad = shutil.which("toad")
    if not toad:
        print("error: toad is not installed", file=sys.stderr)
        sys.exit(1)
    _set_toad_theme(TOAD_THEME)
    os.execvp(toad, [toad, "acp", acp_agent, *sys.argv[1:]])


if __name__ == "__main__":
    main()
