@echo off
REM ========================================================================
REM DATABASE DEPLOYMENT SCRIPT FOR WINDOWS
REM ========================================================================
REM This script deploys the CMS database with multi-organization support
REM 
REM WARNING: This will DELETE all existing data in cms_db database!
REM Only use for development or fresh installations.
REM ========================================================================

echo.
echo ========================================================================
echo  CMS Database Deployment - Multi-Organization Support
echo ========================================================================
echo.
echo WARNING: This will DROP and RECREATE the cms_db database!
echo All existing data will be LOST!
echo.
set /p confirm="Are you sure you want to continue? (yes/no): "

if /i NOT "%confirm%"=="yes" (
    echo.
    echo Deployment cancelled.
    pause
    exit /b
)

echo.
echo ========================================================================
echo Step 1: Dropping existing database...
echo ========================================================================
mysql -u root -p -e "DROP DATABASE IF EXISTS cms_db;"
if errorlevel 1 (
    echo ERROR: Failed to drop database!
    pause
    exit /b 1
)
echo Database dropped successfully.

echo.
echo ========================================================================
echo Step 2: Creating fresh database...
echo ========================================================================
mysql -u root -p -e "CREATE DATABASE cms_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
if errorlevel 1 (
    echo ERROR: Failed to create database!
    pause
    exit /b 1
)
echo Database created successfully.

echo.
echo ========================================================================
echo Step 3: Importing schema with organizations support...
echo ========================================================================
mysql -u root -p cms_db < database_schema.sql
if errorlevel 1 (
    echo ERROR: Failed to import schema!
    pause
    exit /b 1
)
echo Schema imported successfully.

echo.
echo ========================================================================
echo Step 4: Verifying deployment...
echo ========================================================================
mysql -u root -p cms_db -e "SELECT 'Organizations:' AS Info, COUNT(*) AS Count FROM organizations; SELECT 'Units:' AS Info, COUNT(*) AS Count FROM units; SELECT 'Departments:' AS Info, COUNT(*) AS Count FROM departments; SELECT 'Users:' AS Info, COUNT(*) AS Count FROM users;"

echo.
echo ========================================================================
echo  DEPLOYMENT COMPLETE!
echo ========================================================================
echo.
echo Database: cms_db
echo Organization: Bangladesh Navy (ID: 1)
echo Tables Created: 22
echo.
echo Default Login Credentials:
echo   Email: s_admin@gmail.com
echo   Password: (as configured in schema)
echo.
echo Next Steps:
echo   1. Start backend server: npm start (in backend directory)
echo   2. Access dashboard to verify data
echo   3. Check console logs for organization_id values
echo.
echo Documentation:
echo   - ORGANIZATION_QUICK_START.md
echo   - ORGANIZATION_SCHEMA_MIGRATION_GUIDE.md
echo   - SCHEMA_VERIFICATION_SUMMARY.md
echo.
pause

