"""Claude Code agent implementation."""

from __future__ import annotations

from pathlib import Path

from paude.agents.base import (
    AgentConfig,
    build_environment_from_config,
    build_provider_credentials,
    pipefail_install_lines,
)

_CLAUDE_ACTIVITY_FILES = [
    "history.jsonl",
    "debug/*",
]


class ClaudeAgent:
    """Claude Code agent implementation."""

    def __init__(self, provider: str | None = None) -> None:
        creds = build_provider_credentials("claude", provider)
        creds.extra_env_vars["NODE_USE_ENV_PROXY"] = "1"
        self._config = AgentConfig(
            name="claude",
            display_name="Claude Code",
            process_name="claude",
            session_name="claude",
            install_script="curl -fsSL https://claude.ai/install.sh | bash",
            install_dir=".local/bin",
            env_vars=creds.extra_env_vars,
            skip_install_env_var="PAUDE_SKIP_AGENT_INSTALL",
            passthrough_env_vars=creds.passthrough_env_vars,
            secret_env_vars=creds.secret_env_vars,
            passthrough_env_prefixes=creds.passthrough_env_prefixes,
            config_dir_name=".claude",
            config_file_name=".claude.json",
            activity_files=list(_CLAUDE_ACTIVITY_FILES),
            yolo_flag="--dangerously-skip-permissions",
            clear_command="/clear",
            args_env_var="PAUDE_AGENT_ARGS",
            provider=creds.resolved_provider_name,
        )

    @property
    def config(self) -> AgentConfig:
        return self._config

    def dockerfile_install_lines(self, container_home: str) -> list[str]:
        install_lines = pipefail_install_lines(self._config, container_home)
        # Remove the generated .claude.json after install
        install_lines[1] += f" && rm -f {container_home}/.claude.json"
        lines = [
            "",
            "# Install Claude Code (as paude user)",
            "USER paude",
            f"WORKDIR {container_home}",
            *install_lines,
            "",
            "# Ensure claude is in PATH",
            f'ENV PATH="{container_home}/{self._config.install_dir}:$PATH"',
        ]
        return lines

    def apply_sandbox_config(
        self, home: str, workspace: str, args: str, *, yolo: bool = False
    ) -> str:
        script = f"""\
#!/bin/bash
# Auto-generated sandbox config for Claude Code
claude_json="{home}/.claude.json"
settings_json="{home}/.claude/settings.json"

# Suppress trust prompt and onboarding
if [ -f "$claude_json" ]; then
    jq --arg ws "{workspace}" '
        .hasCompletedOnboarding = true |
        .projects = {{($ws): {{hasTrustDialogAccepted: true}}}}
    ' "$claude_json" > "${{claude_json}}.tmp" \\
        && cp -f "${{claude_json}}.tmp" "$claude_json" \\
        && rm -f "${{claude_json}}.tmp"
else
    jq -n --arg ws "{workspace}" '{{
        hasCompletedOnboarding: true,
        projects: {{($ws): {{hasTrustDialogAccepted: true}}}}
    }}' > "$claude_json"
fi
chmod g+rw "$claude_json" 2>/dev/null || true
"""
        if yolo:
            script += f"""
# Suppress bypass permissions warning when yolo mode is enabled
mkdir -p "{home}/.claude" 2>/dev/null || true
skip_patch='{{"skipDangerousModePermissionPrompt": true}}'
if [ -f "$settings_json" ]; then
    jq --argjson patch "$skip_patch" '. * $patch' \
        "$settings_json" > "${{settings_json}}.tmp" \\
        && cp -f "${{settings_json}}.tmp" "$settings_json" \\
        && rm -f "${{settings_json}}.tmp"
else
    echo "$skip_patch" > "$settings_json"
fi
chmod g+rw "$settings_json" 2>/dev/null || true
"""
        return script

    def launch_command(self, args: str) -> str:
        if args:
            return f"claude {args}"
        return "claude"

    def host_config_mounts(self, home: Path) -> list[str]:
        return []

    def build_environment(self) -> dict[str, str]:
        return build_environment_from_config(self._config)
