# Quality Rails Installation Script (Windows)
param(
    [Parameter(Mandatory=$true)]
    [string]$Template,

    [Parameter(Mandatory=$false)]
    [string]$TargetDir = "."
)

$ErrorActionPreference = "Stop"

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = Split-Path -Parent $ScriptDir
$TemplateDir = Join-Path $RepoRoot "templates\$Template"

if (-not (Test-Path $TemplateDir)) {
    Write-Error "Template '$Template' not found at $TemplateDir"
    Write-Host "Available templates: typescript-node, typescript-nextjs, python, monorepo"
    exit 1
}

Write-Host "Installing Quality Rails: $Template"
Write-Host "Target directory: $TargetDir"
Write-Host ""

# Copy template files
Write-Host "Copying template files..."
if (Test-Path "$TemplateDir\.husky") {
    Copy-Item -Path "$TemplateDir\.husky" -Destination $TargetDir -Recurse -Force
}
Copy-Item -Path "$TemplateDir\.lintstagedrc.js" -Destination $TargetDir -Force -ErrorAction SilentlyContinue
Copy-Item -Path "$TemplateDir\.eslintrc.strict.js" -Destination "$TargetDir\.eslintrc.js" -Force -ErrorAction SilentlyContinue
Copy-Item -Path "$TemplateDir\tsconfig.strict.json" -Destination "$TargetDir\tsconfig.json" -Force -ErrorAction SilentlyContinue
Copy-Item -Path "$TemplateDir\.woodpecker.yml" -Destination $TargetDir -Force -ErrorAction SilentlyContinue

# Copy shared gitleaks config from templates root
$SharedTemplates = Split-Path -Parent $TemplateDir
Copy-Item -Path "$SharedTemplates\.gitleaks.toml" -Destination $TargetDir -Force -ErrorAction SilentlyContinue

Write-Host "✓ Files copied"

if (Test-Path "$TargetDir\package.json") {
    Write-Host ""
    Write-Host "⚠ package.json exists. Please manually merge dependencies from:"
    Write-Host "  $TemplateDir\package.json.snippet"
} else {
    Write-Host "⚠ No package.json found. Create one and add dependencies from:"
    Write-Host "  $TemplateDir\package.json.snippet"
}

Write-Host ""
Write-Host "✓ Quality Rails installed successfully!"
Write-Host ""
Write-Host "Next steps:"
Write-Host "1. Install dependencies: npm install"
Write-Host "2. Initialize husky: npx husky install"
Write-Host "3. Install gitleaks: winget install gitleaks"
Write-Host "4. Run verification: ..\quality-rails\scripts\verify.ps1"
Write-Host "5. (Optional) Scan full history: gitleaks git --redact --verbose"
