from datetime import date

from goodnews.db import connect, init_db
from goodnews.briefs import build_daily_brief


def _setup():
    c = connect(":memory:"); init_db(c)
    c.execute("INSERT INTO sources (id,name,feed_url,trust_score) VALUES (1,'S','http://s/f',5)")
    today = date.today().isoformat()
    for i, t in enumerate(["environment", "animals", "community", "culture", "science", "health", "environment"], 1):
        c.execute("INSERT INTO articles (id,source_id,canonical_url,title,published_at,url_hash) VALUES (?,1,?,?,?,?)",
                  (i, f"http://s/{i}", f"t{i}", today + "T10:00:00+00:00", f"h{i}"))
        c.execute("INSERT INTO article_scores (article_id,constructive_score,agency_score,human_benefit_score,"
                  "cortisol_score,ragebait_score,pr_risk_score,accepted,topic,flavor) VALUES (?,5,2,2,1,0,2,1,?,'solution')",
                  (i, t))
    c.commit()
    return c, today


def _created_at(c, day):
    return c.execute("SELECT created_at FROM daily_briefs WHERE brief_date=?", (day,)).fetchone()[0]


def test_rebuild_with_same_selection_is_noop():
    c, today = _setup()
    build_daily_brief(c, brief_date=today, limit=7, replace=True)
    before = _created_at(c, today)
    build_daily_brief(c, brief_date=today, limit=7, replace=True)  # nothing new
    assert _created_at(c, today) == before  # freshness stamp preserved
    c.close()


def test_new_higher_ranked_article_changes_the_brief():
    c, today = _setup()
    build_daily_brief(c, brief_date=today, limit=7, replace=True)
    c.execute("INSERT INTO articles (id,source_id,canonical_url,title,published_at,url_hash) VALUES (99,1,'http://s/99','t99',?,'h99')",
              (today + "T11:00:00+00:00",))
    c.execute("INSERT INTO article_scores (article_id,constructive_score,agency_score,human_benefit_score,"
              "cortisol_score,ragebait_score,pr_risk_score,accepted,topic,flavor) VALUES (99,9,3,3,0,0,1,1,'animals','solution')")
    c.commit()
    build_daily_brief(c, brief_date=today, limit=7, replace=True)
    ids = [r[0] for r in c.execute("SELECT article_id FROM daily_brief_items bi JOIN daily_briefs b ON b.id=bi.brief_id WHERE b.brief_date=?", (today,))]
    assert 99 in ids
    c.close()
