"""Tests from the RAG video's recorded runs: gpt-5.4-nano with Lab 04's RAG prompt over the made-up
help center. Run: pytest test_rag_practice.py"""
from rag_practice import build_prompt, cited, citations_check, grade, tally

BILLING = {"source": "billing", "text": "We accept Visa, Mastercard, American Express and PayPal. Cards are charged on the same day each month, or each year for annual plans. We do not currently accept cryptocurrency or bank transfers. To change the card on file, go to Account Settings, then Billing, then Payment method."}
TEAMS = {"source": "teams", "text": "A Pro workspace can have up to 10 members."}

# Written rules for three questions, from what the help center says
RULES = {
    "card": {"wrong_if_starts": ["No"]},                      # PayPal is a way to pay without a card
    "team14": {"needs_any": ["Enterprise"]},                  # Pro allows 10 members
    "quitdata": {"complete_if_any": ["30"]},                  # data is deleted 30 days after the plan ends
}

CARD_1 = "No. We accept Visa, Mastercard, American Express, and PayPal, and we do not currently accept cryptocurrency or bank transfers. [billing]"
CARD_3 = "I don't have enough information to answer that based on the available documents."
CARD_6 = "You can pay with **PayPal** (in addition to Visa, Mastercard, and American Express), but **we do not currently accept cryptocurrency or bank transfers**. [billing]"
TEAM_1 = "For 14 people, you need the **Pro** plan (since Free is for **one user** and has a **limit of up to three projects**, while Pro removes the project limit). **Pro costs $12 per user per month** (or **$120 per user per year**). [billing]"
TEAM_3 = "For 14 people, you should use the **Enterprise** plan, because the **Pro workspace supports up to 10 members**. [teams]\n\n**Cost:** Enterprise is **priced per contract** (the exact price isn’t provided in the documents). [billing]"
QUIT_1 = "After you cancel, your paid features remain active until the end of the period you already paid for, and on that date your workspace moves to the Free plan. Projects beyond the Free limit become read-only: you can open and export them, but not edit them. So to keep a copy, export your projects before (or at) the time they become read-only. [cancelling]"
QUIT_3 = "After you cancel, your paid features remain active until the end of the period you already paid for, and your data is kept for 30 days after your plan ends. To keep a copy of your projects, export everything before the 30-day period ends—download an export from **Account Settings → Data** before that date. [cancelling]"


def test_prompt_matches_the_lab():
    p = build_prompt("Can I pay without a credit card?", [BILLING, TEAMS])
    assert p.startswith("Context:\n\n[billing]\nWe accept Visa")
    assert "\n\n---\n\n[teams]\nA Pro workspace" in p
    assert p.endswith("\n\nQuestion: Can I pay without a credit card?")


def test_every_answer_names_its_source():
    assert cited(TEAM_3) == ["teams", "billing"]
    assert cited(CARD_3) == []
    assert citations_check(CARD_1, [BILLING]) == {"cites": True, "not_sent": []}
    # a citation to a page that wasn't sent is a red flag on its own
    assert citations_check(TEAM_3, [BILLING]) == {"cites": True, "not_sent": ["teams"]}


def test_right_page_cited_still_wrong():
    assert grade(CARD_1, RULES["card"]) == "wrong"      # cites billing, which lists PayPal
    assert grade(CARD_3, RULES["card"]) == "idk"        # PayPal was in the paragraphs sent
    assert grade(CARD_6, RULES["card"]) == "right"


def test_missing_paragraph_shows_up_as_wrong_or_partial():
    assert grade(TEAM_1, RULES["team14"]) == "wrong"    # 1 paragraph: the 10-member limit wasn't sent
    assert grade(TEAM_3, RULES["team14"]) == "right"
    assert grade(QUIT_1, RULES["quitdata"]) == "partial"  # no 30-day deadline
    assert grade(QUIT_3, RULES["quitdata"]) == "right"


def test_tally_the_card_question():
    # "Can I pay without a credit card?" with 1, 3 and 6 paragraphs, 3 runs each
    labels = ["wrong"] * 3 + ["idk"] * 3 + ["idk", "right", "idk"]
    assert tally(labels) == {"right": 1, "partial": 0, "wrong": 3, "idk": 5}
