# Agentic Engineering Course Installer (PowerShell)
# Installs skills, extensions, and configs for Claude Code, Pi Agent, OpenCode
# Run: powershell -File install.ps1

$ErrorActionPreference = "Stop"
$TAC_ROOT = Split-Path -Parent $MyInvocation.MyCommand.Path
$SKILLS_DIR = Join-Path $TAC_ROOT "skills"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Agentic Engineering Course Installer" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Check prerequisites
$tools = @{
    "claude" = "Claude Code"
    "pi" = "Pi Agent"
    "opencode" = "OpenCode"
}
$found = @{}
foreach ($cmd in $tools.Keys) {
    $path = Get-Command $cmd -ErrorAction SilentlyContinue
    if ($path) {
        $found[$cmd] = $true
        Write-Host "  [OK] $($tools[$cmd]) found" -ForegroundColor Green
    } else {
        $found[$cmd] = $false
        Write-Host "  [--] $($tools[$cmd]) not installed (skipping)" -ForegroundColor Yellow
    }
}
Write-Host ""

# Install skill kits
function Install-KitSkills {
    param($KitPath, $TargetDir)
    $skillDirs = Get-ChildItem (Join-Path $KitPath "skills") -Directory -ErrorAction SilentlyContinue
    if (-not $skillDirs) { return }
    
    foreach ($skill in $skillDirs) {
        $target = Join-Path $TargetDir $skill.Name
        if (-not (Test-Path $target)) {
            New-Item -ItemType Directory -Path $target -Force | Out-Null
        }
        Copy-Item -Path "$($skill.FullName)\*" -Destination $target -Recurse -Force
        Write-Host "    skill: $($skill.Name)" -ForegroundColor Green
    }
}

function Install-KitExtensions {
    param($KitPath, $TargetDir)
    $extDir = Join-Path $KitPath "extensions"
    if (-not (Test-Path $extDir)) { return }
    
    if (-not (Test-Path $TargetDir)) {
        New-Item -ItemType Directory -Path $TargetDir -Force | Out-Null
    }
    Copy-Item -Path "$extDir\*.ts" -Destination $TargetDir -Force
    Write-Host "    extensions copied" -ForegroundColor Green
}

# Claude Code install
if ($found["claude"]) {
    Write-Host "Installing to Claude Code..." -ForegroundColor Yellow
    $ccSkills = Join-Path $env:USERPROFILE ".claude\skills"
    New-Item -ItemType Directory -Path $ccSkills -Force | Out-Null
    
    $kitDirs = Get-ChildItem (Join-Path $SKILLS_DIR "kits") -Directory
    foreach ($kit in $kitDirs) {
        Write-Host "  Kit: $($kit.Name)" -ForegroundColor White
        Install-KitSkills -KitPath $kit.FullName -TargetDir $ccSkills
    }
    Write-Host "  [OK] Skills installed to Claude Code" -ForegroundColor Green
    Write-Host ""
}

# Pi Agent install
if ($found["pi"]) {
    Write-Host "Installing to Pi Agent..." -ForegroundColor Yellow
    $piSkills = Join-Path $env:USERPROFILE ".pi\skills"
    $piExts = Join-Path $env:USERPROFILE ".pi\extensions"
    New-Item -ItemType Directory -Path $piSkills -Force | Out-Null
    New-Item -ItemType Directory -Path $piExts -Force | Out-Null
    
    $kitDirs = Get-ChildItem (Join-Path $SKILLS_DIR "kits") -Directory
    foreach ($kit in $kitDirs) {
        Write-Host "  Kit: $($kit.Name)" -ForegroundColor White
        Install-KitSkills -KitPath $kit.FullName -TargetDir $piSkills
        Install-KitExtensions -KitPath $kit.FullName -TargetDir $piExts
    }
    
    # Copy templates
    $templatesDir = Join-Path $SKILLS_DIR "templates"
    Copy-Item "$templatesDir\*.template" (Join-Path $env:USERPROFILE ".pi") -Force
    Write-Host "  [OK] Skills and extensions installed to Pi Agent" -ForegroundColor Green
    Write-Host ""
}

# Course materials
Write-Host "Installing course reference files..." -ForegroundColor Yellow
$courseDir = Join-Path $TAC_ROOT "course"
$docsDir = Join-Path $env:USERPROFILE ".agentic-engineering-course"
New-Item -ItemType Directory -Path $docsDir -Force | Out-Null
Copy-Item -Path "$courseDir\*.md" -Destination $docsDir -Force
Copy-Item -Path "$courseDir\labs" -Destination $docsDir -Recurse -Force
Write-Host "  [OK] Course materials installed to $docsDir" -ForegroundColor Green
Write-Host ""

Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Installation Complete!" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Course docs:    $docsDir"
Write-Host "Claude skills:  $env:USERPROFILE\.claude\skills\"
Write-Host "Pi skills:      $env:USERPROFILE\.pi\skills\"
Write-Host "Pi extensions:  $env:USERPROFILE\.pi\extensions\"
Write-Host ""
Write-Host "Quick start:" -ForegroundColor Yellow
Write-Host "  cd $docsDir"
Write-Host "  Start with: 00-CURRICULUM.md"
