#!/usr/bin/env bash

# Usage: ./db_migrate.sh -m "Your migration message" -d migrations/public

# Load environment variables
set -a
source .env
set +a

project_dir=$(pwd)
# Ensure you're in the correct directory
cd "$project_dir" || exit 1

# Load environment variables (including database connection details)
source .env

# Set Flask app and Python path
export FLASK_APP=scripts/run.py
export PYTHONPATH="$PYTHONPATH:$project_dir"

while getopts m:d: flag
do
    case "${flag}" in
        m) message=${OPTARG};;
        d) directory=${OPTARG};;
        *)
            echo "Invalid option: ${flag}"
            exit 1
        ;;
    esac
done

# Check if the message and directory are provided
if [ -z "$message" ] || [ -z "$directory" ]; then
    echo "Both message and directory are required."
    echo "Usage: ./db_migrate.sh -m \"Your migration message\" -d migrations/public"
    exit 1
fi

# Run the Flask migration command
flask db migrate -m "$message" -d "$directory"