@echo off
REM ============================================================
REM  Medmecs MIS - one-time setup (PURE PHP version)
REM
REM  What this does:
REM    1. Find mysql.exe (XAMPP/Laragon)
REM    2. Drop and recreate the medmecs_mis database (clean slate)
REM    3. Import sql\schema.sql               - core tables, RBAC, audit
REM    4. Import sql\collection-dashboard.sql - collection/mapping/fee schedule
REM    5. Import sql\fee-schedule-starter.sql - CMS-derived starter fee rates
REM    6. Import sql\seed-additional-roles.sql (if present)
REM
REM  Run by double-clicking. Takes about 5 seconds.
REM ============================================================
setlocal EnableDelayedExpansion
cd /d "%~dp0"
title Medmecs MIS Setup
color 0B

echo.
echo ============================================================
echo   MEDMECS MIS - one-time setup (pure PHP)
echo ============================================================
echo.

REM --- Locate mysql.exe ---
set "MYSQL_EXE="
where mysql.exe >nul 2>&1
if not errorlevel 1 (
    set "MYSQL_EXE=mysql"
    goto mysql_found
)
for %%M in ("C:\xampp\mysql\bin\mysql.exe" "C:\xampp8\mysql\bin\mysql.exe" "D:\xampp\mysql\bin\mysql.exe" "C:\laragon\bin\mysql\mysql-8*\bin\mysql.exe" "C:\laragon\bin\mysql\mysql-5*\bin\mysql.exe") do (
    if exist "%%~M" (
        set "MYSQL_EXE=%%~M"
        goto mysql_found
    )
)

color 0C
echo Could not find mysql.exe.
echo Make sure XAMPP is installed and MySQL is running.
pause
exit /b 1

:mysql_found
echo Found MySQL: !MYSQL_EXE!
echo.

if not exist "sql\schema.sql" (
    color 0C
    echo sql\schema.sql is missing. Cannot continue.
    pause
    exit /b 1
)

REM --- Drop and recreate the database (clean state) ---
echo Dropping and recreating database 'medmecs_mis'...
"!MYSQL_EXE!" -u root -e "DROP DATABASE IF EXISTS medmecs_mis; CREATE DATABASE medmecs_mis DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" 2>nul
if errorlevel 1 (
    color 0C
    echo.
    echo Could not connect to MySQL as root with no password.
    echo If your MySQL root has a password, run this manually in
    echo phpMyAdmin SQL tab:
    echo.
    echo   DROP DATABASE IF EXISTS medmecs_mis;
    echo   CREATE DATABASE medmecs_mis DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    echo.
    echo Then re-run this script.
    pause
    exit /b 1
)
echo   Database 'medmecs_mis' is ready.
echo.

REM --- Import schema.sql ---
echo Importing sql\schema.sql (core: RBAC, audit, reports)...
"!MYSQL_EXE!" -u root medmecs_mis < "sql\schema.sql"
if errorlevel 1 (
    color 0C
    echo Schema import failed. Check the error above.
    pause
    exit /b 1
)
echo   Core schema imported.

echo Importing sql\collection-dashboard.sql (collection/mapping/fee tables)...
if exist "sql\collection-dashboard.sql" (
    "!MYSQL_EXE!" -u root medmecs_mis < "sql\collection-dashboard.sql"
    echo   Collection schema imported.
)

echo Importing sql\fee-schedule-starter.sql (CMS-derived starter rates)...
if exist "sql\fee-schedule-starter.sql" (
    "!MYSQL_EXE!" -u root medmecs_mis < "sql\fee-schedule-starter.sql"
    echo   Starter fee schedule imported (2024 / 2025 / 2026, ~70 CPTs).
)

if exist "sql\seed-additional-roles.sql" (
    "!MYSQL_EXE!" -u root medmecs_mis < "sql\seed-additional-roles.sql" 2>nul
    echo   Additional roles seeded (Manager, Supervisor, User).
)

REM Remove any leftover Blue Central demo rows just in case (safe to run on clean DBs too)
if exist "sql\clean-demo-data.sql" (
    "!MYSQL_EXE!" -u root medmecs_mis < "sql\clean-demo-data.sql" >nul 2>&1
)
echo.

color 0A
echo ============================================================
echo   SETUP COMPLETE
echo ============================================================
echo.
echo   Next: make sure XAMPP Apache and MySQL are RUNNING in the
echo   XAMPP Control Panel, then run create-htdocs-link.bat once
echo   (only the first time).
echo.
echo   Login URL:    http://localhost/medmecs/login.php
echo.
echo   Email:        admin@medmecs.local
echo   Password:     ChangeMe@2026
echo.
echo   You will be asked to change the password on first login.
echo.
echo   Then go to:   Reports browser ^> Collection Dashboard
echo                 1. Upload Facility Mapping CSV (top-right action)
echo                 2. Upload Collection Raw CSV   (top-right action)
echo                 3. (Optional) Replace fee schedule with client's actual
echo.
echo   To add another client's Collection Dashboard later, create a new
echo   Vertical / Client / Process / Report row with handler='collection_dashboard'.
echo   All data tables are scoped by report_id for multi-tenancy.
echo.
pause
