"""Rychlý summary tabulky photo_errors — spusť a zkopíruj výstup."""
import psycopg2
from dotenv import load_dotenv
import os

load_dotenv()

conn = psycopg2.connect(
    host=os.getenv("DB_HOST", "192.168.1.76"),
    port=int(os.getenv("DB_PORT", 5432)),
    user=os.getenv("DB_USER", "vladimir.buzalka"),
    password=os.getenv("DB_PASSWORD", "Vlado7309208104++"),
    dbname=os.getenv("DB_NAME", "fotky_buzalkovi"),
)
cur = conn.cursor()

# Struktura
cur.execute("""
    SELECT column_name, data_type
    FROM information_schema.columns
    WHERE table_name = 'photo_errors'
    ORDER BY ordinal_position
""")
print("=== SLOUPCE ===")
for row in cur.fetchall():
    print(f"  {row[0]:30s} {row[1]}")

# Celkový počet
cur.execute("SELECT COUNT(*) FROM photo_errors")
total = cur.fetchone()[0]
print(f"\n=== CELKEM ZÁZNAMŮ: {total} ===")

# Počty podle error_type (pokud sloupec existuje)
try:
    cur.execute("""
        SELECT error_type, COUNT(*) AS pocet
        FROM photo_errors
        GROUP BY error_type
        ORDER BY pocet DESC
    """)
    rows = cur.fetchall()
    if rows:
        print("\n=== TYPY CHYB ===")
        for r in rows:
            print(f"  {str(r[0]):50s} {r[1]:>8,}")
except Exception as e:
    print(f"\n(error_type sloupec neexistuje nebo chyba: {e})")

# Ukázka prvních 5 řádků
cur.execute("SELECT * FROM photo_errors LIMIT 5")
cols = [d[0] for d in cur.description]
rows = cur.fetchall()
print(f"\n=== UKÁZKA (prvních 5) ===")
print("  " + " | ".join(cols))
for r in rows:
    print("  " + " | ".join(str(v)[:60] for v in r))

# Timeline (pokud je sloupec s datumem)
date_cols = [d[0] for d in cur.description if "dat" in d[0].lower() or "time" in d[0].lower() or "created" in d[0].lower()]
if date_cols:
    dc = date_cols[0]
    cur.execute(f"SELECT MIN({dc}), MAX({dc}) FROM photo_errors")
    mn, mx = cur.fetchone()
    print(f"\n=== ČASOVÝ ROZSAH ({dc}) ===")
    print(f"  Od:  {mn}")
    print(f"  Do:  {mx}")

conn.close()
print("\nHotovo.")
