#!/bin/bash

# Configuration
DB_USER="root"
DB_PASS="" # Allow empty password
DB_HOST="127.0.0.1"

# Database Names
DB1="maiadmin2024"
DB2="maidatmas"
DB3="maisppnew"
DB4="maisavingb"

# SQL File Paths (Assuming they are in the parent directory)
SQL1="../sipschoo_admin.sql"
SQL2="../sipschoo_datmas.sql"
SQL3="../sipschoo_spp.sql"
SQL4="../sipschoo_savingb.sql"

echo "Checking if mysql command exists..."
if ! command -v mysql &> /dev/null; then
    echo "Error: mysql command not found. Please install MySQL Client or run from a terminal where mysql is available."
    exit 1
fi

# Function to import DB
import_db() {
    local dbname=$1
    local sqlfile=$2

    echo "----------------------------------------"
    echo "Processing Database: $dbname"
    
    # Create DB if not exists
    echo "Creating database if not exists..."
    mysql -u"$DB_USER" -h"$DB_HOST" -e "CREATE DATABASE IF NOT EXISTS $dbname;"
    
    # Import
    if [ -f "$sqlfile" ]; then
        echo "Importing $sqlfile..."
        mysql -u"$DB_USER" -h"$DB_HOST" "$dbname" < "$sqlfile"
        echo "Import complete."
    else
        echo "Warning: SQL file $sqlfile not found via path. Skipping import."
    fi
}

import_db "$DB1" "$SQL1"
import_db "$DB2" "$SQL2"
import_db "$DB3" "$SQL3"
import_db "$DB4" "$SQL4"

echo "----------------------------------------"
echo "✅ Database Setup Complete!"
