param(
    [string]$ConfigPath = "",
    [string]$Slides = "S09,S10",
    [string]$ReportMonth = "",
    [int]$ReportYear = 0,
    [int]$CompareYear = 0,
    [switch]$Render,
    [string]$OutputDir = "",
    [string]$TemplatePath = "",
    [string]$OutputPath = "",
    [string]$CompareOutputPath = ""
)

$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $PSScriptRoot
$workdir = if ($OutputDir) { [System.IO.Path]::GetFullPath($OutputDir) } else { Join-Path $root "output" }
$opsPath = Join-Path $workdir "render-ops.top-products.live.json"
$workspaceRoot = Split-Path -Parent (Split-Path -Parent $root)
$workspacePython = Join-Path $workspaceRoot "tools\python.exe"

function Resolve-ProjectPath {
    param([string]$PathValue)

    if (-not $PathValue) {
        return $PathValue
    }
    if ([System.IO.Path]::IsPathRooted($PathValue)) {
        return $PathValue
    }
    return [System.IO.Path]::GetFullPath((Join-Path $root $PathValue))
}

function Resolve-PythonCommand {
    if (Test-Path -LiteralPath $workspacePython) {
        return $workspacePython
    }
    $pythonCmd = Get-Command python -ErrorAction SilentlyContinue
    if ($pythonCmd) {
        return $pythonCmd.Source
    }
    throw "Python executable not found. Checked PATH and $workspacePython"
}

if (-not $ConfigPath) {
    $ConfigPath = "config.yaml"
}

if (-not $TemplatePath) {
    $TemplatePath = "Report.pptx"
}

if (-not $OutputPath) {
    $OutputPath = if ($OutputDir) { Join-Path $workdir "generated-top-products.pptx" } else { "output\generated-top-products.pptx" }
}

if (-not $CompareOutputPath) {
    $CompareOutputPath = if ($OutputDir) { Join-Path $workdir "generated-top-products.compare.json" } else { "output\generated-top-products.compare.json" }
}

$ConfigPath = Resolve-ProjectPath $ConfigPath
$TemplatePath = Resolve-ProjectPath $TemplatePath
$OutputPath = Resolve-ProjectPath $OutputPath
$CompareOutputPath = Resolve-ProjectPath $CompareOutputPath

if (-not (Test-Path -LiteralPath $workdir)) {
    New-Item -ItemType Directory -Path $workdir -Force | Out-Null
}

if (-not (Test-Path -LiteralPath $ConfigPath)) {
    throw "ConfigPath not found: $ConfigPath"
}

if (-not (Test-Path -LiteralPath $TemplatePath)) {
    throw "TemplatePath not found: $TemplatePath"
}

$pythonExe = Resolve-PythonCommand
$pythonArgs = @(
    "$root\scripts\sync_top_products_assets.py",
    "--config", "$ConfigPath",
    "--slides", "$Slides"
)
if ($ReportMonth) {
    $pythonArgs += @("--report-month", "$ReportMonth")
}
if ($ReportYear -gt 0) {
    $pythonArgs += @("--report-year", "$ReportYear")
}
if ($CompareYear -gt 0) {
    $pythonArgs += @("--compare-year", "$CompareYear")
}
$pythonArgs += @("--output-dir", "$workdir")

& $pythonExe @pythonArgs
if ($LASTEXITCODE -ne 0) {
    throw "sync_top_products_assets.py failed with exit code $LASTEXITCODE"
}

if ($Render) {
    powershell -ExecutionPolicy Bypass -File "$root\bin\vip-report-render.ps1" -TemplatePath "$TemplatePath" -OutputPath "$OutputPath" -OperationsPath "$opsPath" | Out-Null
    & $pythonExe "$root\scripts\compare_pptx.py" "$TemplatePath" "$OutputPath" --output "$CompareOutputPath"
    if ($LASTEXITCODE -ne 0) {
        throw "compare_pptx.py failed with exit code $LASTEXITCODE"
    }
    Write-Output $OutputPath
    Write-Output $CompareOutputPath
} else {
    Write-Output $opsPath
}
