# setup.ps1 - Claude Code bootstrap for Windows
# Usage:
#   irm https://gist.githubusercontent.com/YOUR_GIST_URL/raw/setup.ps1 | iex
#
# Or with parameters (paste as one line):
#   $env:CC_API_KEY="sk-ant-..."; $env:CC_REPO="https://github.com/org/repo"; irm https://gist.../raw/setup.ps1 | iex

$ErrorActionPreference = "Stop"

function Write-Step { param([string]$Msg) Write-Host "[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 }

Write-Host ""
Write-Host "  Claude Code Bootstrap - X9.cz" -ForegroundColor Cyan
Write-Host "  ==============================" -ForegroundColor Cyan
Write-Host ""

# ------------------------------------------------------------
# API KEY
# ------------------------------------------------------------
$apiKey = $env:CC_API_KEY
if (-not $apiKey) {
    $apiKey = Read-Host "Enter Anthropic API key (sk-ant-...)"
}
if (-not $apiKey -or -not $apiKey.StartsWith("sk-")) {
    Write-Fail "Invalid API key"
}

# ------------------------------------------------------------
# REPO URL
# ------------------------------------------------------------
$repoUrl = $env:CC_REPO
if (-not $repoUrl) {
    $repoUrl = Read-Host "Enter repo URL (https://github.com/org/repo)"
}
if (-not $repoUrl) {
    Write-Fail "No repo URL provided"
}

$workDir = if ($env:CC_WORKDIR) { $env:CC_WORKDIR } else { "$HOME\Projects" }

# ------------------------------------------------------------
# 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"
        $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
        $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: https://nodejs.org"
    }
}

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

$gitOk = $false
try { git --version | Out-Null; $gitOk = $true; Write-OK "Git available" } catch {}

if (-not $gitOk) {
    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: https://git-scm.com"
    }
}

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

$ccOk = $false
try { $ccVer = claude --version 2>$null; Write-OK "Claude Code $ccVer"; $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"
    }
}

# ------------------------------------------------------------
# API KEY - ulozit
# ------------------------------------------------------------
Write-Step "Saving API key..."
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $apiKey, "User")
$env:ANTHROPIC_API_KEY = $apiKey
# Vymazat z env aby nezustal v historii procesu
Remove-Item Env:\CC_API_KEY -ErrorAction SilentlyContinue
Write-OK "API key saved"

# ------------------------------------------------------------
# CLONE / PULL REPO
# ------------------------------------------------------------
Write-Step "Setting up repository..."
New-Item -ItemType Directory -Path $workDir -Force | Out-Null

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

if (Test-Path (Join-Path $targetPath ".git")) {
    Write-Step "Repo exists, pulling latest..."
    Push-Location $targetPath
    git pull
    Pop-Location
} else {
    git clone $repoUrl $targetPath
    Write-OK "Cloned to $targetPath"
}

# ------------------------------------------------------------
# CLEAN PS HISTORY - odstranit radky s API key
# ------------------------------------------------------------
try {
    $histPath = (Get-PSReadlineOption).HistorySavePath
    if ($histPath -and (Test-Path $histPath)) {
        $clean = Get-Content $histPath | Where-Object { $_ -notmatch 'sk-ant-|CC_API_KEY|ANTHROPIC' }
        $clean | Set-Content $histPath
    }
} catch {}

# ------------------------------------------------------------
# LAUNCH
# ------------------------------------------------------------
Write-Host ""
Write-Host "  ==============================" -ForegroundColor Green
Write-Host "  Ready! Repo: $targetPath" -ForegroundColor Green
Write-Host "  ==============================" -ForegroundColor Green
Write-Host ""

Set-Location $targetPath
claude
