#!/usr/bin/env bash
# ai-status - Diagnostics for Py-Agent Cloud Models & Quotas v2.3.0

set -euo pipefail

ENV_PATH="$HOME/.config/fetch/.env"
LOG_FILE="$HOME/.config/fetch/.request_log"

# Safely source active keys from .env into script environment
if [[ -f "$ENV_PATH" ]]; then
    set +u
    set -a
    source "$ENV_PATH"
    set +a
    set -u
fi

# Helper to dynamically map Google AI Studio Gemini rate limits
get_gemini_limits() {
    local model="${1:-gemini-3.8-flash}"
    local m_low; m_low=$(echo "$model" | tr '[:upper:]' '[:lower:]')
    
    # 1. Flash-Lite Models (Higher daily/RPM throughput)
    if [[ "$m_low" == *"3.5-flash-lite"* || "$m_low" == *"3.1-flash-lite"* ]]; then
        echo "15 RPM / 1,000 RPD (Free Tier)"
    elif [[ "$m_low" == *"2.5-flash-lite"* || "$m_low" == *"2.0-flash-lite"* ]]; then
        echo "30 RPM / 1,500 RPD (Free Tier)"

    # 2. Heavy Reasoning / Restricted 3.x Flash Models
    elif [[ "$m_low" == *"3.8-flash"* ]]; then
        echo "5 RPM / 20 RPD (Free Tier)"
    elif [[ "$m_low" == *"3.7-flash"* || "$m_low" == *"3.6-flash"* || "$m_low" == *"3.5-flash"* ]]; then
        echo "5 RPM / 25 RPD (Free Tier)"

    # 3. Pro Models
    elif [[ "$m_low" == *"-pro"* ]]; then
        echo "5 RPM / 25 RPD (Free Tier)"

    # 4. Legacy Standard Flash Models (1.5, 2.0, 2.5)
    elif [[ "$m_low" == *"2.5-flash"* || "$m_low" == *"2.0-flash"* || "$m_low" == *"1.5-flash"* ]]; then
        echo "10 RPM / 250 RPD (Free Tier)"

    # Safe fallback default
    else
        echo "5 RPM / 25 RPD (Free Tier)"
    fi
}

# Helper to mask API keys safely
mask_key() {
    [[ -z "${1:-}" ]] && echo "*Not Exported*" || echo "Enabled (\`${1:0:6}...${1: -4}\`)"
}

# Unified 1-line connection and key-existence checker
check_conn() {
    local key="${1:-}" endpoint="${2:-}"
    [[ -z "$key" || -z "$endpoint" ]] && { echo 1; return; }
    
    # Extract scheme + host for safe ping check
    local host_url
    host_url=$(echo "$endpoint" | grep -o -E '^https?://[^/]+' || echo "$endpoint")
    
    curl -s -I --connect-timeout 1.5 "$host_url" >/dev/null 2>&1 && echo 0 || echo 1
}

# --- DYNAMIC TOP-DOWN SCRIPT PARSING ---
active_providers=()
active_keys=()
active_models=()
active_urls=()

