# build-deploy-zip.ps1
# Usage:
#   powershell -ExecutionPolicy Bypass -File .\build-deploy-zip.ps1
# Optional:
#   powershell -ExecutionPolicy Bypass -File .\build-deploy-zip.ps1 -ProjectRoot "C:\path\to\hillcrest-web" -OutName "staging-deploy.zip"

param(
    [string]$ProjectRoot = (Get-Location).Path,
    [string]$OutName = "staging-deploy.zip"
)

$ErrorActionPreference = "Stop"

Write-Host "Project root: $ProjectRoot" -ForegroundColor Cyan

if (!(Test-Path $ProjectRoot)) {
    throw "Project root not found: $ProjectRoot"
}

Set-Location $ProjectRoot

# Basic required files/folders
$mustExist = @(
    "artisan",
    "composer.json",
    "package.json",
    "app",
    "bootstrap",
    "config",
    "database",
    "public",
    "resources",
    "routes"
)

foreach ($p in $mustExist) {
    if (!(Test-Path $p)) {
        throw "Missing required path: $p"
    }
}

# 1) Build PHP deps locally (for server without Composer)
Write-Host "Running composer install --no-dev --optimize-autoloader..." -ForegroundColor Yellow
composer install --no-dev --optimize-autoloader

# 2) Build frontend locally (for server without Node)
Write-Host "Running npm ci..." -ForegroundColor Yellow
npm ci

Write-Host "Running npm run build..." -ForegroundColor Yellow
npm run build

# 3) Clear caches before packaging
Write-Host "Clearing Laravel caches..." -ForegroundColor Yellow
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan cache:clear

# 4) Verify required built artifacts
if (!(Test-Path "vendor")) {
    throw "vendor folder not found after composer install."
}
if (!(Test-Path "public/build")) {
    throw "public/build folder not found after npm run build."
}

# 5) Create temp staging folder
$deployRoot = Join-Path $ProjectRoot ".deploy_tmp"
if (Test-Path $deployRoot) {
    Remove-Item $deployRoot -Recurse -Force
}
New-Item -ItemType Directory -Path $deployRoot | Out-Null

# Include list
$includePaths = @(
    "app",
    "bootstrap",
    "config",
    "database",
    "public",
    "resources",
    "routes",
    "storage",
    "vendor",
    "artisan",
    "composer.json",
    "composer.lock",
    ".env.example"
)

Write-Host "Copying deploy files..." -ForegroundColor Yellow
foreach ($item in $includePaths) {
    if (Test-Path $item) {
        Copy-Item $item -Destination $deployRoot -Recurse -Force
    }
}

# 6) Remove junk from copied deploy folder
Write-Host "Removing non-deploy artifacts..." -ForegroundColor Yellow
$removePatterns = @(
    ".git",
    ".github",
    "node_modules",
    "tests",
    "storage\logs\*",
    "storage\framework\views\*",
    "storage\framework\cache\data\*",
    "storage\framework\sessions\*",
    "storage\framework\testing\*",
    "public\hot"
)

foreach ($pattern in $removePatterns) {
    $full = Join-Path $deployRoot $pattern
    Get-ChildItem -Path $full -Force -ErrorAction SilentlyContinue | ForEach-Object {
        Remove-Item $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
    }
}

# Ensure storage/logs folder exists for Laravel
$storageLogs = Join-Path $deployRoot "storage\logs"
if (!(Test-Path $storageLogs)) {
    New-Item -ItemType Directory -Path $storageLogs -Force | Out-Null
}

# 7) Build zip
$outZip = Join-Path $ProjectRoot $OutName
if (Test-Path $outZip) {
    Remove-Item $outZip -Force
}

Write-Host "Creating zip: $outZip" -ForegroundColor Yellow
Compress-Archive -Path (Join-Path $deployRoot "*") -DestinationPath $outZip -Force

# 8) Cleanup temp folder
Remove-Item $deployRoot -Recurse -Force

Write-Host ""
Write-Host "Done. Deploy zip created:" -ForegroundColor Green
Write-Host $outZip -ForegroundColor Green
Write-Host ""
Write-Host "Next on cPanel:" -ForegroundColor Cyan
Write-Host "1) Upload zip to staging app folder and extract"
Write-Host "2) Ensure subdomain docroot points to /public"
Write-Host "3) Configure .env"
Write-Host "4) Run: php artisan key:generate && php artisan migrate --force && php artisan storage:link && php artisan config:cache"
