# publish-with-sql.ps1
# Publishes Jellyfin and ensures SQL files are included

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Publishing Jellyfin with SQL files" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

Write-Host "Step 1: Publishing Jellyfin..." -ForegroundColor Yellow
dotnet publish Jellyfin.Server\Jellyfin.Server.csproj -c Release -o Jellyfin.Server\bin\Release\net11.0\publish

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ Publish failed" -ForegroundColor Red
    exit 1
}

Write-Host "✓ Publish successful" -ForegroundColor Green
Write-Host ""

Write-Host "Step 2: Copying SQL files..." -ForegroundColor Yellow

$publishDir = "Jellyfin.Server\bin\Release\net11.0\publish"

# Create sql directory structure
New-Item -ItemType Directory -Path "$publishDir\sql" -Force | Out-Null
New-Item -ItemType Directory -Path "$publishDir\sql\schema_init" -Force | Out-Null

# Copy SQL files
try {
    Copy-Item "Jellyfin.Server\sql\*.sql" "$publishDir\sql\" -Force -ErrorAction Stop
    $mainFileCount = (Get-ChildItem "$publishDir\sql\*.sql").Count
    Write-Host "  ✓ Copied $mainFileCount main SQL files" -ForegroundColor Green
    
    Copy-Item "Jellyfin.Server\sql\schema_init\*.sql" "$publishDir\sql\schema_init\" -Force -ErrorAction SilentlyContinue
    $initFileCount = (Get-ChildItem "$publishDir\sql\schema_init\*.sql" -ErrorAction SilentlyContinue).Count
    Write-Host "  ✓ Copied $initFileCount schema_init SQL files" -ForegroundColor Green
} catch {
    Write-Host "❌ Failed to copy SQL files: $_" -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "✓ Publish Complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Output directory: $publishDir" -ForegroundColor Yellow
Write-Host ""
Write-Host "SQL files:" -ForegroundColor Yellow
Get-ChildItem "$publishDir\sql" -Recurse -File | ForEach-Object {
    Write-Host "  - $($_.FullName.Replace($PWD.Path + '\', ''))" -ForegroundColor Gray
}
Write-Host ""
Write-Host "Next: Run jellyfin.exe from the publish directory" -ForegroundColor Cyan
