#!/usr/bin/env pwsh
<#
.SYNOPSIS
Quick Production Package Creator for Spacetris SDL3

.DESCRIPTION
This script creates a production package using the existing build-msvc directory.
Useful when you already have a working build and just want to create a distribution package.
#>

param(
    [string]$OutputDir = "dist"
)

$ProjectName = "spacetris"
$PackageDir = Join-Path $OutputDir "SpacetrisGame"
$Version = Get-Date -Format "yyyy.MM.dd"

function Write-ColorOutput($ForegroundColor) {
    $fc = $host.UI.RawUI.ForegroundColor
    $host.UI.RawUI.ForegroundColor = $ForegroundColor
    if ($args) { Write-Output $args }
    $host.UI.RawUI.ForegroundColor = $fc
}

function Write-Success { Write-ColorOutput Green $args }
function Write-Info { Write-ColorOutput Cyan $args }
function Write-Warning { Write-ColorOutput Yellow $args }
function Write-Error { Write-ColorOutput Red $args }

Write-Info "======================================"
Write-Info "   Spacetris Quick Package Creator"
Write-Info "======================================"

# Check if build exists
$ExecutablePath = "build-msvc\Debug\spacetris.exe"
if (!(Test-Path $ExecutablePath)) {
    $ExecutablePath = "build-msvc\Release\spacetris.exe"
    if (!(Test-Path $ExecutablePath)) {
        Write-Error "No executable found in build-msvc directory. Please build the project first."
        exit 1
    }
}

Write-Success "Found executable: $ExecutablePath"

# Create package directory
if (Test-Path $PackageDir) {
    Remove-Item $PackageDir -Recurse -Force
}
New-Item -ItemType Directory -Path $PackageDir -Force | Out-Null

# Copy executable
Copy-Item $ExecutablePath $PackageDir
Write-Success "Copied spacetris.exe"

# Copy assets
if (Test-Path "assets") {
    Copy-Item "assets" -Destination (Join-Path $PackageDir "assets") -Recurse -Force
    Write-Success "Copied assets folder"
}

if (Test-Path "FreeSans.ttf") {
    Copy-Item "FreeSans.ttf" $PackageDir
    Write-Success "Copied FreeSans.ttf"
}

# Copy DLLs from vcpkg (SDL_image no longer needed)
$VcpkgBin = "vcpkg_installed\x64-windows\bin"
if (Test-Path $VcpkgBin) {
    $DllFiles = @("SDL3.dll", "SDL3_ttf.dll")
    foreach ($dll in $DllFiles) {
        $dllPath = Join-Path $VcpkgBin $dll
        if (Test-Path $dllPath) {
            Copy-Item $dllPath $PackageDir
            Write-Success "Copied $dll"
        }
    }
}

# Create launcher
$LaunchContent = @"
@echo off
cd /d "%~dp0"
spacetris.exe
if %errorlevel% neq 0 (
    echo.
    echo Game crashed or failed to start!
    echo Check that all DLL files are present.
    echo.
    pause
)
"@
$LaunchContent | Out-File -FilePath (Join-Path $PackageDir "Launch-Spacetris.bat") -Encoding ASCII
Write-Success "Created Launch-Spacetris.bat"

# Create README
$ReadmeContent = @"
Spacetris SDL3 Game
===================

## Quick Start
1. Run Launch-Spacetris.bat or spacetris.exe
2. Use arrow keys to move, Z/X to rotate, Space to drop
3. Press F11 for fullscreen, Esc for menu

## Files
- spacetris.exe: Main game
- Launch-Spacetris.bat: Safe launcher with error handling
- assets/: Game resources (music, images, fonts)
- *.dll: Required libraries

## Controls
Arrow Keys: Move pieces
Z/X: Rotate pieces  
C: Hold piece
Space: Hard drop
P: Pause
F11: Fullscreen
Esc: Menu

Enjoy!
"@
$ReadmeContent | Out-File -FilePath (Join-Path $PackageDir "README.txt") -Encoding UTF8
Write-Success "Created README.txt"

# Calculate size
$PackageSize = (Get-ChildItem $PackageDir -Recurse | Measure-Object -Property Length -Sum).Sum
$PackageSizeMB = [math]::Round($PackageSize / 1MB, 2)

# Create ZIP
$ZipPath = Join-Path $OutputDir "SpacetrisGame-$Version.zip"
try {
    Compress-Archive -Path $PackageDir -DestinationPath $ZipPath -Force
    Write-Success "Created ZIP: $ZipPath"
} catch {
    Write-Warning "Failed to create ZIP: $($_.Exception.Message)"
}

Write-Info ""
Write-Success "Package created successfully!"
Write-Info "Location: $PackageDir"
Write-Info "Size: $PackageSizeMB MB"
Write-Info "ZIP: $ZipPath"
Write-Info ""
Write-Info "Ready for distribution! 🎮"
