from __future__ import annotations

import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

from scripts.deploy_adapters import default_codex_home, deploy_antigravity, deploy_codex


def test_default_codex_home_is_external_to_project(tmp_path: Path) -> None:
    home = default_codex_home(env={}, user_home=tmp_path)

    assert home == tmp_path / ".codex"
    assert not home.is_relative_to(REPO_ROOT)


def test_deploy_codex_writes_adapter_to_external_home(tmp_path: Path) -> None:
    target = tmp_path / "codex-home"

    result = deploy_codex(target=target, force=True, repo_root=REPO_ROOT)

    assert result.written
    assert not (target / "config.toml").exists()
    assert (target / "commands" / "dr-run.md").exists()
    assert (target / "agents" / "dr-pm.toml").exists()
    assert (target / "skills" / "search-strategy" / "SKILL.md").exists()
    assert (target / "skills" / "search-gateway" / "SKILL.md").exists()
    assert (target / "skills" / "document-ingest" / "SKILL.md").exists()
    assert (target / "skills" / "deep-research" / "SKILL.md").exists()
    assert result.target == target


def test_deploy_codex_config_is_explicit_opt_in(tmp_path: Path) -> None:
    target = tmp_path / "codex-home"

    result = deploy_codex(target=target, force=True, repo_root=REPO_ROOT, include_config=True)

    assert result.written
    assert (target / "config.toml").exists()


def test_deploy_codex_dry_run_does_not_write(tmp_path: Path) -> None:
    target = tmp_path / "codex-home"

    result = deploy_codex(target=target, force=True, repo_root=REPO_ROOT, dry_run=True)

    assert result.planned
    assert not target.exists()


def test_deploy_antigravity_writes_workspace_rules_and_skills(tmp_path: Path) -> None:
    target = tmp_path / "workspace"

    result = deploy_antigravity(target=target, repo_root=REPO_ROOT)

    assert result.written
    assert (target / ".agents" / "agents.md").exists()
    assert (target / ".agents" / "rules" / "deep-research-antigravity.md").exists()
    assert (target / ".agents" / "workflows" / "deep-research-native.md").exists()
    assert (target / ".agents" / "skills" / "antigravity-surface-adapter" / "SKILL.md").exists()
    assert (target / ".agents" / "skills" / "search-strategy" / "SKILL.md").exists()
    assert result.target == target


def test_deploy_antigravity_skips_existing_files_by_default(tmp_path: Path) -> None:
    target = tmp_path / "workspace"
    existing = target / ".agents" / "rules" / "deep-research-antigravity.md"
    existing.parent.mkdir(parents=True)
    existing.write_text("custom rule\n", encoding="utf-8")

    result = deploy_antigravity(target=target, repo_root=REPO_ROOT)

    assert existing.read_text(encoding="utf-8") == "custom rule\n"
    assert existing in result.skipped
    assert not existing.with_name("deep-research-antigravity.md.bak").exists()


def test_deploy_antigravity_skips_existing_agents_file_by_default(tmp_path: Path) -> None:
    target = tmp_path / "workspace"
    existing = target / ".agents" / "agents.md"
    existing.parent.mkdir(parents=True)
    existing.write_text("custom agents\n", encoding="utf-8")

    result = deploy_antigravity(target=target, repo_root=REPO_ROOT)

    assert existing.read_text(encoding="utf-8") == "custom agents\n"
    assert existing in result.skipped
    assert not existing.with_name("agents.md.bak").exists()


def test_deploy_antigravity_force_backs_up_existing_files(tmp_path: Path) -> None:
    target = tmp_path / "workspace"
    existing = target / ".agents" / "rules" / "deep-research-antigravity.md"
    existing.parent.mkdir(parents=True)
    existing.write_text("custom rule\n", encoding="utf-8")

    result = deploy_antigravity(target=target, repo_root=REPO_ROOT, force=True)

    assert existing.read_text(encoding="utf-8") != "custom rule\n"
    assert existing.with_name("deep-research-antigravity.md.bak").read_text(encoding="utf-8") == "custom rule\n"
    assert existing.with_name("deep-research-antigravity.md.bak") in result.backups
