# Canonical subject normalizer (ISO 8000 consistency + uniqueness)
# Maps abbreviations and variants to one canonical subject name.

SUBJECT_CANON = {
    "BI": "Biology",
    "BIO": "Biology",
    "BIOLOGY": "Biology",
    "CH": "Chemistry",
    "CHEM": "Chemistry",
    "CHEMISTRY": "Chemistry",
    "PH": "Physics",
    "PHY": "Physics",
    "PHYSICS": "Physics",
    "MT": "Mathematics",
    "MA": "Mathematics",
    "MATH": "Mathematics",
    "MATHEMATICS": "Mathematics",
    "EN": "English",
    "ENG": "English",
    "ENGLISH": "English",
    "UR": "Urdu",
    "URDU": "Urdu",
    "IS": "Islamiyat",
    "PS": "Pak Study",
    "ISL": "Islamiyat",
    "ISLAMIYAT": "Islamiyat",
    "CS": "Computer Science",
    "COMPUTER": "Computer Science",
    "COMPUTER SCIENCE": "Computer Science",
    "TQ": "Tarjumatul Quran",
    "TARJUMATUL QURAN": "Tarjumatul Quran",
    "GEN": "General",
}

def canonical_subject(raw: str) -> str:
    """Return one canonical name for any subject variant. Never returns empty."""
    if not raw:
        return "General"
    key = raw.strip().upper()
    return SUBJECT_CANON.get(key, raw.strip())

def unique_canonical_subjects(raw_subjects):
    """Deduplicate a list of raw subjects after canonicalization."""
    return sorted({canonical_subject(s) for s in raw_subjects if s and s.strip()})
