"""Coverage check: ile tube'ów z theporndude.com mamy już w bazie."""
from app.db import SessionLocal
from sqlalchemy import text


def main():
    with SessionLocal() as s:
        # Wszystkie distinct origins (canonical + tube: + pornapp:)
        rows = s.execute(text("""
            SELECT origin, COUNT(*) AS n,
                   COUNT(*) FILTER (WHERE dead_at IS NULL) AS live,
                   COUNT(*) FILTER (WHERE dead_at IS NOT NULL) AS dead
            FROM playback_sources
            GROUP BY origin
            ORDER BY origin
        """)).all()
    print(f"distinct origins: {len(rows)}")
    by_kind = {}
    for r in rows:
        kind = r.origin.split(":")[0] if ":" in r.origin else "other"
        by_kind.setdefault(kind, []).append((r.origin, r.n, r.live, r.dead))
    for kind, items in by_kind.items():
        print(f"\n=== {kind} ({len(items)} origins) ===")
        for origin, n, live, dead in sorted(items, key=lambda x: -x[2]):
            print(f"  {origin:<35} n={n:>7,} live={live:>7,} dead={dead:>5,}")


if __name__ == "__main__":
    main()
