from datetime import datetime, UTC
import nntplib
from db import get_conn
from dotenv import load_dotenv
import os

load_dotenv()  # načte .env
EWEKA_USER = os.getenv("EWEKA_USER")
EWEKA_PASS = os.getenv("EWEKA_PASS")

PROVIDER = "eweka"

print("🔌 Connecting to PostgreSQL...")
conn = get_conn()

conn.autocommit = True
cur = conn.cursor()

print("🔌 Connecting to Eweka NNTP...")
with nntplib.NNTP_SSL(
    host="news.eweka.nl",
    port=563,
    user=EWEKA_USER,
    password=EWEKA_PASS,
    readermode=True,
) as nntp:

    print("📜 Fetching LIST ACTIVE...")
    resp, groups = nntp.list()
    print(f"📦 Received {len(groups)} groups")

    rows = [
        (
            name,
            int(first),
            int(last),
            flag,
            PROVIDER,
            datetime.now(UTC),
        )
        for name, last, first, flag in groups
    ]

    cur.executemany(
        """
        INSERT INTO newsgroups
            (name, first_article, last_article, posting_flag, provider, fetched_at)
        VALUES (%s, %s, %s, %s, %s, %s)
        ON CONFLICT (name) DO UPDATE SET
            first_article = EXCLUDED.first_article,
            last_article  = EXCLUDED.last_article,
            posting_flag  = EXCLUDED.posting_flag,
            fetched_at    = EXCLUDED.fetched_at
        """,
        rows,
    )

print("🎉 DONE")
