$ErrorActionPreference = "Stop"

param(
  [ValidateSet("historical", "incremental", "targeted")]
  [string]$Mode = "incremental",
  [string]$FromDate = "",
  [string]$ToDate = "",
  [string]$TargetId = "",
  [int]$LimitPerSet = 0,
  [string[]]$EntitySet = @(),
  [string[]]$Keyword = @(),
  [string]$Output = "",
  [switch]$Strict
)

$EnvName = "ndc_1c_mvp"
$EnvPython = Join-Path $env:USERPROFILE "miniconda3\envs\$EnvName\python.exe"
if (-not (Test-Path $EnvPython)) {
  $EnvPython = Join-Path $env:USERPROFILE "Miniconda3\envs\$EnvName\python.exe"
}
if (-not (Test-Path $EnvPython)) {
  throw "Python for env '$EnvName' not found. Expected: $EnvPython"
}

$Args = @("scripts/run_refresh.py", "--mode", $Mode)
if ($FromDate) { $Args += @("--from-date", $FromDate) }
if ($ToDate) { $Args += @("--to-date", $ToDate) }
if ($TargetId) { $Args += @("--target-id", $TargetId) }
if ($LimitPerSet -gt 0) { $Args += @("--limit-per-set", $LimitPerSet.ToString()) }
if ($Output) { $Args += @("--output", $Output) }
if ($Strict) { $Args += "--strict" }
foreach ($Name in $EntitySet) {
  if ($Name) { $Args += @("--entity-set", $Name) }
}
foreach ($Item in $Keyword) {
  if ($Item) { $Args += @("--keyword", $Item) }
}

& $EnvPython @Args
exit $LASTEXITCODE
