#!/bin/bash

# Modal Manager (mm) script

# The great thing about modal workers is that they're so easy to deploy.  The terrible thing about modal
# workers is that they're... so easy to deploy.  The purpose of this script is to provide a thin
# abstraction around modal deployments in order to ensure that each `modal run` or `modal deploy` command
# does exactly what you expect it to.

# The usage of the script is as follows:
# `mm [test|deploy] <script_name.py> [prod]`

# When the script is invoked without `prod`, the environment is assumed to be `dev`.  In that case, we
# check the DEPLOYMENT_TYPE in the script to ensure it's actually dev, exiting if that's not the case.  If the
# environment is correct, we test (`modal run`) or deploy (`modal deploy`) as desired.

# When the script is invoked with `prod`, we perform several checks:
# - check the DEPLOYMENT_TYPE in the script to ensure it's set to prod.
# - check that we're on the main branch
# - check that local main is up to date with the remote
# - check for explicit confirmation from the user (the user must type "I want to test/deploy prod")

# Configuration
REPO_DIR="$HOME/glockenspiel/suno_utils"

# Function to check if a file contains a specific string
check_deployment_type() {
    local script_name=$1
    local deployment_type=$2
    local full_path="$REPO_DIR/suno_utils/worker/$script_name"
    
    if [[ ! -f "$full_path" ]]; then
        echo "Error: Script $script_name not found in $REPO_DIR/suno_utils/worker/"
        exit 1
    fi
    
    grep -q "DEPLOYMENT_TYPE = \"$deployment_type\"" "$full_path"
    return $?
}

# Function to check git status
check_git_status() {
    if [[ $(git -C "$REPO_DIR" rev-parse --abbrev-ref HEAD) != "main" ]]; then
        echo "Error: Not on main branch"
        exit 1
    fi
    git -C "$REPO_DIR" fetch origin main
    if [[ $(git -C "$REPO_DIR" rev-list HEAD...origin/main --count) != "0" ]]; then
        echo "Error: Local main branch is not up to date with remote"
        exit 1
    fi
}

# Function to get user confirmation
get_confirmation() {
    local expected_input=$1
    read -p "Type '$expected_input' to confirm: " user_input
    if [[ "$user_input" != "$expected_input" ]]; then
        echo "Confirmation failed. Exiting."
        exit 1
    fi
}

# Main script logic
if [[ $# -lt 2 ]]; then
    echo "Usage: mm [test|deploy] <script_name.py> [prod]"
    exit 1
fi

action=$1
script_name=$2
env=${3:-dev}

case "$action" in
    test)
        if [[ "$env" == "prod" ]]; then
            get_confirmation "I want to test prod"
            check_git_status
            if ! check_deployment_type "$script_name" "prod"; then
                echo "Error: DEPLOYMENT_TYPE is not set to 'prod' in $script_name"
                exit 1
            fi
        else
            if ! check_deployment_type "$script_name" "dev"; then
                echo "Error: DEPLOYMENT_TYPE is not set to 'dev' in $script_name"
                exit 1
            fi
        fi
        (cd "$REPO_DIR" && uv run modal run suno_utils/worker/$script_name && cd -)
        ;;
    deploy)
        if [[ "$env" == "prod" ]]; then
            get_confirmation "I want to deploy prod"
            check_git_status
            if ! check_deployment_type "$script_name" "prod"; then
                echo "Error: DEPLOYMENT_TYPE is not set to 'prod' in $script_name"
                exit 1
            fi
        else
            if ! check_deployment_type "$script_name" "dev"; then
                echo "Error: DEPLOYMENT_TYPE is not set to 'dev' in $script_name"
                exit 1
            fi
        fi
        (cd "$REPO_DIR" && uv run modal deploy suno_utils/worker/$script_name && cd -)
        ;;
    *)
        echo "Invalid action. Use 'test' or 'deploy'."
        exit 1
        ;;
esac
