# Siapkan ZIP upload ke cPanel (jalankan di PowerShell dari root project)
$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $PSScriptRoot
$zipPath = Join-Path $root "okelaundry-cpanel.zip"

if (-not (Test-Path (Join-Path $root "public\build\manifest.json"))) {
    Write-Host "Jalankan dulu: npm run build" -ForegroundColor Yellow
    exit 1
}

# Salin dump SQL terbaru ke deploy/ (untuk import phpMyAdmin di hosting)
$backupDir = Join-Path $root "storage\app\backups"
$deploySql = Join-Path $root "deploy\okelaundry_hosting.sql"
if (Test-Path $backupDir) {
    $latestSql = Get-ChildItem $backupDir -Filter "*.sql" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
    if ($latestSql) {
        Copy-Item $latestSql.FullName $deploySql -Force
        Write-Host "SQL untuk hosting: deploy\okelaundry_hosting.sql" -ForegroundColor Cyan
    }
}

$items = @(
    "app", "bootstrap", "config", "database", "deploy", "public",
    "resources", "routes", "storage", "vendor",
    "artisan", "composer.json", "composer.lock",
    "DEPLOY.md", "README.md"
)

$temp = Join-Path $env:TEMP "okelaundry-cpanel-pack"
if (Test-Path $temp) { Remove-Item $temp -Recurse -Force }
New-Item -ItemType Directory -Path $temp | Out-Null

foreach ($item in $items) {
    $src = Join-Path $root $item
    if (Test-Path $src) {
        Copy-Item $src (Join-Path $temp $item) -Recurse -Force
    }
}

# Bersihkan cache/log agar ZIP lebih kecil
@("storage\logs", "storage\framework\cache", "storage\framework\sessions", "storage\framework\views") | ForEach-Object {
    $p = Join-Path $temp $_
    if (Test-Path $p) { Get-ChildItem $p -File -ErrorAction SilentlyContinue | Remove-Item -Force }
}

if (Test-Path $zipPath) { Remove-Item $zipPath -Force }
Compress-Archive -Path (Join-Path $temp "*") -DestinationPath $zipPath -Force
Remove-Item $temp -Recurse -Force

Write-Host "Siap upload ke cPanel:" -ForegroundColor Green
Write-Host $zipPath
