param(
    [string]$ConfigPath = "C:\Users\niuniu\.codex\vip-report\config.yaml",
    [string]$Slides = "S02,S03",
    [string]$ReportMonth = "",
    [int]$ReportYear = 0,
    [int]$CompareYear = 0,
    [switch]$Render,
    [string]$OutputDir = "",
    [string]$OutputPath = "C:\workspace\cursor\output\vip-report\generated-monthly-sales-live.pptx",
    [string]$CompareOutputPath = "C:\workspace\cursor\output\vip-report\generated-monthly-sales-live.compare.json"
)

$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $PSScriptRoot
$resolvedOutputDir = if ($OutputDir) { [System.IO.Path]::GetFullPath($OutputDir) } else { "C:\workspace\cursor\output\vip-report" }
$opsPath = Join-Path $resolvedOutputDir "render-ops.monthly-sales.live.json"
$templatePath = "C:\Users\niuniu\Desktop\Report.pptx"

if (-not (Test-Path -LiteralPath $resolvedOutputDir)) {
    New-Item -ItemType Directory -Path $resolvedOutputDir -Force | Out-Null
}
if ($OutputDir -and $OutputPath -eq "C:\workspace\cursor\output\vip-report\generated-monthly-sales-live.pptx") {
    $OutputPath = Join-Path $resolvedOutputDir "generated-monthly-sales-live.pptx"
}
if ($OutputDir -and $CompareOutputPath -eq "C:\workspace\cursor\output\vip-report\generated-monthly-sales-live.compare.json") {
    $CompareOutputPath = Join-Path $resolvedOutputDir "generated-monthly-sales-live.compare.json"
}

# Forward optional report-period args; config.yaml defaults are used when omitted.
$pythonArgs = @(
    "$root\scripts\sync_monthly_sales_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", "$resolvedOutputDir")
python @pythonArgs

if ($Render) {
    powershell -ExecutionPolicy Bypass -File "$root\bin\vip-report-render.ps1" -TemplatePath "$templatePath" -OutputPath "$OutputPath" -OperationsPath "$opsPath" | Out-Null
    python "$root\scripts\compare_pptx.py" "$templatePath" "$OutputPath" --output "$CompareOutputPath"
    Write-Output $OutputPath
    Write-Output $CompareOutputPath
} else {
    Write-Output $opsPath
}
