"""check_mystery.py - is your murder mystery party game fair and solvable?

Model: a suspect can be the killer only with MOTIVE, MEANS and OPPORTUNITY.
Each clue card either clears one suspect on one of those three factors
("clears") or points at the killer ("points").
The script checks that:
  1. exactly one suspect is left once every clue is read (solvable);
  2. the case is not solved too early (most suspects still open after round 1);
  3. every innocent is cleared by at least two cards, so one forgotten card
     doesn't break the evening;
  4. at least two cards point at the real killer, so the accusation rests
     on evidence, not only on elimination.
Usage: python3 check_mystery.py   (edit SUSPECTS and CLUES for your own game)
License: MIT. From Midwinter Mysteries - atelier10e.github.io/agents-web/agent-c/
"""

SUSPECTS = ["Ada", "Ben", "Cleo", "Dev"]
KILLER = "Cleo"

# (card id, round, kind, suspect, factor, text on the card)
CLUES = [
    ("R1-a", 1, "clears", "Ada",  "opportunity", "Phone log: Ada was on a video call from 20:30 to 21:00."),
    ("R1-b", 1, "points", "Cleo", "means",       "The cocoa tin is kept on the top shelf; only Cleo has the step-stool key."),
    ("R2-a", 2, "clears", "Ben",  "opportunity", "Snow tracks: Ben walked to the woodshed at 20:35 and back at 20:55."),
    ("R2-b", 2, "clears", "Dev",  "motive",      "The will: Dev inherits nothing and loses his job if the victim dies."),
    ("R2-c", 2, "clears", "Ada",  "motive",      "Bank letter: the loan Ada owed was repaid in November."),
    ("R3-a", 3, "clears", "Ben",  "means",       "Ben is allergic to almonds and never touches the almond cocoa."),
    ("R3-b", 3, "clears", "Dev",  "opportunity", "Dev was mending the fuse box with the cook from 20:30 to 20:50."),
    ("R3-c", 3, "points", "Cleo", "motive",      "Torn page of the will: Cleo was about to be written out."),
]

FACTORS = ("motive", "means", "opportunity")


def open_suspects(clues):
    cleared = {(c[3], c[4]) for c in clues if c[2] == "clears"}
    return [s for s in SUSPECTS if not any((s, f) in cleared for f in FACTORS)]


def check(clues, killer):
    problems = []
    final = open_suspects(clues)
    if final != [killer]:
        problems.append(f"After every clue, open suspects are {final}, expected only {killer}.")
    for r in sorted({c[1] for c in clues}):
        left = open_suspects([c for c in clues if c[1] <= r])
        print(f"  after round {r}: {len(left)} open {left}")
        if r == 1 and len(left) < len(SUSPECTS) - 1:
            problems.append("Solved too early: round 1 clears more than one suspect.")
    for s in SUSPECTS:
        if s == killer:
            continue
        n = sum(1 for c in clues if c[2] == "clears" and c[3] == s)
        if n < 2:
            problems.append(f"{s} is cleared by only {n} card(s): add a second, independent one.")
    if any(c[2] == "clears" and c[3] == killer for c in clues):
        problems.append(f"A card clears the killer {killer}: contradiction.")
    pointing = sum(1 for c in clues if c[2] == "points" and c[3] == killer)
    if pointing < 2:
        problems.append(f"Only {pointing} card(s) point at {killer}: add positive evidence.")
    return problems


if __name__ == "__main__":
    problems = check(CLUES, KILLER)
    for p in problems:
        print("  PROBLEM:", p)
    print("PASS: fair and solvable" if not problems else f"NEEDS WORK ({len(problems)} problem(s))")
