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

import sys, os
from pathlib import Path

base = Path("/home2/melgalis/agent_backend")
sys.path.insert(0, str(base))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")

# Ielādē .env
env = base / ".env"
if env.exists():
    for line in env.read_text().splitlines():
        line = line.strip()
        if line and not line.startswith("#") and "=" in line:
            k, _, v = line.partition("=")
            os.environ.setdefault(k.strip(), v.strip())

import django
django.setup()

print("=" * 55)
print("1. MIGRATIONS STATUSS")
print("=" * 55)
from django.db import connection
cursor = connection.cursor()
cursor.execute("SHOW TABLES LIKE '%pieteikum%'")
tables = cursor.fetchall()
print(f"Tabulas DB: {tables}")

print("\n" + "=" * 55)
print("2. VIEWS.PY IMPORTS TESTS")
print("=" * 55)
try:
    from pieteikumi import views
    print("✅ views.py importēts veiksmīgi")
except Exception as e:
    print(f"❌ views.py kļūda: {e}")

print("\n" + "=" * 55)
print("3. URLS TESTS")
print("=" * 55)
try:
    from pieteikumi import urls
    print("✅ urls.py importēts veiksmīgi")
    print(f"   URL patterns: {urls.urlpatterns}")
except Exception as e:
    print(f"❌ urls.py kļūda: {e}")

print("\n" + "=" * 55)
print("4. MODELS TESTS")
print("=" * 55)
try:
    from pieteikumi.models import Pieteikums
    count = Pieteikums.objects.count()
    print(f"✅ Model OK — {count} ieraksti DB")
except Exception as e:
    print(f"❌ Model kļūda: {e}")

print("\n" + "=" * 55)
print("5. VIEWS.PY SATURS")
print("=" * 55)
views_file = base / "pieteikumi" / "views.py"
if views_file.exists():
    print(views_file.read_text())
else:
    print("❌ views.py nav atrasts")
