#!/usr/bin/env python3
# check_settings.py — Palaid cPanel Execute python script

import os
import sys
from pathlib import Path

# Ielādē .env
env_path = Path(__file__).resolve().parent / ".env"
print(f".env ceļš: {env_path}")
print(f".env eksistē: {env_path.exists()}\n")

if env_path.exists():
    print("=== .env SATURS (maskēts) ===")
    with open(env_path) as f:
        for line in f:
            line = line.rstrip()
            if not line or line.startswith("#"):
                continue
            if "=" in line:
                key, _, val = line.partition("=")
                key = key.strip()
                val = val.strip()
                if key == "GEMINI_API_KEY":
                    print(f"  {key} = {val[:10]}...{val[-4:]} (garums: {len(val)})")
                elif any(s in key for s in ["PASSWORD", "SECRET", "KEY"]):
                    print(f"  {key} = ***")
                else:
                    print(f"  {key} = {val}")

# Pārbauda settings.py
print("\n=== SETTINGS.PY — GEMINI rindas ===")
settings_path = Path(__file__).resolve().parent / "core" / "settings.py"
if settings_path.exists():
    for i, line in enumerate(settings_path.read_text().splitlines(), 1):
        if "GEMINI" in line or "gemini" in line:
            print(f"  {i}: {line}")
else:
    print(f"❌ settings.py nav atrasts: {settings_path}")

# Ielādē Django un pārbauda
print("\n=== DJANGO SETTINGS VĒRTĪBA ===")
sys.path.insert(0, str(Path(__file__).resolve().parent))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")

try:
    import django
    django.setup()
    from django.conf import settings
    val = getattr(settings, "GEMINI_API_KEY", "NAV ATRASTS")
    if val and val != "NAV ATRASTS":
        print(f"✅ Django redz GEMINI_API_KEY: {str(val)[:10]}...{str(val)[-4:]}")
    else:
        print(f"❌ Django NEREDZ GEMINI_API_KEY — vērtība: '{val}'")
except Exception as e:
    print(f"❌ Django kļūda: {e}")
