@echo off
echo ========================================
echo  CargoFlow - Database Setup
echo ========================================
echo.

echo Please choose your MySQL setup method:
echo.
echo 1. I have MySQL command line in PATH
echo 2. I will create database manually
echo.
set /p choice="Enter your choice (1 or 2): "

if "%choice%"=="1" goto auto_setup
if "%choice%"=="2" goto manual_setup

:auto_setup
echo.
echo Creating database automatically...
set /p mysql_password="Enter MySQL root password (press Enter if no password): "

if "%mysql_password%"=="" (
    mysql -u root -e "CREATE DATABASE IF NOT EXISTS cargo_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
) else (
    mysql -u root -p%mysql_password% -e "CREATE DATABASE IF NOT EXISTS cargo_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
)

if errorlevel 1 (
    echo.
    echo Error: Could not create database automatically.
    echo Please use option 2 to create manually.
    pause
    exit /b 1
)

echo Database created successfully!
goto run_migrations

:manual_setup
echo.
echo ========================================
echo  Manual Database Creation Steps
echo ========================================
echo.
echo Please follow these steps:
echo.
echo 1. Open MySQL Workbench, phpMyAdmin, or MySQL command line
echo 2. Run this SQL command:
echo.
echo    CREATE DATABASE cargo_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
echo.
echo 3. Or import the file: create_database.sql
echo.
echo Press any key after you have created the database...
pause > nul

:run_migrations
echo.
echo ========================================
echo  Running Database Migrations
echo ========================================
echo.

php artisan migrate

if errorlevel 1 (
    echo.
    echo ========================================
    echo  Migration Error!
    echo ========================================
    echo.
    echo Please check:
    echo 1. MySQL is running
    echo 2. Database 'cargo_management' exists
    echo 3. .env file has correct database credentials
    echo.
    echo Current .env settings:
    echo DB_HOST=127.0.0.1
    echo DB_PORT=3306
    echo DB_DATABASE=cargo_management
    echo DB_USERNAME=root
    echo DB_PASSWORD=(check your .env file)
    echo.
    pause
    exit /b 1
)

echo.
echo ========================================
echo  Database Setup Complete!
echo ========================================
echo.
echo Database 'cargo_management' is ready with all tables:
echo - customers
echo - rates
echo - bookings
echo - shipment_tracking
echo.
echo You can now start the application:
echo   php artisan serve
echo.
echo Then visit: http://localhost:8000
echo.
pause
