from datetime import datetime
import mysql.connector

from app.db.connection import get_connection
from app.rss.fetcher import fetch_feed


def parse_published_at(entry):
    # feedparser fournit souvent published_parsed / updated_parsed
    t = getattr(entry, "published_parsed", None) or getattr(entry, "updated_parsed", None)
    if not t:
        return None
    return datetime(*t[:6])


def collect():
    conn = get_connection()
    cur = conn.cursor(dictionary=True)

    # 1) Charger les sources actives
    cur.execute("SELECT id, name, rss_url, lang FROM sources WHERE is_active = 1")
    sources = cur.fetchall()

    inserted = 0
    skipped = 0

    for s in sources:
        rss_url = s["rss_url"]
        source_id = s["id"]
        lang = s.get("lang") or "fr"

        try:
            feed = fetch_feed(rss_url)
        except Exception as e:
            print(f"[WARN] RSS KO: {rss_url} -> {e}")
            continue

        for entry in getattr(feed, "entries", []):
            url = getattr(entry, "link", None)
            if not url:
                continue

            guid = getattr(entry, "id", None) or getattr(entry, "guid", None) or None
            title = getattr(entry, "title", "") or ""
            excerpt = getattr(entry, "summary", None)
            published_at = parse_published_at(entry)

            # 2) Insert en évitant les doublons (UNIQUE url/guid)
            try:
                cur.execute(
                    """
                    INSERT INTO articles_raw
                      (source_id, guid, url, title, excerpt, content, published_at, lang)
                    VALUES
                      (%s, %s, %s, %s, %s, %s, %s, %s)
                    """,
                    (source_id, guid, url, title, excerpt, None, published_at, lang),
                )
                inserted += 1
            except mysql.connector.errors.IntegrityError:
                skipped += 1  # doublon
            except Exception as e:
                print(f"[WARN] Insert KO: {url} -> {e}")

        conn.commit()

    cur.close()
    conn.close()

    print(f"✅ Collect terminé — inserted={inserted}, skipped(dup)={skipped}")


if __name__ == "__main__":
    collect()
