param(
   [string]$TargetDir,
   [switch]$Force
)

$ErrorActionPreference = 'Stop'

if ([string]::IsNullOrWhiteSpace($TargetDir)) {
   Write-Host "Uso: .vscode/automatizadores/bootstrap-workspace.ps1 <pasta_destino> [-Force]"
   exit 1
}

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$sourceDir = [System.IO.Path]::GetFullPath((Join-Path $scriptDir '..\..'))
$targetFullPath = [System.IO.Path]::GetFullPath($TargetDir)

function Fail {
   param([string]$Message)

   Write-Error $Message
   exit 1
}

function Copy-Entry {
   param([string]$RelativePath)

   $sourcePath = Join-Path $sourceDir $RelativePath
   $targetPath = Join-Path $targetFullPath $RelativePath

   if (-not (Test-Path -LiteralPath $sourcePath)) {
      Fail "Origem nao encontrada: $RelativePath"
   }

   $parentDir = Split-Path -Parent $targetPath
   New-Item -ItemType Directory -Path $parentDir -Force | Out-Null

   if (Test-Path -LiteralPath $targetPath) {
      if (-not $Force.IsPresent) {
         Fail "Destino ja existe: $RelativePath. Use -Force para sobrescrever."
      }

      Remove-Item -LiteralPath $targetPath -Recurse -Force
   }

   Copy-Item -LiteralPath $sourcePath -Destination $targetPath -Recurse -Force
   Write-Host "Copiado: $RelativePath"
}

function Write-PortableClaudeSettings {
   $targetPath = Join-Path $targetFullPath '.claude\settings.local.json'
   $targetDir = Split-Path -Parent $targetPath

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

   if ((Test-Path -LiteralPath $targetPath) -and (-not $Force.IsPresent)) {
      Fail 'Destino ja existe: .claude/settings.local.json. Use -Force para sobrescrever.'
   }

   @'
{
  "permissions": {
    "allow": [
      "mcp__oracle-davinti__run_query"
    ]
  },
  "enableAllProjectMcpServers": true,
  "enabledMcpjsonServers": [
    "oracle-davinti"
  ]
}
'@ | Set-Content -LiteralPath $targetPath -Encoding UTF8

   Write-Host 'Gerado: .claude/settings.local.json'
}

function Cleanup-VscodeMcpDir {
   $mcpDir = Join-Path $targetFullPath '.vscode\mcp-oracle-davinti'
   $nodeModulesDir = Join-Path $mcpDir 'node_modules'
   $envFile = Join-Path $mcpDir '.env'

   if (Test-Path -LiteralPath $nodeModulesDir) {
      Remove-Item -LiteralPath $nodeModulesDir -Recurse -Force
   }

   if (Test-Path -LiteralPath $envFile) {
      Remove-Item -LiteralPath $envFile -Force
   }
}

function Create-BaseDirectories {
   New-Item -ItemType Directory -Path (Join-Path $targetFullPath 'Andamento') -Force | Out-Null
   New-Item -ItemType Directory -Path (Join-Path $targetFullPath 'Concluidos') -Force | Out-Null
   New-Item -ItemType Directory -Path (Join-Path $targetFullPath 'Vitruvio\Documentação') -Force | Out-Null
   New-Item -ItemType Directory -Path (Join-Path $targetFullPath 'Vitruvio\Documentação\Componentes') -Force | Out-Null

   Write-Host 'Criadas pastas base do workspace'
}

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

if ($targetFullPath -eq $sourceDir) {
   Fail 'O destino deve ser diferente da pasta do template.'
}

Copy-Entry '.claude\agents'
Write-PortableClaudeSettings
Copy-Entry '.vscode'
Cleanup-VscodeMcpDir
Copy-Entry '.github'
Copy-Entry 'CLAUDE.md'
Copy-Entry '.mcp.json'
Create-BaseDirectories

Write-Host "Workspace base criado em: $targetFullPath"
Write-Host 'Proximo passo: copie .vscode/mcp-oracle-davinti/.env.example para .env e ajuste as credenciais locais.'