from __future__ import annotations

import csv
from pathlib import Path

Link = tuple[str, str, str]


def read_links(name: str) -> set[Link]:
    with Path("data", name).open(newline="", encoding="utf-8") as handle:
        return {
            (row["source_id"], row["target_id"], row["link_type"])
            for row in csv.DictReader(handle)
        }


def main() -> None:
    reference = read_links("reference_trace_links.csv")
    candidates = read_links("ai_trace_candidates.csv")
    true_positive = reference & candidates
    false_positive = candidates - reference
    false_negative = reference - candidates
    precision = len(true_positive) / (len(true_positive) + len(false_positive))
    recall = len(true_positive) / (len(true_positive) + len(false_negative))
    print("true_positives =", sorted(true_positive))
    print("false_positives =", sorted(false_positive))
    print("false_negatives =", sorted(false_negative))
    print("precision =", round(precision, 3))
    print("recall =", round(recall, 3))
    assert len(true_positive) == 4
    assert len(false_positive) == 2
    assert len(false_negative) == 1
    assert round(precision, 3) == 0.667
    assert round(recall, 3) == 0.8


if __name__ == "__main__":
    main()
