#!/usr/bin/env python3
"""Pull the repo and rebuild isolated platform workspaces at repository root."""

from __future__ import annotations

import argparse
import shutil
import subprocess
from pathlib import Path


SRC_ROOT = Path(__file__).resolve().parent.parent
REPO_ROOT = SRC_ROOT.parent
ADAPTER_ROOT = SRC_ROOT / "platform_adapters"

COMMON_FILES = [
    "AGENTS.md",
    "GEMINI.md",
    "CLAUDE.md",
    "PLAN.md",
    "docs/antigravity-clean-workspace.md",
    "docs/platform-adapters.md",
    "docs/platform-branch-strategy.md",
    "scripts/dr.py",
    "scripts/deploy_adapters.py",
    "scripts/update_platform_envs.py",
    "scripts/export_antigravity_workspace.py",
    "pyproject.toml",
    "requirements.txt",
    "uv.lock",
]

COMMON_DIRS = [
    "configs",
    "scripts/runtime",
    "scripts/reporting",
    "skills",
]


def run_pull() -> None:
    subprocess.run(["git", "pull", "--ff-only"], cwd=REPO_ROOT, check=True)


def reset_dir(path: Path) -> None:
    if path.exists():
        shutil.rmtree(path)
    path.mkdir(parents=True, exist_ok=True)


def copy_file(rel: str, target_root: Path) -> None:
    src = SRC_ROOT / rel
    if not src.exists():
        raise FileNotFoundError(src)
    dst = target_root / rel
    dst.parent.mkdir(parents=True, exist_ok=True)
    shutil.copy2(src, dst)


def copy_dir(src: Path, dst: Path) -> None:
    if not src.exists():
        raise FileNotFoundError(src)
    if dst.exists():
        shutil.rmtree(dst)
    shutil.copytree(src, dst, ignore=shutil.ignore_patterns("__pycache__", ".pytest_cache"))


def copy_common_workspace(target: Path) -> None:
    for rel in COMMON_FILES:
        copy_file(rel, target)
    for rel in COMMON_DIRS:
        copy_dir(SRC_ROOT / rel, target / rel)
    (target / "projects").mkdir(parents=True, exist_ok=True)
    (target / "projects" / ".gitkeep").touch(exist_ok=True)


def build_antigravity() -> Path:
    target = REPO_ROOT / "antigravity"
    reset_dir(target)
    copy_common_workspace(target)
    copy_dir(ADAPTER_ROOT / "antigravity" / "agent", target / ".agent")
    copy_file("platform_adapters/antigravity/README.md", target)
    return target


def build_codex() -> Path:
    target = REPO_ROOT / "codex"
    reset_dir(target)
    copy_common_workspace(target)
    copy_dir(ADAPTER_ROOT / "codex", target / ".codex")
    copy_dir(ADAPTER_ROOT / "antigravity" / "agent" / "skills", target / ".codex" / "skills")
    copy_file("platform_adapters/codex/README.md", target)
    return target


def build_opencode() -> Path:
    target = REPO_ROOT / "opencode"
    reset_dir(target)
    copy_common_workspace(target)
    copy_dir(ADAPTER_ROOT / "opencode", target / ".opencode")
    copy_file("platform_adapters/opencode/README.md", target)
    return target


def build_claude_code() -> Path:
    target = REPO_ROOT / "claude-code"
    reset_dir(target)
    copy_common_workspace(target)
    copy_dir(ADAPTER_ROOT / "claude-code", target / ".claude")
    copy_file("platform_adapters/claude-code/README.md", target)
    return target


def build_gemini_cli() -> Path:
    target = REPO_ROOT / "gemini-cli"
    reset_dir(target)
    copy_common_workspace(target)
    copy_dir(ADAPTER_ROOT / "gemini-cli", target / ".gemini")
    copy_file("platform_adapters/gemini-cli/README.md", target)
    return target


BUILDERS = {
    "antigravity": build_antigravity,
    "codex": build_codex,
    "opencode": build_opencode,
    "claude-code": build_claude_code,
    "gemini-cli": build_gemini_cli,
}


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="Update isolated Deep Research platform environments")
    parser.add_argument(
        "--platform",
        choices=["all", *BUILDERS.keys()],
        default="all",
        help="which platform env to rebuild",
    )
    parser.add_argument("--skip-pull", action="store_true", help="do not run git pull --ff-only first")
    return parser


def main() -> int:
    args = build_parser().parse_args()
    if not args.skip_pull:
        run_pull()

    names = list(BUILDERS) if args.platform == "all" else [args.platform]
    for name in names:
        target = BUILDERS[name]()
        print(f"rebuilt {name}: {target}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
