# این اسکریپت باید روی سیستمی با دسترسی به اینترنت اجرا شود

$COMMIT_HASH = "c2990dca591cba766e3b7ef5d9e8a84796e47ab7"
$PLATFORM = "linux-musl-openssl-3.0.x"

# Create engines directory
New-Item -ItemType Directory -Force -Path "prisma/engines" | Out-Null

Write-Host "Downloading Prisma engines..." -ForegroundColor Green

# Download query engine
Write-Host "Downloading query-engine..."
$queryEngineUrl = "https://binaries.prisma.sh/all_commits/$COMMIT_HASH/$PLATFORM/query-engine.gz"
$queryEnginePath = "prisma/engines/query-engine"

try {
    Invoke-WebRequest -Uri $queryEngineUrl -OutFile "prisma/engines/query-engine.gz"
    
    # Decompress using 7zip or built-in if available
    if (Get-Command 7z -ErrorAction SilentlyContinue) {
        7z e "prisma/engines/query-engine.gz" -o"prisma/engines" -y
        Remove-Item "prisma/engines/query-engine.gz"
    } else {
        Write-Host "Please install 7-Zip or manually decompress prisma/engines/query-engine.gz" -ForegroundColor Yellow
    }
} catch {
    Write-Host "Error downloading query-engine: $_" -ForegroundColor Red
}

# Download schema engine
Write-Host "Downloading schema-engine..."
$schemaEngineUrl = "https://binaries.prisma.sh/all_commits/$COMMIT_HASH/$PLATFORM/schema-engine.gz"

try {
    Invoke-WebRequest -Uri $schemaEngineUrl -OutFile "prisma/engines/schema-engine.gz"
    
    if (Get-Command 7z -ErrorAction SilentlyContinue) {
        7z e "prisma/engines/schema-engine.gz" -o"prisma/engines" -y
        Remove-Item "prisma/engines/schema-engine.gz"
    } else {
        Write-Host "Please install 7-Zip or manually decompress prisma/engines/schema-engine.gz" -ForegroundColor Yellow
    }
} catch {
    Write-Host "Error downloading schema-engine: $_" -ForegroundColor Red
}

Write-Host "`nEngines downloaded successfully!" -ForegroundColor Green
Write-Host "Now commit these files:" -ForegroundColor Cyan
Write-Host "git add prisma/engines"
Write-Host "git commit -m 'Add Prisma engines for offline deployment'"
Write-Host "git push"
