"""macOS AppAdapter stub — open -a launch; honest automation pending (DSL Phase 3c)."""

from __future__ import annotations

from pathlib import Path
from typing import Any

from iuve_connect_agents.desktop.platform_adapters.adapter_types import (
    EmptyTrashResult,
    FocusResult,
    InstalledAppEntry,
    LaunchResult,
    TypeResult,
)
from iuve_connect_agents.desktop.platform_adapters import runtime_launch

_AUTOMATION_MESSAGE = (
    "macOS Accessibility automation is not implemented in v0.1; see WIN_MAC_FULL_AGENT_ROADMAP.md"
)


class MacOSAppAdapter:
    """macOS L2 adapter: open -a where trivial; automation stub."""

    os_family = "macos"

    def launch(
        self,
        app_id: str,
        entry: InstalledAppEntry,
        *,
        new_window: bool = True,
    ) -> LaunchResult:
        del app_id, new_window
        ok, cmd, pid, error = runtime_launch.launch_macos_entry(entry)
        if ok:
            return LaunchResult(success=True, pid=pid, launch_command=cmd)
        return LaunchResult(success=False, launch_command=cmd, error=error or "launch_failed")

    def launch_uri(self, uri: str) -> LaunchResult:
        ok, cmd, pid, error = runtime_launch.launch_uri_macos(uri)
        if ok:
            return LaunchResult(success=True, pid=pid, launch_command=cmd)
        return LaunchResult(success=False, launch_command=cmd, error=error or "launch_failed")

    def open_folder(self, folder_path: str) -> LaunchResult:
        path = str(folder_path or "").strip()
        if not path:
            return LaunchResult(success=False, error="missing_path")
        if not Path(path).is_dir():
            return LaunchResult(success=False, error="not_a_directory")
        ok, cmd, pid, error = runtime_launch.launch_uri_macos(path)
        if ok:
            return LaunchResult(success=True, pid=pid, launch_command=cmd)
        return LaunchResult(success=False, launch_command=cmd, error=error or "folder_open_failed")

    def empty_trash(self) -> EmptyTrashResult:
        ok, cmd, error = runtime_launch.empty_trash_macos()
        if ok:
            return EmptyTrashResult(
                success=True,
                execution_result="trash_emptied",
                evidence={
                    "tool": "osascript_finder_empty_trash",
                    "handler": "empty_trash",
                    "os_family": self.os_family,
                    "launch_command": cmd,
                },
            )
        return EmptyTrashResult(
            success=False,
            execution_result="trash_empty_failed",
            error=error or "trash_empty_failed",
            evidence={
                "tool": "osascript_finder_empty_trash",
                "handler": "empty_trash",
                "os_family": self.os_family,
                "launch_command": cmd,
            },
        )

    def focus(
        self,
        app_id: str,
        entry: InstalledAppEntry | None,
        *,
        window_hint: str = "",
    ) -> FocusResult:
        del app_id, entry, window_hint
        return FocusResult(
            success=False,
            detail="platform_automation_pending",
            evidence={
                "os_family": self.os_family,
                "message": _AUTOMATION_MESSAGE,
                "automation": "accessibility_api_not_implemented",
            },
        )

    def type_text(self, app_id: str, text: str, *, submit: bool = False) -> TypeResult:
        del app_id, text, submit
        return TypeResult(
            success=False,
            execution_result="platform_automation_pending",
            error=_AUTOMATION_MESSAGE,
            evidence={
                "os_family": self.os_family,
                "automation": "ax_value_set_not_implemented",
            },
        )

    def is_running(self, app_id: str, entry: InstalledAppEntry | None) -> bool:
        del app_id
        if not entry:
            return False
        launch_spec = entry.get("launch_spec")
        if not isinstance(launch_spec, dict):
            return False
        argv = launch_spec.get("argv")
        argv_parts = [str(part) for part in argv] if isinstance(argv, list) else []
        binary_path = str(entry.get("binary_path") or (argv_parts[0] if argv_parts else "") or "").strip()
        if not binary_path:
            display_name = str(entry.get("display_name") or "").strip()
            if display_name:
                binary_path = display_name
            else:
                return False
        return runtime_launch.process_running_macos(binary_path)
