"""Windows AppAdapter stub — launch-only; 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 = (
    "Windows UI Automation is not implemented in v0.1; see WIN_MAC_FULL_AGENT_ROADMAP.md"
)


class WindowsAppAdapter:
    """Windows L2 adapter: detached launch where trivial; automation stub."""

    os_family = "windows"

    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_windows_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_windows(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.open_folder_windows(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_windows()
        if ok:
            return EmptyTrashResult(
                success=True,
                execution_result="trash_emptied",
                evidence={
                    "tool": "powershell_clear_recycle_bin",
                    "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": "powershell_clear_recycle_bin",
                "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": "ui_automation_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": "sendkeys_uia_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:
            return False
        return runtime_launch.process_running_windows(binary_path)
