#!/usr/bin/env python3
# =============================================================
# list_models.py — Parāda VISUS pieejamos Gemini modeļus
# =============================================================
# Palaid cPanel: Python App → "Execute python script"
# Fails: list_models.py
# =============================================================

import os
import requests
from pathlib import Path

# Ielādē .env
env_path = Path(__file__).resolve().parent / ".env"
if env_path.exists():
    with open(env_path) as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith("#") and "=" in line:
                key, _, val = line.partition("=")
                os.environ.setdefault(key.strip(), val.strip())

api_key = os.getenv("GEMINI_API_KEY", "")

print("Pieejamie Gemini modeļi:\n")

# Iegūst sarakstu
for api_ver in ["v1", "v1beta"]:
    url  = f"https://generativelanguage.googleapis.com/{api_ver}/models?key={api_key}"
    resp = requests.get(url, timeout=15)

    if resp.status_code != 200:
        print(f"[{api_ver}] Kļūda: {resp.status_code}")
        continue

    models = resp.json().get("models", [])
    gemini = [
        m["name"] for m in models
        if "gemini" in m["name"].lower()
        and "generateContent" in m.get("supportedGenerationMethods", [])
    ]

    print(f"[{api_ver}] generateContent atbalsta {len(gemini)} Gemini modeļi:")
    for name in sorted(gemini):
        print(f"  → {name}")
    print()
