# Create Forgejo Release with Binary Assets
# Usage: .\create_release.ps1 [-Version 1.0.0]
# If -Version is not provided, it will be read from src/webdrop_bridge/__init__.py
# Uses your Forgejo credentials (same as git)
# First run will prompt for credentials and save them to this session

param(
    [Parameter(Mandatory=$false)]
    [string]$Version,
    
    [Parameter(Mandatory=$false)]
    [string]$ForgejoUser,
    
    [Parameter(Mandatory=$false)]
    [string]$ForgejoPW,
    
    [switch]$ClearCredentials,
    
    [string]$ForgejoUrl = "https://git.him-tools.de",
    [string]$Repo = "HIM-public/webdrop-bridge",
    [string]$ExePath = "build\dist\windows\WebDropBridge.exe",
    [string]$ChecksumPath = "build\dist\windows\WebDropBridge.exe.sha256",
    [string]$MsiPath = "build\dist\windows\WebDropBridge-1.0.0-Setup.msi"
)

$ErrorActionPreference = "Stop"

# Get project root (PSScriptRoot is build/scripts, go up to project root with ..\..)
$projectRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..")

# Resolve file paths relative to project root
$ExePath = Join-Path $projectRoot $ExePath
$ChecksumPath = Join-Path $projectRoot $ChecksumPath
$MsiPath = Join-Path $projectRoot $MsiPath

# Function to read version from .env or .env.example
function Get-VersionFromEnv {
    # Use already resolved project root
    
    # Try .env first (runtime config), then .env.example (template)
    $envFile = Join-Path $projectRoot ".env"
    $envExampleFile = Join-Path $projectRoot ".env.example"
    
    # Check .env first
    if (Test-Path $envFile) {
        $content = Get-Content $envFile -Raw
        if ($content -match 'APP_VERSION=([^\r\n]+)') {
            Write-Host "Version read from .env" -ForegroundColor Gray
            return $matches[1].Trim()
        }
    }
    
    # Fall back to .env.example
    if (Test-Path $envExampleFile) {
        $content = Get-Content $envExampleFile -Raw
        if ($content -match 'APP_VERSION=([^\r\n]+)') {
            Write-Host "Version read from .env.example" -ForegroundColor Gray
            return $matches[1].Trim()
        }
    }
    
    Write-Host "ERROR: Could not find APP_VERSION in .env or .env.example" -ForegroundColor Red
    exit 1
}

# Handle --ClearCredentials flag
if ($ClearCredentials) {
    Remove-Item env:FORGEJO_USER -ErrorAction SilentlyContinue
    Remove-Item env:FORGEJO_PASS -ErrorAction SilentlyContinue
    Write-Host "[OK] Credentials cleared from this session" -ForegroundColor Green
    exit 0
}

# Get credentials from sources (in order of priority)
if (-not $ForgejoUser) {
    $ForgejoUser = $env:FORGEJO_USER
}

if (-not $ForgejoPW) {
    $ForgejoPW = $env:FORGEJO_PASS
}

# If still no credentials, prompt user interactively
if (-not $ForgejoUser -or -not $ForgejoPW) {
    Write-Host "Forgejo credentials not found. Enter your credentials:" -ForegroundColor Yellow
    
    if (-not $ForgejoUser) {
        $ForgejoUser = Read-Host "Username"
    }
    
    if (-not $ForgejoPW) {
        $securePass = Read-Host "Password" -AsSecureString
        $ForgejoPW = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($securePass))
    }
    
    # Save credentials to environment for this session
    $env:FORGEJO_USER = $ForgejoUser
    $env:FORGEJO_PASS = $ForgejoPW
    Write-Host "[OK] Credentials saved to this PowerShell session" -ForegroundColor Green
    Write-Host "Tip: Credentials will persist until you close PowerShell or run: .\create_release.ps1 -ClearCredentials" -ForegroundColor Gray
}

# Verify Version parameter - if not provided, read from .env.example
if (-not $Version) {
    Write-Host "Version not provided, reading from .env.example..." -ForegroundColor Cyan
    $Version = Get-VersionFromEnv
    Write-Host "Using version: $Version" -ForegroundColor Green
}

# Verify files exist
if (-not (Test-Path $ExePath)) {
    Write-Host "ERROR: Executable not found at $ExePath" -ForegroundColor Red
    exit 1
}

