#!/bin/bash
# Database Configuration
# Edit this file to change which database commands run against

# Database connection settings
PSQL_PATH="/usr/bin/psql"
DB_USER="jellyfin"
DB_NAME="jellyfin_test2"  # ← Change this to switch databases
DB_HOST="192.168.129.253"
DB_PORT="6432"

# Export for use in other scripts
export PSQL_PATH
export DB_USER
export DB_NAME
export DB_HOST
export DB_PORT

# Helper function to run psql commands
invoke_psql() {
    local query=""
    local file=""
    local output_file=""

    while [[ $# -gt 0 ]]; do
        case "$1" in
            -q|--query)
                query="$2"
                shift 2
                ;;
            -f|--file)
                file="$2"
                shift 2
                ;;
            -o|--output-file)
                output_file="$2"
                shift 2
                ;;
            *)
                echo "Unknown option: $1"
                return 1
                ;;
        esac
    done

    local base_cmd="$PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME"
    local cmd=""

    if [ -n "$file" ]; then
        cmd="$base_cmd -f \"$file\""
    elif [ -n "$query" ]; then
        cmd="$base_cmd -c \"$query\""
    else
        echo "Must provide either --query or --file parameter"
        return 1
    fi

    if [ -n "$output_file" ]; then
        cmd+=" > \"$output_file\""
    fi

    echo -e "\e[36mConnecting to: $DB_NAME@$DB_HOST as $DB_USER\e[0m"
    eval "$cmd"
}

# Display current configuration
echo -e "\e[36m========================================\e[0m"
echo -e "\e[36mDatabase Configuration Loaded\e[0m"
echo -e "\e[36m========================================\e[0m"
echo -e "\e[33mDatabase: $DB_NAME\e[0m"
echo -e "\e[33mUser: $DB_USER\e[0m"
echo -e "\e[33mHost: $DB_HOST\e[0m"
echo -e "\e[33mPort: $DB_PORT\e[0m"
echo -e "\e[36m========================================\e[0m"
echo ""
echo -e "\e[90mTo change database, edit: db-config.sh\e[0m"
echo -e "\e[90mThen run: source db-config.sh\e[0m"
echo ""
