# ================================================
# PowerShell Script: Compress Aplikasi untuk Upload cPanel
# ================================================
# Jalankan script ini dari folder appsuper
# File hasil: appsuper_upload.zip

param(
    [string]$OutputPath = "C:\xampp\htdocs\appsuper_upload.zip"
)

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "SuperAPP PLKK - Create Upload Package" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Set lokasi
$SourcePath = Get-Location
Write-Host "Source: $SourcePath" -ForegroundColor Yellow

# Folder/file yang akan di-EXCLUDE
$ExcludeItems = @(
    "node_modules",
    ".git",
    ".gitignore",
    ".gitattributes",
    "database_backup.sql",
    "*.log",
    "appsuper_upload.zip",
    "deployment_cpanel_only.zip",
    ".env",
    ".env.backup",
    "storage/logs/*",
    "storage/framework/cache/*",
    "storage/framework/sessions/*",
    "storage/framework/views/*",
    ".vscode",
    ".idea",
    "*.tmp"
)

Write-Host ""
Write-Host "Excluded items:" -ForegroundColor Yellow
$ExcludeItems | ForEach-Object { Write-Host "  - $_" -ForegroundColor DarkGray }

Write-Host ""
Write-Host "Creating ZIP package..." -ForegroundColor Green

try {
    # Hapus file lama jika ada
    if (Test-Path $OutputPath) {
        Remove-Item $OutputPath -Force
        Write-Host "Removed old package" -ForegroundColor DarkGray
    }

    # Cara 1: Compress semua kecuali exclude
    $files = Get-ChildItem -Path $SourcePath -Recurse -File | 
        Where-Object { 
            $file = $_
            $shouldExclude = $false
            foreach ($exclude in $ExcludeItems) {
                if ($file.FullName -like "*$exclude*") {
                    $shouldExclude = $true
                    break
                }
            }
            -not $shouldExclude
        }

    # Compress
    $files | Compress-Archive -DestinationPath $OutputPath -CompressionLevel Optimal

    # Info
    $fileSize = (Get-Item $OutputPath).Length / 1MB
    Write-Host ""
    Write-Host "========================================" -ForegroundColor Green
    Write-Host "SUCCESS!" -ForegroundColor Green
    Write-Host "========================================" -ForegroundColor Green
    Write-Host "Package created: $OutputPath" -ForegroundColor Cyan
    Write-Host "File size: $([math]::Round($fileSize, 2)) MB" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "Next steps:" -ForegroundColor Yellow
    Write-Host "1. Upload file ZIP ke cPanel File Manager" -ForegroundColor White
    Write-Host "2. Extract di folder /home/username/appsuper" -ForegroundColor White
    Write-Host "3. Follow panduan di: deployment_cpanel/INSTALL_CPANEL.md" -ForegroundColor White
    Write-Host ""
}
catch {
    Write-Host ""
    Write-Host "ERROR!" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
    Write-Host ""
    Write-Host "Alternative: Compress manual via Windows Explorer" -ForegroundColor Yellow
    Write-Host "1. Select folder appsuper" -ForegroundColor White
    Write-Host "2. Right click > Send to > Compressed (zipped) folder" -ForegroundColor White
    Write-Host "3. Manual delete: node_modules, .git, .env dari ZIP" -ForegroundColor White
}

Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor DarkGray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
