@echo off
color 0A
title CargoFlow - Complete Setup Wizard

:MENU
cls
echo.
echo ╔════════════════════════════════════════════════════════════════╗
echo ║                                                                ║
echo ║          CARGOFLOW - COMPLETE SETUP WIZARD                    ║
echo ║          Cargo Management System                               ║
echo ║                                                                ║
echo ╚════════════════════════════════════════════════════════════════╝
echo.
echo  Please select an option:
echo.
echo  [1] Complete Setup (All Steps)
echo  [2] Create Database Only
echo  [3] Install Dependencies Only
echo  [4] Run Migrations Only
echo  [5] Start Server
echo  [6] View Setup Checklist
echo  [7] Test Database Connection
echo  [0] Exit
echo.
set /p choice="Enter your choice (0-7): "

if "%choice%"=="1" goto COMPLETE_SETUP
if "%choice%"=="2" goto CREATE_DB
if "%choice%"=="3" goto INSTALL_DEPS
if "%choice%"=="4" goto RUN_MIGRATIONS
if "%choice%"=="5" goto START_SERVER
if "%choice%"=="6" goto VIEW_CHECKLIST
if "%choice%"=="7" goto TEST_CONNECTION
if "%choice%"=="0" goto EXIT
goto MENU

:COMPLETE_SETUP
cls
echo ════════════════════════════════════════════════════════════════
echo  COMPLETE SETUP - ALL STEPS
echo ════════════════════════════════════════════════════════════════
echo.
echo [Step 1/5] Installing Composer Dependencies...
echo.
call composer install
if errorlevel 1 (
    echo ERROR: Composer install failed!
    pause
    goto MENU
)

echo.
echo [Step 2/5] Installing PDF Library...
echo.
call composer require barryvdh/laravel-dompdf
if errorlevel 1 (
    echo ERROR: PDF library installation failed!
    pause
    goto MENU
)

echo.
echo [Step 3/5] Creating Database...
echo Please enter your MySQL root password:
echo.
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS cargo_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
if errorlevel 1 (
    echo.
    echo WARNING: Database creation failed or already exists
    echo Please create database manually if needed
    echo.
)

echo.
echo [Step 4/5] Running Migrations...
echo.
call php artisan migrate
if errorlevel 1 (
    echo ERROR: Migration failed!
    echo Please check your database configuration in .env
    pause
    goto MENU
)

echo.
echo [Step 5/5] Clearing Cache...
echo.
call php artisan config:clear
call php artisan cache:clear

echo.
echo ════════════════════════════════════════════════════════════════
echo  ✓ SETUP COMPLETE!
echo ════════════════════════════════════════════════════════════════
echo.
echo  Your CargoFlow system is ready to use!
echo.
echo  To start the server:
echo    php artisan serve
echo.
echo  Then visit:
echo    http://localhost:8000
echo.
echo  Press any key to return to menu...
pause >nul
goto MENU

:CREATE_DB
cls
echo ════════════════════════════════════════════════════════════════
echo  CREATE DATABASE
echo ════════════════════════════════════════════════════════════════
echo.
echo  Database Name: cargo_management
echo  Character Set: utf8mb4
echo  Collation: utf8mb4_unicode_ci
echo.
echo  Please enter your MySQL root password:
echo.
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS cargo_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; SHOW DATABASES LIKE 'cargo_management';"
if errorlevel 1 (
    echo.
    echo ERROR: Database creation failed!
    echo.
    echo Alternative methods:
    echo 1. Open phpMyAdmin (http://localhost/phpmyadmin)
    echo 2. Click "New" and create database "cargo_management"
    echo 3. Or use MySQL Command Line Client
    echo.
) else (
    echo.
    echo ✓ Database created successfully!
    echo.
)
pause
goto MENU

:INSTALL_DEPS
cls
echo ════════════════════════════════════════════════════════════════
echo  INSTALL DEPENDENCIES
echo ════════════════════════════════════════════════════════════════
echo.
echo [1/2] Installing Composer packages...
echo.
call composer install
if errorlevel 1 (
    echo ERROR: Composer install failed!
    pause
    goto MENU
)

echo.
echo [2/2] Installing PDF library...
echo.
call composer require barryvdh/laravel-dompdf
if errorlevel 1 (
    echo ERROR: PDF library installation failed!
    pause
    goto MENU
)

echo.
echo ✓ All dependencies installed successfully!
echo.
pause
goto MENU

:RUN_MIGRATIONS
cls
echo ════════════════════════════════════════════════════════════════
echo  RUN DATABASE MIGRATIONS
echo ════════════════════════════════════════════════════════════════
echo.
echo  This will create all required tables in your database.
echo.
echo  Tables to be created:
echo    - customers
echo    - rates
echo    - bookings
echo    - shipment_tracking
echo    - sessions
echo    - cache
echo    - jobs
echo.
pause
echo.
call php artisan migrate
if errorlevel 1 (
    echo.
    echo ERROR: Migration failed!
    echo.
    echo Please check:
    echo 1. Database exists (cargo_management)
    echo 2. Database credentials in .env are correct
    echo 3. MySQL server is running
    echo.
) else (
    echo.
    echo ✓ Migrations completed successfully!
    echo.
)
pause
goto MENU

:START_SERVER
cls
echo ════════════════════════════════════════════════════════════════
echo  START DEVELOPMENT SERVER
echo ════════════════════════════════════════════════════════════════
echo.
echo  Starting Laravel development server...
echo.
echo  Server will be available at:
echo    http://localhost:8000
echo.
echo  Press Ctrl+C to stop the server
echo.
echo ════════════════════════════════════════════════════════════════
echo.
call php artisan serve
pause
goto MENU

:VIEW_CHECKLIST
cls
type SETUP_CHECKLIST.txt
echo.
echo.
pause
goto MENU

:TEST_CONNECTION
cls
echo ════════════════════════════════════════════════════════════════
echo  TEST DATABASE CONNECTION
echo ════════════════════════════════════════════════════════════════
echo.
echo  Testing connection to cargo_management database...
echo.
mysql -u root -p -e "USE cargo_management; SELECT 'Connection successful!' AS Status; SHOW TABLES;"
if errorlevel 1 (
    echo.
    echo ✗ Connection failed!
    echo.
    echo Please check:
    echo 1. MySQL is running
    echo 2. Database exists
    echo 3. Credentials are correct
    echo.
) else (
    echo.
    echo ✓ Connection successful!
    echo.
)
pause
goto MENU

:EXIT
cls
echo.
echo ════════════════════════════════════════════════════════════════
echo  Thank you for using CargoFlow!
echo ════════════════════════════════════════════════════════════════
echo.
echo  For help, check these files:
echo    - START_HERE.md
echo    - QUICK_START.md
echo    - DATABASE_SETUP.md
echo.
echo  Happy shipping! 🚢
echo.
timeout /t 3
exit
