# Script de setup para Photum Backend

param(
    [Parameter(Position=0)]
    [string]$Command = "help"
)

function Show-Help {
    Write-Host "=== Photum Backend - Comandos Disponíveis ===" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "  db-up          " -ForegroundColor Green -NoNewline
    Write-Host "Inicia o banco de dados PostgreSQL"
    Write-Host "  db-down        " -ForegroundColor Green -NoNewline
    Write-Host "Para o banco de dados"
    Write-Host "  db-reset       " -ForegroundColor Green -NoNewline
    Write-Host "Reseta o banco de dados (apaga todos os dados)"
    Write-Host "  sqlc-generate  " -ForegroundColor Green -NoNewline
    Write-Host "Gera código Go a partir das queries SQL"
    Write-Host "  swagger        " -ForegroundColor Green -NoNewline
    Write-Host "Gera documentação Swagger"
    Write-Host "  run            " -ForegroundColor Green -NoNewline
    Write-Host "Executa a aplicação"
    Write-Host "  dev            " -ForegroundColor Green -NoNewline
    Write-Host "Inicia ambiente de desenvolvimento completo"
    Write-Host "  test           " -ForegroundColor Green -NoNewline
    Write-Host "Executa os testes"
    Write-Host ""
    Write-Host "Uso: .\setup.ps1 <comando>" -ForegroundColor Yellow
    Write-Host ""
}

function Start-Database {
    Write-Host "Iniciando banco de dados PostgreSQL..." -ForegroundColor Cyan
    docker-compose up -d
    Write-Host "Aguardando banco de dados ficar pronto..." -ForegroundColor Yellow
    Start-Sleep -Seconds 5
    Write-Host "Banco de dados pronto!" -ForegroundColor Green
}

function Stop-Database {
    Write-Host "Parando banco de dados..." -ForegroundColor Cyan
    docker-compose down
    Write-Host "Banco de dados parado!" -ForegroundColor Green
}

function Reset-Database {
    Write-Host "Resetando banco de dados..." -ForegroundColor Cyan
    docker-compose down -v
    docker-compose up -d
    Write-Host "Aguardando banco de dados ficar pronto..." -ForegroundColor Yellow
    Start-Sleep -Seconds 5
    Write-Host "Banco de dados resetado!" -ForegroundColor Green
}

function Generate-SQLC {
    Write-Host "Gerando código Go a partir das queries SQL..." -ForegroundColor Cyan
    sqlc generate
    Write-Host "Código gerado com sucesso!" -ForegroundColor Green
}

function Generate-Swagger {
    Write-Host "Gerando documentação Swagger..." -ForegroundColor Cyan
    swag init -g cmd/api/main.go -o docs
    Write-Host "Documentação Swagger gerada!" -ForegroundColor Green
}

function Start-Application {
    Write-Host "Iniciando aplicação..." -ForegroundColor Cyan
    go run cmd/api/main.go
}

function Start-Dev {
    Write-Host "=== Iniciando ambiente de desenvolvimento ===" -ForegroundColor Cyan
    Start-Database
    Generate-SQLC
    Generate-Swagger
    Start-Application
}

function Run-Tests {
    Write-Host "Executando testes..." -ForegroundColor Cyan
    go test -v ./...
}

switch ($Command.ToLower()) {
    "help" { Show-Help }
    "db-up" { Start-Database }
    "db-down" { Stop-Database }
    "db-reset" { Reset-Database }
    "sqlc-generate" { Generate-SQLC }
    "swagger" { Generate-Swagger }
    "run" { Start-Application }
    "dev" { Start-Dev }
    "test" { Run-Tests }
    default {
        Write-Host "Comando desconhecido: $Command" -ForegroundColor Red
        Write-Host ""
        Show-Help
    }
}
