#!/usr/bin/env bash
# Genererer LiteLLM config.yaml fra ai_model_aliases/providers i PostgreSQL.
# Kjøres ved oppstart, ved endringer i admin-panelet, eller manuelt.
#
# Bruk:
#   ./scripts/generate-litellm-config.sh [--restart]
#
# --restart: restart ai-gateway containeren etter generering.

set -euo pipefail

CONFIG_FILE="/srv/synops/config/litellm/config.yaml"
DOCKER_CONTAINER="sidelinja-ai-gateway-1"
PG_CONTAINER="sidelinja-postgres-1"
PG_USER="sidelinja"
PG_DB="synops"
ENV_FILE="/srv/synops/.env"

read_env() { grep "^$1=" "$ENV_FILE" 2>/dev/null | head -1 | cut -d= -f2; }

psql_cmd() {
    docker exec "$PG_CONTAINER" psql -U "$PG_USER" -d "$PG_DB" -tAF '|' -c "$1"
}

# Hent aktive aliaser og deres providers, sortert etter alias + priority
PROVIDERS=$(psql_cmd "
    SELECT a.alias, p.model, p.api_key_env, p.priority
    FROM ai_model_providers p
    JOIN ai_model_aliases a ON p.alias_id = a.id
    WHERE a.is_active = true AND p.is_active = true
    ORDER BY a.alias, p.priority;
")

if [ -z "$PROVIDERS" ]; then
    echo "FEIL: Ingen aktive providers funnet i databasen." >&2
    exit 1
fi

# Generer YAML — filtrerer bort providers med tomme API-nøkler
{
    echo "model_list:"

    current_alias=""
    included=0
    while IFS='|' read -r alias model api_key_env priority; do
        # Sjekk at API-nøkkelen finnes og er ikke-tom i .env
        key_value=$(read_env "$api_key_env")
        if [ -z "$key_value" ]; then
            echo "  # HOPPET OVER: ${model} (${api_key_env} ikke satt)" >&2
            continue
        fi

        if [ "$alias" != "$current_alias" ]; then
            echo "  # === ${alias} ==="
            current_alias="$alias"
        fi
        # priority i DB er 1-basert, LiteLLM priority er 0-basert (lavere = høyere)
        litellm_priority=$((priority - 1))
        cat <<ENTRY
  - model_name: "${alias}"
    litellm_params:
      model: "${model}"
      api_key: "os.environ/${api_key_env}"
    model_info:
      priority: ${litellm_priority}
ENTRY
        included=$((included + 1))
    done <<< "$PROVIDERS"

    if [ "$included" -eq 0 ]; then
        echo "FEIL: Ingen providers med gyldige API-nøkler." >&2
        exit 1
    fi

    cat <<'FOOTER'

router_settings:
  routing_strategy: "simple-shuffle"
  num_retries: 3
  timeout: 60
  allowed_fails: 1
  retry_after: 5
  enable_pre_call_checks: true

general_settings:
  master_key: "os.environ/LITELLM_MASTER_KEY"
FOOTER
} | sudo tee "$CONFIG_FILE" > /dev/null

echo "Config generert: $CONFIG_FILE"

# Eventuelt restart
if [[ "${1:-}" == "--restart" ]]; then
    echo "Restarter $DOCKER_CONTAINER..."
    sudo docker restart "$DOCKER_CONTAINER" > /dev/null
    echo "Ferdig."
fi
