#Requires -Version 5.1
<#
.SYNOPSIS
    Claude Code - odstraneni citlivych dat a volitelne cele instalace
.USAGE
    # Jen citliva data (API key + repo):
    .\Remove-ClaudeCode.ps1 -RepoPath "C:\Projects\windows-deployment"

    # Vse vcetne Claude Code a Node.js:
    .\Remove-ClaudeCode.ps1 -RepoPath "C:\Projects\windows-deployment" -Full
#>

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

    [switch] $Full
)

$ErrorActionPreference = "Continue"

function Write-Step { param([string]$Msg) Write-Host "`n[REMOVE] $Msg" -ForegroundColor Yellow }
function Write-OK   { param([string]$Msg) Write-Host "  OK: $Msg" -ForegroundColor Green }
function Write-Skip { param([string]$Msg) Write-Host "  SKIP: $Msg" -ForegroundColor DarkGray }

Write-Host "`n========================================" -ForegroundColor Red
Write-Host " Claude Code Cleanup" -ForegroundColor Red
Write-Host "========================================`n" -ForegroundColor Red

# ------------------------------------------------------------
# 1. API KEY
# ------------------------------------------------------------
Write-Step "Removing ANTHROPIC_API_KEY..."

[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $null, "User")
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $null, "Machine")
$env:ANTHROPIC_API_KEY = $null
Write-OK "API key removed from environment variables"

# Claude Code si uklada API key take v ~/.claude
$claudeConfig = Join-Path $HOME ".claude"
if (Test-Path $claudeConfig) {
    Remove-Item $claudeConfig -Recurse -Force
    Write-OK "Removed ~/.claude config directory"
} else {
    Write-Skip "~/.claude not found"
}

# ------------------------------------------------------------
# 2. REPO
# ------------------------------------------------------------
Write-Step "Removing repository at $RepoPath..."

if (Test-Path $RepoPath) {
    # Nejdriv over ze je to skutecne git repo - pojistka
    $gitDir = Join-Path $RepoPath ".git"
    if (Test-Path $gitDir) {
        Remove-Item $RepoPath -Recurse -Force
        Write-OK "Repository removed"
    } else {
        Write-Host "  WARN: $RepoPath does not look like a git repo. Skipping for safety." -ForegroundColor Yellow
    }
} else {
    Write-Skip "Repo path not found: $RepoPath"
}

# ------------------------------------------------------------
# 3. GIT CREDENTIALS (pokud byly ulozeny)
# ------------------------------------------------------------
Write-Step "Clearing git credentials for repo..."

try {
    git credential reject | Out-Null
} catch {}

# Windows Credential Manager - GitHub tokeny
try {
    $creds = cmdkey /list 2>$null | Select-String "github"
    foreach ($cred in $creds) {
        $target = ($cred -split '\s+') | Where-Object { $_ -like "*github*" } | Select-Object -First 1
        if ($target) {
            cmdkey /delete:$target | Out-Null
            Write-OK "Removed credential: $target"
        }
    }
} catch {
    Write-Skip "No GitHub credentials found in Credential Manager"
}

# ------------------------------------------------------------
# 4. VOLITELNE - Claude Code + Node.js
# ------------------------------------------------------------
if ($Full) {
    Write-Step "Uninstalling Claude Code..."
    try {
        npm uninstall -g @anthropic-ai/claude-code
        Write-OK "Claude Code uninstalled"
    } catch {
        Write-Skip "Claude Code not installed via npm or npm not available"
    }

    Write-Step "Uninstalling Node.js..."
    try {
        winget uninstall OpenJS.NodeJS.LTS --silent
        Write-OK "Node.js uninstalled"
    } catch {
        Write-Skip "Node.js not found via winget - remove manually if needed"
    }
}

# ------------------------------------------------------------
# 5. POWERSHELL HISTORY (muze obsahovat API key z parametru)
# ------------------------------------------------------------
Write-Step "Clearing PowerShell history..."

$historyPath = (Get-PSReadlineOption).HistorySavePath
if ($historyPath -and (Test-Path $historyPath)) {
    # Vymaz pouze radky obsahujici ApiKey / sk-ant
    $lines = Get-Content $historyPath | Where-Object { $_ -notmatch 'ApiKey|sk-ant-|ANTHROPIC' }
    $lines | Set-Content $historyPath
    Write-OK "Sensitive lines removed from PS history"
} else {
    Write-Skip "PS history file not found"
}

# ------------------------------------------------------------
# SUMMARY
# ------------------------------------------------------------
Write-Host "`n========================================" -ForegroundColor Green
Write-Host " Cleanup complete!" -ForegroundColor Green
if ($Full) {
    Write-Host " Removed: API key, repo, ~/.claude, Node.js, Claude Code" -ForegroundColor White
} else {
    Write-Host " Removed: API key, repo, ~/.claude config" -ForegroundColor White
    Write-Host " Node.js and Claude Code kept (use -Full to remove)" -ForegroundColor DarkGray
}
Write-Host "========================================`n" -ForegroundColor Green
