"""Update Skill command."""

import argparse
import os
import shutil
from pathlib import Path

def run(args: argparse.Namespace):
    agent = args.agent
    print(f"==============================")
    print(f" UIdetox Integration: {agent.capitalize()}")
    print(f"==============================\n")
    
    cwd = Path.cwd()
    skill_path = cwd / "SKILL.md"
    project_root = Path(__file__).resolve().parent.parent.parent
    docs_dir = project_root / "docs"
    
    if not skill_path.exists():
        print("Error: SKILL.md not found in the current directory.")
        return
        
    # Copy files if necessary for specific native integrations
    if agent == "claude":
        dest_dir = cwd / ".claude" / "skills" / "uidetox"
        dest_dir.mkdir(parents=True, exist_ok=True)
        shutil.copy(skill_path, dest_dir / "SKILL.md")
        print(f"✓ Copied SKILL.md to {dest_dir}\n")
    elif agent == "codex":
        dest_dir = Path.home() / ".codex" / "skills" / "uidetox"
        dest_dir.mkdir(parents=True, exist_ok=True)
        shutil.copy(skill_path, dest_dir / "SKILL.md")
        print(f"✓ Copied SKILL.md to {dest_dir}\n")
        
    # Print the tailored instructions from the docs/ catalog
    doc_path = docs_dir / f"{agent.upper()}.md"
    if doc_path.exists():
        print(doc_path.read_text(encoding="utf-8"))
    else:
        # Fallback if the package is installed in a way that excludes the docs folder
        print(f"Integration documentation for {agent.capitalize()} could not be found.")
        print("Please check the GitHub repository /docs folder for instructions.")

    print("\nNext step: Run `uidetox scan` to begin.")
