    # ==================================================
    # DIVISION PERFORMANCE BY POINTS (ExamCandidate ONLY)
    # ==================================================
    division_perf = []

    cand_qs = ExamCandidate.objects.filter(
        year=year,
        exam_type_id=exam_type_id,
        school__district_id=district_id
    )

    total_candidates = cand_qs.count()

    if total_candidates > 0:

        # group by division + points
        rows = (
            cand_qs
            .values("division", "points")
            .annotate(total=Count("id"))
            .order_by("division", "points")
        )

        for r in rows:
            pct = round((r["total"] / total_candidates) * 100, 2)

            division_perf.append({
                "division": r["division"],
                "point": r["points"],
                "total": r["total"],
                "pct": pct,
            })

    # ==================================================
    # DIVISION PERFORMANCE SUMMARY (ExamCandidate ONLY)
    # ==================================================
    division_perf_summary = []

    cand_qs = ExamCandidate.objects.filter(
        year=year,
        exam_type_id=exam_type_id,
        school__district_id=district_id
    )

    total_candidates = cand_qs.count()

    if total_candidates > 0:

        div_rows = (
            cand_qs
            .values("division")
            .annotate(total=Count("id"))
            .order_by("division")
        )

        for r in div_rows:
            division_perf_summary.append({
                "division": r["division"],
                "total": r["total"],
                "pct": round((r["total"] / total_candidates) * 100, 2),
            })

    # ==================================================
    # GENDER & PASS SUMMARIES (ExamCandidate ONLY)
    # ==================================================
    division_gender_summary = []
    pass_gender_summary = {}
    pass_division_summary = []

    cand_qs = ExamCandidate.objects.filter(
        year=year,
        exam_type_id=exam_type_id,
        school__district_id=district_id
    )

    total_candidates = cand_qs.count()

    if total_candidates > 0:

        # --------------------------------------------------
        # 1) DIVISION x GENDER SUMMARY (I–0)
        # --------------------------------------------------
        div_gender_rows = (
            cand_qs
            .values("division", "sex")
            .annotate(total=Count("id"))
            .order_by("division", "sex")
        )

        # Normalize into a predictable structure for charts
        # Example item:
        # { division:'I', f:544, m:751, total:1295 }
        by_div = {}
        for r in div_gender_rows:
            d = r["division"]
            by_div.setdefault(d, {"division": d, "f": 0, "m": 0, "total": 0})
            if (r.get("sex") or "").upper() == "F":
                by_div[d]["f"] += r["total"]
            elif (r.get("sex") or "").upper() == "M":
                by_div[d]["m"] += r["total"]
            by_div[d]["total"] += r["total"]

        # keep natural division order I, II, III, IV, 0
        ORDER = ["I", "II", "III", "IV", "0"]
        for d in ORDER:
            if d in by_div:
                division_gender_summary.append(by_div[d])

        # --------------------------------------------------
        # 2) PASS PERFORMANCE BY GENDER (I–IV only)
        # --------------------------------------------------
        pass_qs = cand_qs.filter(division__in=["I", "II", "III", "IV"])
        pass_total = pass_qs.count()

        if pass_total > 0:
            pf = pass_qs.filter(sex__iexact="F").count()
            pm = pass_qs.filter(sex__iexact="M").count()

            pass_gender_summary = {
                "F": {
                    "total": pf,
                    "pct": round((pf / pass_total) * 100, 2),
                },
                "M": {
                    "total": pm,
                    "pct": round((pm / pass_total) * 100, 2),
                },
                "total": pass_total,
            }

        # --------------------------------------------------
        # 3) PASS BY DIVISION (I–IV only)
        # --------------------------------------------------
        pass_div_rows = (
            pass_qs
            .values("division")
            .annotate(total=Count("id"))
            .order_by("division")
        )

        for r in pass_div_rows:
            pass_division_summary.append({
                "division": r["division"],
                "total": r["total"],
                "pct": round((r["total"] / pass_total) * 100, 2) if pass_total else 0,
            })

    return {
        "region": region,
        "district": district,
        "exam_type": exam_type_id,
        "year": year,

        "totals": {
            "reg_f": reg_f,
            "reg_m": reg_m,
            "reg": reg,

            "sat_f": sat_f,
            "sat_m": sat_m,
            "sat": sat_a,

            "abs_f": abs_f,
            "abs_m": abs_m,
            "abs": abs_total,

            "avg": final_gpa,
            "grade": final_grade,
            "status": final_status,
        },

        "grades": grades,
        "subjects": subject_rows,

        "a_c_f": a_c_f,
        "a_c_m": a_c_m,
        "a_c_total": a_c_total,

        "a_d_f": a_d_f,
        "a_d_m": a_d_m,
        "a_d_total": a_d_total,

        # 🔁 DIFFERENCE FROM REGION SUMMARY
        "ward_perf": ward_rows,
        "best_schools": best_schools,

        "years": years,
        "exam_types": exam_types,
        "division_performance": division_perf,
        "division_perf_summary": division_perf_summary,
        "division_gender_summary": division_gender_summary,
        "pass_gender_summary": pass_gender_summary,
        "pass_division_summary": pass_division_summary,
    }