#Requires -Version 5.1
<#
.SYNOPSIS
    Claude Code - rychla instalace a nastaveni prostredi
.USAGE
    # Windows PS:
    .\Setup-ClaudeCode.ps1 -ApiKey "sk-ant-..." -RepoUrl "https://github.com/org/repo"

    # S volitelnym cilove adresarem:
    .\Setup-ClaudeCode.ps1 -ApiKey "sk-ant-..." -RepoUrl "https://github.com/org/repo" -WorkDir "C:\Projects"
#>

param(
    [Parameter(Mandatory)]
    [string] $ApiKey,

    [Parameter(Mandatory)]
    [string] $RepoUrl,

    [string] $WorkDir = "$HOME\Projects"
)

$ErrorActionPreference = "Stop"

function Write-Step { param([string]$Msg) Write-Host "`n[SETUP] $Msg" -ForegroundColor Cyan }
function Write-OK   { param([string]$Msg) Write-Host "  OK: $Msg" -ForegroundColor Green }
function Write-Fail { param([string]$Msg) Write-Host "  ERR: $Msg" -ForegroundColor Red; exit 1 }

# ------------------------------------------------------------
# 1. NODE.JS
# ------------------------------------------------------------
Write-Step "Checking Node.js..."

$nodeOk = $false
try {
    $nodeVer = node --version 2>$null
    if ($nodeVer -match 'v(\d+)' -and [int]$Matches[1] -ge 18) {
        Write-OK "Node.js $nodeVer already installed"
        $nodeOk = $true
    }
} catch {}

if (-not $nodeOk) {
    Write-Step "Installing Node.js via winget..."
    try {
        winget install OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements --silent
        # Reload PATH
        $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" +
                    [System.Environment]::GetEnvironmentVariable("Path","User")
        Write-OK "Node.js installed"
    } catch {
        Write-Fail "Node.js install failed: $_. Install manually from https://nodejs.org"
    }
}

# ------------------------------------------------------------
# 2. CLAUDE CODE
# ------------------------------------------------------------
Write-Step "Checking Claude Code..."

$ccOk = $false
try {
    $ccVer = claude --version 2>$null
    Write-OK "Claude Code $ccVer already installed"
    $ccOk = $true
} catch {}

if (-not $ccOk) {
    Write-Step "Installing Claude Code..."
    try {
        npm install -g @anthropic-ai/claude-code
        Write-OK "Claude Code installed"
    } catch {
        Write-Fail "Claude Code install failed: $_"
    }
}

# ------------------------------------------------------------
# 3. API KEY
# ------------------------------------------------------------
Write-Step "Setting ANTHROPIC_API_KEY..."

[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $ApiKey, "User")
$env:ANTHROPIC_API_KEY = $ApiKey
Write-OK "API key set (User environment variable)"

# ------------------------------------------------------------
# 4. GIT
# ------------------------------------------------------------
Write-Step "Checking Git..."

try {
    git --version | Out-Null
    Write-OK "Git available"
} catch {
    Write-Step "Installing Git via winget..."
    try {
        winget install Git.Git --accept-package-agreements --accept-source-agreements --silent
        $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" +
                    [System.Environment]::GetEnvironmentVariable("Path","User")
        Write-OK "Git installed"
    } catch {
        Write-Fail "Git install failed: $_. Install manually from https://git-scm.com"
    }
}

# ------------------------------------------------------------
# 5. CLONE REPO
# ------------------------------------------------------------
Write-Step "Cloning repository..."

New-Item -ItemType Directory -Path $WorkDir -Force | Out-Null

$repoName = ($RepoUrl -split '/')[-1] -replace '\.git$', ''
$targetPath = Join-Path $WorkDir $repoName

if (Test-Path $targetPath) {
    Write-OK "Repo already exists at $targetPath — pulling latest..."
    Push-Location $targetPath
    git pull
    Pop-Location
} else {
    git clone $RepoUrl $targetPath
    Write-OK "Cloned to $targetPath"
}

# ------------------------------------------------------------
# 6. LAUNCH
# ------------------------------------------------------------
Write-Host "`n========================================" -ForegroundColor Green
Write-Host " Setup complete!" -ForegroundColor Green
Write-Host " Repo: $targetPath" -ForegroundColor White
Write-Host " Run: cd '$targetPath' && claude" -ForegroundColor White
Write-Host "========================================`n" -ForegroundColor Green

$launch = Read-Host "Launch Claude Code now? (Y/n)"
if ($launch -ne 'n') {
    Set-Location $targetPath
    claude
}