if [[ -f "$ENV_PATH" ]]; then
    while IFS= read -r line || [[ -n "$line" ]]; do
        stripped=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' 2>/dev/null || echo "$line")
        [[ -z "$stripped" || "$stripped" == "#"* ]] && continue
        
        # Match active key assignments: e.g. CUSTOM2_API_KEY="xxx"
        if [[ "$stripped" =~ ^([A-Z0-9_]+_API_KEY|[A-Z0-9_]+_KEY)=\"?(.*)\"?$ ]]; then
            k_name="${BASH_REMATCH[1]}"
            k_val="${BASH_REMATCH[2]}"
            k_val=$(echo "$k_val" | sed -e 's/"$//' -e "s/'$//" 2>/dev/null || echo "$k_val")
            
            # Skip unconfigured placeholders
            if [[ -n "$k_val" && "$k_val" != *"your"* && "$k_val" != *"here"* ]]; then
                prefix=$(echo "$k_name" | sed -E 's/_(API_)?KEY$//')
                provider=$(echo "$prefix" | tr '[:upper:]' '[:lower:]')
                
                m_var="${prefix}_MODEL"
                u_var="${prefix}_URL"
                
                # Defaults
                if [[ "$provider" == "openrouter" ]]; then
                    m_val="${OPENROUTER_MODEL:-openrouter/free}"
                    u_val="https://openrouter.ai/api/v1/chat/completions"
                elif [[ "$provider" == "gemini" ]]; then
                    m_val="${GEMINI_MODEL:-gemini-3.8-flash}"
                    u_val="https://generativelanguage.googleapis.com"
                elif [[ "$provider" == "custom2" ]]; then
                    m_val="${CUSTOM2_MODEL:-deepseek-chat}"
                    u_val="${CUSTOM2_URL:-https://api.deepseek.com/chat/completions}"
                elif [[ "$provider" == "custom" ]]; then
                    m_val="${CUSTOM_MODEL:-Qwen/Qwen3.8-27B}"
                    u_val="${CUSTOM_URL:-https://router.huggingface.co/v1/chat/completions}"
                else
                    m_val="${!m_var:-default-model}"
                    u_val="${!u_var:-https://api.openai.com/v1/chat/completions}"
                fi
                
                if [[ ! " ${active_providers[*]:-} " =~ " ${provider} " ]]; then
                    active_providers+=("$provider")
                    active_keys+=("$k_val")
                    active_models+=("$m_val")
                    active_urls+=("$u_val")
                fi
            fi
        fi
    done < "$ENV_PATH"
fi

# Fetch cloud metrics and prune old logs in a single Python pass
request_stats="0|0|0|0"
if [[ -f "$LOG_FILE" ]]; then
    request_stats=$(python3 -c "
import os, time
log_file = os.path.expanduser('~/.config/fetch/.request_log')
now = int(time.time())
day_ago, min_ago = now - 86400, now - 60
rpd = {'gemini': 0, 'openrouter': 0}
rpm = {'gemini': 0, 'openrouter': 0}
valid = []
try:
    with open(log_file, 'r') as f:
        for line in f:
            parts = line.strip().split('|')
            if len(parts) == 2 and parts[0].isdigit():
                ts, provider = int(parts[0]), parts[1]
                if ts >= day_ago:
                    valid.append(f'{ts}|{provider}')
                    if provider in rpd:
                        rpd[provider] += 1
                        if ts >= min_ago:
                            rpm[provider] += 1
    with open(log_file, 'w') as f:
        for entry in valid:
            f.write(f'{entry}\n')
except:
    pass
print(f\"{rpd['gemini']}|{rpm['gemini']}|{rpd['openrouter']}|{rpm['openrouter']}\")
" 2>/dev/null || echo "0|0|0|0")
fi

gemini_rpd=$(echo "$request_stats" | cut -d'|' -f1)
gemini_rpm=$(echo "$request_stats" | cut -d'|' -f2)
or_rpd=$(echo "$request_stats" | cut -d'|' -f3)
or_rpm=$(echo "$request_stats" | cut -d'|' -f4)

# Probe endpoints
custom_status=$(check_conn "${CUSTOM_API_KEY:-}" "${CUSTOM_URL:-https://router.huggingface.co}")
custom2_status=$(check_conn "${CUSTOM2_API_KEY:-}" "${CUSTOM2_URL:-https://api.deepseek.com}")
gemini_status=$(check_conn "${GEMINI_API_KEY:-}" "https://generativelanguage.googleapis.com")
or_status=$(check_conn "${OPENROUTER_API_KEY:-}" "https://openrouter.ai")
local_status=$(curl -s -I --connect-timeout 1.0 "http://localhost:8080" >/dev/null 2>&1 && echo 0 || echo 1)

# Fetch Local Server model if online
local_model="Local Server"
if [[ $local_status -eq 0 ]]; then
    local_model=$(curl -s "http://localhost:8080/v1/models" | python3 -c "
import sys, json
try:
    models = json.load(sys.stdin).get('data', [])
    if models: print(models[0].get('id', 'Local Server'))
    else: print('Local Server')
except:
    print('Local Server')
" 2>/dev/null || echo "Local Server")
fi

# Evaluate active runtime route based on strict top-down .env priority
resolved_provider="*None / All Offline*"
resolved_model="*None*"

for i in "${!active_providers[@]}"; do
    prov="${active_providers[$i]}"
    key_v="${active_keys[$i]}"
    mod_v="${active_models[$i]}"
    url_v="${active_urls[$i]}"
    
    status=$(check_conn "$key_v" "$url_v")
    if [[ $status -eq 0 ]]; then
        if [[ "$prov" == "custom" ]]; then resolved_provider="Hugging Face / Custom 1"
        elif [[ "$prov" == "custom2" ]]; then
            if [[ "$url_v" == *"deepseek"* ]]; then resolved_provider="DeepSeek Direct"
            else resolved_provider="Custom 2 Direct"
            fi
        elif [[ "$prov" == "gemini" ]]; then resolved_provider="Google Gemini"
        elif [[ "$prov" == "openrouter" ]]; then resolved_provider="OpenRouter"
        else resolved_provider=$(echo "$prov" | tr '[:lower:]' '[:upper:]')
        fi
        resolved_model="$mod_v"
        break
    fi
done

if [[ "$resolved_provider" == "*None / All Offline*" && $local_status -eq 0 ]]; then
    resolved_provider="Local AI Server"
    resolved_model="$local_model"
fi

st_label() {
    local key="$1" status="$2"
    if [[ -z "$key" ]]; then echo "Disabled (No Key)"
    elif [[ "$status" -eq 0 ]]; then echo "**Online**"
    else echo "Offline"
    fi
}

gemini_cap=$(get_gemini_limits "${GEMINI_MODEL:-gemini-3.8-flash}")

# Custom 2 Labeling
c2_url="${CUSTOM2_URL:-https://api.deepseek.com/chat/completions}"
c2_label="Custom 2"
if [[ "$c2_url" == *"deepseek"* ]]; then c2_label="Custom 2 (DeepSeek)"
elif [[ "$c2_url" == *"groq"* ]]; then c2_label="Custom 2 (Groq)"
elif [[ "$c2_url" == *"mistral"* ]]; then c2_label="Custom 2 (Mistral)"
fi

echo "### Cloud & Provider System Status"
echo "| Provider | Active Model | Status | Quota & Live Activity |"
echo "| :--- | :--- | :--- | :--- |"
echo "| **Active Route (${resolved_provider})** | \`${resolved_model}\` | **Online** | Priority Active |"
echo "| **Custom 1 (Local / HF)** | \`${CUSTOM_MODEL:-Qwen/Qwen3.8-27B}\` | $(st_label "${CUSTOM_API_KEY:-}" "$custom_status") | Router / Spaces |"
echo "| **${c2_label}** | \`${CUSTOM2_MODEL:-deepseek-chat}\` | $(st_label "${CUSTOM2_API_KEY:-}" "$custom2_status") | Direct Endpoint |"
echo "| **Google Gemini** | \`${GEMINI_MODEL:-gemini-3.8-flash}\` | $(st_label "${GEMINI_API_KEY:-}" "$gemini_status") | RPM: ${gemini_rpm} / RPD: ${gemini_rpd} • Cap: ${gemini_cap} |"
echo "| **OpenRouter** | \`${OPENROUTER_MODEL:-openrouter/free}\` | $(st_label "${OPENROUTER_API_KEY:-}" "$or_status") | RPM: ${or_rpm} / RPD: ${or_rpd} • Cap: 20 RPM / 50 RPD |"
echo "| **Local Server** | \`${local_model}\` | $([[ $local_status -eq 0 ]] && echo "**Online**" || echo "Offline") | Port 8080 |"
