/**
 * SecondarySchoolSections — the section sequence for secondary detail pages.
 * Server component.
 *
 * Wrapped in `.secondaryScope`, which activates the secondary-only style
 * overrides in schoolSections.module.css. Those rules target class names the
 * primary page also uses (.card, .sectionTitle, .metricCard …), so scoping is
 * what keeps them from restyling primary pages.
 *
 * The render conditions here MUST match buildSecondaryNavItems in
 * lib/schoolSections, or the sticky nav will link to sections that do not exist.
 */

import type {
  School, SchoolResult, AbsenceData, OfstedInspection, SchoolCensus,
  SchoolAdmissions, SchoolDeprivation, SchoolFinance, NationalAverages,
} from '@/lib/types';
import { ofstedLegacyAreas } from '@/lib/utils';
import type { SecondaryFlags } from '@/lib/schoolSections';
import { OfstedSection } from './OfstedSection';
import { GcseSection } from './GcseSection';
import { SecondaryAdmissionsSection } from './SecondaryAdmissionsSection';
import { SecondaryHistorySection } from './SecondaryHistorySection';
import { WellbeingSection } from './WellbeingSection';
import { FinancesSection } from './FinancesSection';
import styles from './schoolSections.module.css';

export interface SecondarySchoolSectionsProps {
  schoolInfo: School;
  yearlyData: SchoolResult[];
  absenceData: AbsenceData | null;
  ofsted: OfstedInspection | null;
  census: SchoolCensus | null;
  admissions: SchoolAdmissions | null;
  deprivation: SchoolDeprivation | null;
  finance: SchoolFinance | null;
  nationalAvg: NationalAverages | null;
  flags: SecondaryFlags;
}

export function SecondarySchoolSections({
  schoolInfo, yearlyData, ofsted, census,
  admissions, deprivation, finance, nationalAvg, flags,
}: SecondarySchoolSectionsProps) {
  const secondaryAvg = nationalAvg?.secondary ?? {};

  const isReportCard = !!(ofsted?.report_card && Object.keys(ofsted.report_card).length > 0);
  const ofstedInspectedDate = isReportCard
    ? ofsted?.rc_inspection_date ?? null
    : ofsted?.inspection_date ?? null;
  const oeifAreas = ofsted ? ofstedLegacyAreas(ofsted) : [];
  const oeifAllSameGrade =
    !!ofsted &&
    !isReportCard &&
    oeifAreas.length >= 3 &&
    oeifAreas.every((a) => a.value === ofsted.overall_effectiveness);

  return (
    <div className={styles.secondaryScope}>
      {ofsted && (
        <OfstedSection
          ofsted={ofsted}
          urn={schoolInfo.urn}
          isReportCard={isReportCard}
          ofstedInspectedDate={ofstedInspectedDate}
          oeifAllSameGrade={oeifAllSameGrade}
          oeifAreas={oeifAreas}
          variant="secondary"
        />
      )}

      {flags.hasResults && flags.latestResults && (
        <GcseSection
          latestResults={flags.latestResults}
          schoolInfo={schoolInfo}
          secondaryAvg={secondaryAvg}
          p8Suspended={flags.p8Suspended}
          suppressComparison={flags.suppressComparison}
        />
      )}

      {admissions && (
        <SecondaryAdmissionsSection
          admissions={admissions}
          schoolInfo={schoolInfo}
          hasSixthForm={flags.hasSixthForm}
        />
      )}

      {yearlyData.length > 1 && (
        <SecondaryHistorySection
          yearlyData={yearlyData}
          schoolInfo={schoolInfo}
          nationalAvg={nationalAvg}
          secondaryAvg={secondaryAvg}
          suppressComparison={flags.suppressComparison}
        />
      )}

      {flags.hasWellbeing && (
        <WellbeingSection
          latestResults={flags.latestResults}
          census={census}
          schoolInfo={schoolInfo}
          deprivation={deprivation}
          hasDeprivation={flags.hasDeprivation}
        />
      )}

      {flags.hasFinance && finance && (
        <FinancesSection finance={finance} showPremises />
      )}
    </div>
  );
}