if (-not (Test-Path $ChecksumPath)) {
    Write-Host "ERROR: Checksum file not found at $ChecksumPath" -ForegroundColor Red
    exit 1
}

# MSI is optional (only available on Windows after build)
$hasMsi = Test-Path $MsiPath

Write-Host "Creating WebDropBridge $Version release on Forgejo..." -ForegroundColor Cyan

# Get file info
$exeSize = (Get-Item $ExePath).Length / 1MB
$checksum = Get-Content $ChecksumPath -Raw

Write-Host "File: WebDropBridge.exe ($([math]::Round($exeSize, 2)) MB)"
if ($hasMsi) {
    $msiSize = (Get-Item $MsiPath).Length / 1MB
    Write-Host "File: WebDropBridge-1.0.0-Setup.msi ($([math]::Round($msiSize, 2)) MB)"
}
Write-Host "Checksum: $($checksum.Substring(0, 16))..."

# Create basic auth header
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("${ForgejoUser}:${ForgejoPW}"))

$headers = @{
    "Authorization" = "Basic $auth"
    "Content-Type" = "application/json"
}

# Step 1: Create release
Write-Host "`nCreating release v$Version..." -ForegroundColor Yellow
$releaseUrl = "$ForgejoUrl/api/v1/repos/$Repo/releases"

$releaseData = @{
    tag_name = "v$Version"
    name = "WebDropBridge v$Version"
    body = "WebDropBridge v$Version`n`nChecksum: $checksum"
    draft = $false
    prerelease = $false
} | ConvertTo-Json

try {
    $response = Invoke-WebRequest -Uri $releaseUrl `
        -Method POST `
        -Headers $headers `
        -Body $releaseData `
        -TimeoutSec 30 `
        -UseBasicParsing `
        -ErrorAction Stop
    
    $releaseInfo = $response.Content | ConvertFrom-Json
    $releaseId = $releaseInfo.id
    Write-Host "[OK] Release created (ID: $releaseId)" -ForegroundColor Green
}
catch {
    Write-Host "ERROR creating release: $_" -ForegroundColor Red
    exit 1
}

# Step 2: Upload executable as asset using curl
Write-Host "Uploading executable asset..." -ForegroundColor Yellow

try {
    $curlAuth = "$ForgejoUser`:$ForgejoPW"
    $uploadUrl = "$ForgejoUrl/api/v1/repos/$Repo/releases/$releaseId/assets"
    
    $response = curl.exe -s -X POST `
        -u $curlAuth `
        -F "attachment=@$ExePath" `
        $uploadUrl
    
    if ($response -like "*error*" -or $response -like "*404*") {
        Write-Host "ERROR uploading executable: $response" -ForegroundColor Red
        exit 1
    }
    
    Write-Host "[OK] Executable uploaded" -ForegroundColor Green
}
catch {
    Write-Host "ERROR uploading executable: $_" -ForegroundColor Red
    exit 1
}

# Step 3: Upload checksum as asset using curl
Write-Host "Uploading checksum asset..." -ForegroundColor Yellow

try {
    $response = curl.exe -s -X POST `
        -u $curlAuth `
        -F "attachment=@$ChecksumPath" `
        $uploadUrl
    
    if ($response -like "*error*" -or $response -like "*404*") {
        Write-Host "ERROR uploading checksum: $response" -ForegroundColor Red
        exit 1
    }
    
    Write-Host "[OK] Checksum uploaded" -ForegroundColor Green
}
catch {
    Write-Host "ERROR uploading checksum: $_" -ForegroundColor Red
    exit 1
}

# Step 4: Upload MSI as asset (if available)
if ($hasMsi) {
    Write-Host "Uploading MSI installer asset..." -ForegroundColor Yellow
    
    try {
        $response = curl.exe -s -X POST `
            -u $curlAuth `
            -F "attachment=@$MsiPath" `
            $uploadUrl
        
        if ($response -like "*error*" -or $response -like "*404*") {
            Write-Host "ERROR uploading MSI: $response" -ForegroundColor Red
            exit 1
        }
        
        Write-Host "[OK] MSI uploaded" -ForegroundColor Green
    }
    catch {
        Write-Host "ERROR uploading MSI: $_" -ForegroundColor Red
        exit 1
    }
}

Write-Host "`n[OK] Release complete!" -ForegroundColor Green
Write-Host "View at: $ForgejoUrl/$Repo/releases/tag/v$Version" -ForegroundColor Cyan
