#!/bin/bash

# Directories for schema and population scripts
CREATE_DIR="create"
POPULATE_DIR="populate"

# List of databases
DATABASES=("auth" "blueprints" "fights" "live")

# Function to create a database and apply the schema
create_db() {
    for db in "${DATABASES[@]}"; do
        db_file="${db}.db"
        schema_file="${CREATE_DIR}/${db}.sql"

        if [[ -f "$schema_file" ]]; then
            echo "Creating $db_file and applying schema from $schema_file..."
            sqlite3 "$db_file" < "$schema_file"
        else
            echo "Schema file for $db not found. Skipping."
        fi
    done
}

# Function to populate the database with data
populate_db() {
    for db in "${DATABASES[@]}"; do
        db_file="${db}.db"
        populate_file="${POPULATE_DIR}/${db}.sql"

        if [[ -f "$populate_file" ]]; then
            echo "Populating $db_file using data from $populate_file..."
            sqlite3 "$db_file" < "$populate_file"
        else
            echo "Population file for $db not found. Skipping."
        fi
    done
}

# Function to drop (delete) the databases
drop_db() {
    for db in "${DATABASES[@]}"; do
        db_file="${db}.db"

        if [[ -f "$db_file" ]]; then
            echo "Dropping $db_file..."
            rm "$db_file"
        else
            echo "$db_file does not exist. Skipping."
        fi
    done
}

# Function for a full reset (create and populate)
full_reset() {
    create_db
    populate_db
}

# Main script
case $1 in
    create)
        create_db
        ;;
    populate)
        populate_db
        ;;
    reset)
        full_reset
        ;;
    drop)
        drop_db
        ;;
    *)
        echo "Usage: $0 {create|populate|reset}"
        ;;
esac
