"""
Practice: the pieces of a RAG answer you can check (Lab 04, 01_basic_rag.py).

Fill in the TODOs, then run the tests:
    pytest test_rag_practice.py        (locally)
or press "Run tests" on the chapter page.
"""
import re

IDK = "I don't have enough information"


def build_prompt(query: str, chunks: list[dict]) -> str:
    """Build the user message the way the lab does.

    Each chunk becomes "[<source>]\\n<text>"; join the blocks with "\\n\\n---\\n\\n"; then return
    "Context:\\n\\n<blocks>\\n\\nQuestion: <query>".

    TODO 1
    """
    return f"Question: {query}"


def cited(answer: str) -> list[str]:
    """The sources an answer cites, written as [name] tags (lowercase letters, digits, _ or -),
    in the order they first appear, without repeats.

    TODO 2
    """
    return []


def citations_check(answer: str, sent: list[dict]) -> dict:
    """{"cites": does the answer cite anything?,
        "not_sent": cited sources that weren't among the chunks sent (each chunk has "source")}

    TODO 3
    """
    return {"cites": True, "not_sent": []}


def grade(answer: str, rule: dict) -> str:
    """Label an answer against a written rule for its question. Check in this order:

    "idk"      the answer starts with IDK (the "not enough information" sentence)
    "wrong"    it starts with any phrase in rule["wrong_if_starts"], or rule["needs_any"] is
               given and the answer contains none of its phrases (ignore case)
    "partial"  rule["complete_if_any"] is given and the answer contains none of those (ignore case)
    "right"    otherwise

    Missing keys mean "no such rule".

    TODO 4
    """
    return "right"


def tally(labels: list[str]) -> dict:
    """Count labels: {"right": n, "partial": n, "wrong": n, "idk": n}.

    TODO 5
    """
    return {"right": 0, "partial": 0, "wrong": 0, "idk": 0}
