@echo off
rem Full daily refresh: run the project pipeline first, then rebuild the full-market catalog.
rem
rem Why this wrapper exists:
rem   deploy\build_full_market_catalog.py derives data\processed\full_market_catalog.csv
rem   from the M1/M2 products, and the dashboard build reads that CSV. Running only
rem   run_daily_update.cmd would leave the catalog at an older cut-off than the dashboard.
rem
rem Usage (same arguments as run_daily_update.cmd):
rem   daily_all.cmd
rem   daily_all.cmd --check
rem
rem Point the scheduled task at THIS file instead of run_daily_update.cmd, so the
rem catalog is rebuilt every day right after the dashboard.
setlocal EnableExtensions
cd /d "%~dp0.."
set "PYTHONIOENCODING=utf-8"
set "PYTHONPATH=%~dp0..\vendor;%~dp0..\src;%PYTHONPATH%"

set "PYEXE="
if exist ".venv\Scripts\python.exe" set "PYEXE=.venv\Scripts\python.exe"
if not defined PYEXE (
  where py >nul 2>&1
  if not errorlevel 1 set "PYEXE=py -3.12"
)
if not defined PYEXE (
  where python >nul 2>&1
  if not errorlevel 1 set "PYEXE=python"
)
if not defined PYEXE (
  echo [ERROR] Python 3.12 not found. 1>&2
  exit /b 9009
)

rem 1) project pipeline: M1 -> M2 -> dashboard
call "%~dp0..\run_daily_update.cmd" %*
set "RC=%errorlevel%"

rem With arguments (e.g. --check) just pass through and stop.
if not "%~1"=="" exit /b %RC%

if not "%RC%"=="0" (
  echo [WARN] daily update exited with %RC%; catalog not rebuilt. 1>&2
  exit /b %RC%
)

rem 2) rebuild the full-market catalog so it matches the dashboard just generated
echo [info] rebuilding full market catalog...
%PYEXE% "%~dp0build_full_market_catalog.py"
set "RC2=%errorlevel%"
if not "%RC2%"=="0" (
  echo [WARN] catalog build failed with %RC2%; dashboard keeps its previous catalog. 1>&2
  exit /b %RC2%
)

echo [info] done: dashboard + full market catalog refreshed.
exit /b 0
