param(
    [Parameter(Mandatory=$false)]
    [string]$Directory = ".",
    [float]$Vmaf = 95.0,
    [int]$Preset = 6,
    [int]$Workers = 1,
    [int]$Samples = 4,
    [string]$Hwaccel = "",
    [string]$Encoder = "svt-av1",
    [switch]$UseHardwareWorker,
    [string]$PlexUrl = "",
    [string]$PlexToken = "",
    [string]$LogDir = "/opt/Optmiser/logs"
)

$ErrorActionPreference = "Stop"

function Write-ColorOutput {
    param([string]$Message, [string]$Color = "White")
    Write-Host $Message -ForegroundColor $Color
}

function Invoke-OptimizeLibrary {
    $scriptPath = Join-Path $PSScriptRoot "optimize_library.py"

    if (-not (Test-Path $scriptPath)) {
        Write-ColorOutput -Message "ERROR: optimize_library.py not found in current directory" -Color "Red"
        exit 1
    }

    $pythonCmd = Get-Command "python"
    if (-not $pythonCmd) {
        Write-ColorOutput -Message "ERROR: Python 3 not found. Please install Python 3." -Color "Red"
        exit 1
    }

    $arguments = @(
        $scriptPath,
        $Directory,
        "--vmaf", $Vmaf.ToString("F1"),
        "--preset", $Preset.ToString(),
        "--workers", $Workers.ToString(),
        "--samples", $Samples.ToString(),
        "--encoder", $Encoder,
        "--log-dir", $LogDir
    )

    if ($Hwaccel) {
        $arguments += "--hwaccel", $Hwaccel
    }

    if ($UseHardwareWorker) {
        $arguments += "--use-hardware-worker"
    }

    if ($PlexUrl) {
        $arguments += "--plex-url", $PlexUrl
    }

    if ($PlexToken) {
        $arguments += "--plex-token", $PlexToken
    }

    Write-ColorOutput -Message "Running optimize_library.py..." -Color "Cyan"
    Write-ColorOutput -Message "  Directory: $Directory" -Color "White"
    Write-ColorOutput -Message "  Target VMAF: $Vmaf" -Color "White"
    Write-ColorOutput -Message "  Preset: $Preset" -Color "White"
    Write-ColorOutput -Message "  Workers: $Workers" -Color "White"
    Write-ColorOutput -Message "  Samples: $Samples" -Color "White"
    Write-ColorOutput -Message "  Encoder: $Encoder" -Color "White"
    if ($Hwaccel) {
        Write-ColorOutput -Message "  HW Accel: $Hwaccel" -Color "White"
    }
    if ($UseHardwareWorker) {
        Write-ColorOutput -Message "  Hardware worker: Enabled (1 HW + $($Workers - 1) CPU)" -Color "White"
    }
    if ($PlexUrl -and $PlexToken) {
        Write-ColorOutput -Message "  Plex refresh: Enabled" -Color "White"
    }
    Write-Host ""

    $process = Start-Process -FilePath $pythonCmd.Path -ArgumentList $arguments -NoNewWindow -PassThru
    $process.WaitForExit()
    $exitCode = $process.ExitCode

    if ($exitCode -eq 0) {
        Write-ColorOutput -Message "SUCCESS: Library optimization completed" -Color "Green"
    } else {
        Write-ColorOutput -Message "ERROR: optimize_library.py exited with code $exitCode" -Color "Red"
    }

    exit $exitCode
}

Write-ColorOutput -Message "========================================" -Color "Cyan"
Write-ColorOutput -Message "VMAF Library Optimiser (Windows)" -Color "Yellow"
Write-ColorOutput -Message "========================================" -Color "Cyan"
Write-Host ""

Invoke-OptimizeLibrary
