import { useEffect, useMemo, useState, type FormEvent } from 'react'
import { Link, useSearchParams } from 'react-router-dom'
import { CiFlashMessages, type CiFlashMessage } from '../../components/CiPartials'
import { ApiHttpError } from '../../api/http'
import {
  closeSchoolYear,
  createSchoolYear,
  fetchSchoolYearParentBalances,
  fetchSchoolYearPromotionPreview,
  fetchSchoolYearSummary,
  fetchSchoolYears,
  previewSchoolYearClose,
  reopenSchoolYear,
  updateSchoolYear,
  validateSchoolYearClose,
  type CloseSchoolYearPayload,
  type SchoolYearClosePreview,
  type SchoolYearCloseValidation,
  type SchoolYearParentBalancePreview,
  type SchoolYearPromotionPreview,
  type SchoolYearRecord,
  type SchoolYearSummary,
} from '../../api/schoolYears'

type YearFormState = {
  name: string
  start_date: string
  end_date: string
}

type CloseFormState = {
  name: string
  start_date: string
  end_date: string
  confirmation: string
  transfer_unpaid_balances: boolean
}

function emptyYearForm(): YearFormState {
  return { name: '', start_date: '', end_date: '' }
}

function emptyCloseForm(): CloseFormState {
  return {
    name: '',
    start_date: '',
    end_date: '',
    confirmation: '',
    transfer_unpaid_balances: true,
  }
}

function normalizeApiError(error: unknown, fallback: string): string {
  if (error instanceof ApiHttpError) return error.message || fallback
  if (error instanceof Error) return error.message
  return fallback
}

function toDisplayDate(value: string | null | undefined): string {
  if (!value) return '—'
  const iso = value.length > 10 ? value.slice(0, 10) : value
  const parsed = new Date(`${iso}T00:00:00`)
  if (Number.isNaN(parsed.getTime())) return value
  return parsed.toLocaleDateString(undefined, {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  })
}

function formatMoney(value: number | null | undefined): string {
  return Number(value ?? 0).toLocaleString(undefined, {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  })
}

function statusBadge(status: string): string {
  switch (status) {
    case 'active':
      return 'success'
    case 'closed':
      return 'danger'
    case 'draft':
      return 'warning'
    default:
      return 'secondary'
  }
}

function suggestNextName(name: string): string {
  const fullRange = name.match(/^(\d{4})\s*-\s*(\d{4})$/)
  if (fullRange) {
    return `${Number(fullRange[1]) + 1}-${Number(fullRange[2]) + 1}`
  }

  const shortRange = name.match(/^(\d{4})\s*-\s*(\d{2})$/)
  if (shortRange) {
    const nextStart = Number(shortRange[1]) + 1
    const nextEnd = (Number(shortRange[2]) + 1).toString().padStart(2, '0')
    return `${nextStart}-${nextEnd}`
  }

  return ''
}

function shiftSchoolYearDate(value: string): string {
  const parts = value.split('-')
  if (parts.length !== 3) return ''
  return `${Number(parts[0]) + 1}-${parts[1]}-${parts[2]}`
}

function defaultDraftFor(activeYear: SchoolYearRecord | null): YearFormState {
  if (!activeYear) return emptyYearForm()
  return {
    name: suggestNextName(activeYear.name),
    start_date: shiftSchoolYearDate(activeYear.start_date),
    end_date: shiftSchoolYearDate(activeYear.end_date),
  }
}

function closePayloadFrom(state: CloseFormState): CloseSchoolYearPayload {
  return {
    new_school_year: {
      name: state.name.trim(),
      start_date: state.start_date,
      end_date: state.end_date,
    },
    transfer_unpaid_balances: state.transfer_unpaid_balances,
    confirmation: state.confirmation.trim(),
  }
}

export function SchoolYearsManagementPage() {
  const [searchParams, setSearchParams] = useSearchParams()
  const [years, setYears] = useState<SchoolYearRecord[]>([])
  const [loading, setLoading] = useState(true)
  const [loadingMessage, setLoadingMessage] = useState('Loading school years…')
  const [error, setError] = useState<string | null>(null)
  const [messages, setMessages] = useState<CiFlashMessage[]>([])
  const [summary, setSummary] = useState<SchoolYearSummary | null>(null)
  const [promotionPreview, setPromotionPreview] = useState<SchoolYearPromotionPreview | null>(null)
  const [parentBalances, setParentBalances] = useState<SchoolYearParentBalancePreview | null>(null)
  const [closeValidation, setCloseValidation] = useState<SchoolYearCloseValidation | null>(null)
  const [closePreview, setClosePreview] = useState<SchoolYearClosePreview | null>(null)
  const [createForm, setCreateForm] = useState<YearFormState>(emptyYearForm())
  const [editForm, setEditForm] = useState<YearFormState>(emptyYearForm())
  const [closeForm, setCloseForm] = useState<CloseFormState>(emptyCloseForm())
  const [busyCreate, setBusyCreate] = useState(false)
  const [busyEdit, setBusyEdit] = useState(false)
  const [busyOpenId, setBusyOpenId] = useState<number | null>(null)
  const [busyCloseAction, setBusyCloseAction] = useState<'validate' | 'preview' | 'close' | null>(null)
  const selectedYearId = Number(searchParams.get('year') ?? 0) || null

  const activeYear = useMemo(
    () => years.find((year) => Boolean(year.is_current)) ?? null,
    [years],
  )

  const selectedYear = useMemo(() => {
    if (years.length === 0) return null
    if (selectedYearId != null) {
      return years.find((year) => year.id === selectedYearId) ?? null
    }
    return activeYear ?? years[0] ?? null
  }, [activeYear, selectedYearId, years])

  async function refreshYears(preferredYearId?: number | null) {
    setLoading(true)
    setLoadingMessage('Loading school years…')
    setError(null)

    try {
      const list = await fetchSchoolYears()
      setYears(list)

      const nextSelectedId =
        preferredYearId ??
        (selectedYearId != null && list.some((year) => year.id === selectedYearId)
          ? selectedYearId
          : list.find((year) => year.is_current)?.id ?? list[0]?.id ?? null)

      if (nextSelectedId != null) {
        setSearchParams((current) => {
          const next = new URLSearchParams(current)
          next.set('year', String(nextSelectedId))
          return next
        }, { replace: true })
      }

      const nextActive = list.find((year) => year.is_current) ?? null
      const draftDefaults = defaultDraftFor(nextActive)
      setCreateForm((current) =>
        current.name || current.start_date || current.end_date ? current : draftDefaults,
      )

      if (nextActive) {
        setCloseForm((current) =>
          current.name || current.start_date || current.end_date
            ? current
            : {
                ...current,
                ...draftDefaults,
              },
        )
      } else {
        setCloseForm(emptyCloseForm())
      }
    } catch (loadError) {
      setYears([])
      setSummary(null)
      setPromotionPreview(null)
      setParentBalances(null)
      setCloseValidation(null)
      setClosePreview(null)
      setError(normalizeApiError(loadError, 'Unable to load school years.'))
    } finally {
      setLoading(false)
    }
  }

  useEffect(() => {
    void refreshYears()
    // search params are intentionally handled by explicit selection logic
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  useEffect(() => {
    if (!selectedYear) {
      setSummary(null)
      setEditForm(emptyYearForm())
      setPromotionPreview(null)
      setParentBalances(null)
      return
    }

    setEditForm({
      name: selectedYear.name,
      start_date: selectedYear.start_date,
      end_date: selectedYear.end_date,
    })

    let cancelled = false
    setSummary(null)
    setLoadingMessage(`Loading ${selectedYear.name} summary…`)

    ;(async () => {
      try {
        const nextSummary = await fetchSchoolYearSummary(selectedYear.id)
        if (!cancelled) {
          setSummary(nextSummary)
        }
      } catch (loadError) {
        if (!cancelled) {
          setError(normalizeApiError(loadError, 'Unable to load school year summary.'))
        }
      }
    })()

    return () => {
      cancelled = true
    }
  }, [selectedYear])

  useEffect(() => {
    const defaults = defaultDraftFor(activeYear)
    if (!activeYear) {
      setCloseForm(emptyCloseForm())
      return
    }

    setCloseForm((current) => ({
      ...current,
      name: current.name || defaults.name,
      start_date: current.start_date || defaults.start_date,
      end_date: current.end_date || defaults.end_date,
    }))
  }, [activeYear])

  function pushMessage(type: CiFlashMessage['type'], message: string) {
    setMessages([{ type, message }])
  }

  function selectYear(yearId: number) {
    setSearchParams((current) => {
      const next = new URLSearchParams(current)
      next.set('year', String(yearId))
      return next
    })
    setPromotionPreview(null)
    setParentBalances(null)
  }

  async function handleCreate(event: FormEvent) {
    event.preventDefault()
    setBusyCreate(true)
    setError(null)
    try {
      const created = await createSchoolYear(createForm)
      pushMessage('success', `Draft school year ${created.name} created.`)
      setCreateForm(emptyYearForm())
      await refreshYears(created.id)
    } catch (saveError) {
      setError(normalizeApiError(saveError, 'Unable to create school year.'))
    } finally {
      setBusyCreate(false)
    }
  }

  async function handleEdit(event: FormEvent) {
    event.preventDefault()
    if (!selectedYear) return
    setBusyEdit(true)
    setError(null)
    try {
      const updated = await updateSchoolYear(selectedYear.id, editForm)
      pushMessage('success', `School year ${updated.name} updated.`)
      await refreshYears(updated.id)
    } catch (saveError) {
      setError(normalizeApiError(saveError, 'Unable to update school year.'))
    } finally {
      setBusyEdit(false)
    }
  }

  async function handleReopen(year: SchoolYearRecord) {
    setBusyOpenId(year.id)
    setError(null)
    try {
      const reopened = await reopenSchoolYear(year.id)
      pushMessage('success', `School year ${reopened.name} is now active.`)
      setCloseValidation(null)
      setClosePreview(null)
      await refreshYears(reopened.id)
    } catch (actionError) {
      setError(normalizeApiError(actionError, 'Unable to open school year.'))
    } finally {
      setBusyOpenId(null)
    }
  }

  async function loadPromotionPreview() {
    if (!selectedYear) return
    setLoadingMessage(`Loading ${selectedYear.name} promotion preview…`)
    setError(null)
    try {
      const preview = await fetchSchoolYearPromotionPreview(
        selectedYear.id,
        suggestNextName(selectedYear.name),
      )
      setPromotionPreview(preview)
    } catch (loadError) {
      setError(normalizeApiError(loadError, 'Unable to load promotion preview.'))
    }
  }

  async function loadParentBalances() {
    if (!selectedYear) return
    setLoadingMessage(`Loading ${selectedYear.name} parent balances…`)
    setError(null)
    try {
      const balances = await fetchSchoolYearParentBalances(
        selectedYear.id,
        suggestNextName(selectedYear.name),
      )
      setParentBalances(balances)
    } catch (loadError) {
      setError(normalizeApiError(loadError, 'Unable to load parent balance preview.'))
    }
  }

  async function handleValidateClose() {
    if (!activeYear) return
    setBusyCloseAction('validate')
    setError(null)
    try {
      const validation = await validateSchoolYearClose(activeYear.id, closePayloadFrom(closeForm))
      setCloseValidation(validation)
      setClosePreview(null)
    } catch (actionError) {
      setError(normalizeApiError(actionError, 'Unable to validate school year closure.'))
    } finally {
      setBusyCloseAction(null)
    }
  }

  async function handlePreviewClose() {
    if (!activeYear) return
    setBusyCloseAction('preview')
    setError(null)
    try {
      const preview = await previewSchoolYearClose(activeYear.id, closePayloadFrom(closeForm))
      setCloseValidation(preview.validation)
      setClosePreview(preview)
    } catch (actionError) {
      setError(normalizeApiError(actionError, 'Unable to preview school year closure.'))
    } finally {
      setBusyCloseAction(null)
    }
  }

  async function handleClose(event: FormEvent) {
    event.preventDefault()
    if (!activeYear) return
    setBusyCloseAction('close')
    setError(null)
    try {
      const result = await closeSchoolYear(activeYear.id, closePayloadFrom(closeForm))
      pushMessage(
        'success',
        `Closed ${result.closed_school_year.name} and opened ${result.new_school_year.name}.`,
      )
      setCloseValidation(null)
      setClosePreview(null)
      setPromotionPreview(null)
      setParentBalances(null)
      setCloseForm(emptyCloseForm())
      await refreshYears(result.new_school_year.id)
    } catch (actionError) {
      setError(normalizeApiError(actionError, 'Unable to close school year.'))
    } finally {
      setBusyCloseAction(null)
    }
  }

  const summaryCards = summary?.totals
  const promotionRows = promotionPreview?.rows ?? []
  const balanceRows = parentBalances?.rows ?? []
  const closePromotionRows = closePreview?.promotion_preview.rows ?? []
  const closeBalanceRows = closePreview?.parent_balances.rows ?? []

  return (
    <div className="container-fluid py-4">
      <nav aria-label="breadcrumb">
        <ol className="breadcrumb">
          <li className="breadcrumb-item">
            <Link to="/app/home">Home</Link>
          </li>
          <li className="breadcrumb-item active" aria-current="page">
            administrator
          </li>
          <li className="breadcrumb-item active" aria-current="page">
            school-years
          </li>
        </ol>
      </nav>

      <div className="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
        <div>
          <h1 className="h3 mb-1">School Years Management</h1>
          <p className="text-muted mb-0">
            Control draft years, metadata updates, reopening, validation, promotion preview,
            parent balance carryover, and year closure through the matching Laravel API routes.
          </p>
        </div>
        <div className="text-muted small">
          Uses <code>/api/v1/school-years</code> with JWT <code>Authorization: Bearer</code>.
        </div>
      </div>

      <CiFlashMessages messages={messages} />
      {error ? <div className="alert alert-danger">{error}</div> : null}

      <div className="row g-4">
        <div className="col-xl-4">
          <div className="card shadow-sm mb-4">
            <div className="card-body">
              <h2 className="h5 mb-3">School Years</h2>
              {loading && years.length === 0 ? (
                <p className="text-muted mb-0">{loadingMessage}</p>
              ) : years.length === 0 ? (
                <p className="text-muted mb-0">No school years found.</p>
              ) : (
                <div className="d-grid gap-3">
                  {years.map((year) => (
                    <div
                      key={year.id}
                      className={`border rounded p-3 ${selectedYear?.id === year.id ? 'border-primary bg-light' : 'bg-white'}`}
                    >
                      <div className="d-flex justify-content-between gap-2 align-items-start mb-2">
                        <div>
                          <button
                            type="button"
                            className="btn btn-link p-0 fw-semibold text-decoration-none"
                            onClick={() => selectYear(year.id)}
                          >
                            {year.name}
                          </button>
                          <div className="small text-muted">
                            {toDisplayDate(year.start_date)} to {toDisplayDate(year.end_date)}
                          </div>
                        </div>
                        <span className={`badge text-bg-${statusBadge(year.status)}`}>
                          {year.status.toUpperCase()}
                        </span>
                      </div>
                      <div className="d-flex flex-wrap gap-2">
                        <button
                          type="button"
                          className="btn btn-sm btn-outline-primary"
                          onClick={() => selectYear(year.id)}
                        >
                          Inspect
                        </button>
                        {!year.is_current ? (
                          <button
                            type="button"
                            className="btn btn-sm btn-primary"
                            disabled={busyOpenId === year.id}
                            onClick={() => void handleReopen(year)}
                          >
                            {busyOpenId === year.id
                              ? 'Opening…'
                              : year.status === 'draft'
                                ? 'Open draft'
                                : 'Reopen'}
                          </button>
                        ) : (
                          <span className="badge text-bg-success align-self-center">Current</span>
                        )}
                      </div>
                    </div>
                  ))}
                </div>
              )}
            </div>
          </div>

          <div className="card shadow-sm">
            <div className="card-body">
              <div className="d-flex justify-content-between align-items-center mb-3">
                <h2 className="h5 mb-0">Create Draft Year</h2>
                <button
                  type="button"
                  className="btn btn-sm btn-outline-secondary"
                  onClick={() => setCreateForm(defaultDraftFor(activeYear))}
                >
                  Suggest next
                </button>
              </div>
              <form onSubmit={(event) => void handleCreate(event)}>
                <div className="mb-3">
                  <label className="form-label" htmlFor="create_school_year_name">
                    Name
                  </label>
                  <input
                    id="create_school_year_name"
                    className="form-control"
                    value={createForm.name}
                    onChange={(event) =>
                      setCreateForm((current) => ({ ...current, name: event.target.value }))
                    }
                    required
                  />
                </div>
                <div className="row g-3">
                  <div className="col-md-6">
                    <label className="form-label" htmlFor="create_school_year_start">
                      Start date
                    </label>
                    <input
                      id="create_school_year_start"
                      className="form-control"
                      type="date"
                      value={createForm.start_date}
                      onChange={(event) =>
                        setCreateForm((current) => ({ ...current, start_date: event.target.value }))
                      }
                      required
                    />
                  </div>
                  <div className="col-md-6">
                    <label className="form-label" htmlFor="create_school_year_end">
                      End date
                    </label>
                    <input
                      id="create_school_year_end"
                      className="form-control"
                      type="date"
                      value={createForm.end_date}
                      onChange={(event) =>
                        setCreateForm((current) => ({ ...current, end_date: event.target.value }))
                      }
                      required
                    />
                  </div>
                </div>
                <button type="submit" className="btn btn-success mt-3" disabled={busyCreate}>
                  {busyCreate ? 'Creating…' : 'Create draft year'}
                </button>
              </form>
            </div>
          </div>
        </div>

        <div className="col-xl-8">
          <div className="card shadow-sm mb-4">
            <div className="card-body">
              <div className="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-3">
                <div>
                  <h2 className="h5 mb-1">{selectedYear?.name ?? 'Year summary'}</h2>
                  <div className="text-muted small">
                    Review the selected year before updating metadata, reopening, or closing.
                  </div>
                </div>
                {selectedYear ? (
                  <span className={`badge text-bg-${statusBadge(selectedYear.status)}`}>
                    {selectedYear.status.toUpperCase()}
                  </span>
                ) : null}
              </div>

              {!selectedYear ? (
                <p className="text-muted mb-0">Select a school year to inspect it.</p>
              ) : !summaryCards ? (
                <p className="text-muted mb-0">Loading summary…</p>
              ) : (
                <div className="row g-3">
                  <div className="col-md-6 col-xl-3">
                    <div className="border rounded p-3 h-100">
                      <div className="small text-muted text-uppercase">Students considered</div>
                      <div className="display-6">{summaryCards.students_considered}</div>
                    </div>
                  </div>
                  <div className="col-md-6 col-xl-3">
                    <div className="border rounded p-3 h-100">
                      <div className="small text-muted text-uppercase">Students blocked</div>
                      <div className="display-6">{summaryCards.students_blocked}</div>
                    </div>
                  </div>
                  <div className="col-md-6 col-xl-3">
                    <div className="border rounded p-3 h-100">
                      <div className="small text-muted text-uppercase">Parents with balances</div>
                      <div className="display-6">{summaryCards.parents_with_balances}</div>
                    </div>
                  </div>
                  <div className="col-md-6 col-xl-3">
                    <div className="border rounded p-3 h-100">
                      <div className="small text-muted text-uppercase">Net balance impact</div>
                      <div className="display-6">${formatMoney(summaryCards.net_balance_to_transfer)}</div>
                    </div>
                  </div>
                </div>
              )}
            </div>
          </div>

          <div className="card shadow-sm mb-4">
            <div className="card-body">
              <h2 className="h5 mb-3">Edit Selected Year</h2>
              {!selectedYear ? (
                <p className="text-muted mb-0">Select a school year to edit its metadata.</p>
              ) : (
                <form onSubmit={(event) => void handleEdit(event)}>
                  <div className="mb-3">
                    <label className="form-label" htmlFor="edit_school_year_name">
                      Name
                    </label>
                    <input
                      id="edit_school_year_name"
                      className="form-control"
                      value={editForm.name}
                      onChange={(event) =>
                        setEditForm((current) => ({ ...current, name: event.target.value }))
                      }
                      required
                    />
                  </div>
                  <div className="row g-3">
                    <div className="col-md-6">
                      <label className="form-label" htmlFor="edit_school_year_start">
                        Start date
                      </label>
                      <input
                        id="edit_school_year_start"
                        className="form-control"
                        type="date"
                        value={editForm.start_date}
                        onChange={(event) =>
                          setEditForm((current) => ({ ...current, start_date: event.target.value }))
                        }
                        required
                      />
                    </div>
                    <div className="col-md-6">
                      <label className="form-label" htmlFor="edit_school_year_end">
                        End date
                      </label>
                      <input
                        id="edit_school_year_end"
                        className="form-control"
                        type="date"
                        value={editForm.end_date}
                        onChange={(event) =>
                          setEditForm((current) => ({ ...current, end_date: event.target.value }))
                        }
                        required
                      />
                    </div>
                  </div>
                  <div className="form-text mt-2">
                    Safe renames depend on whether finance and enrollment records already reference
                    the existing year label.
                  </div>
                  <button type="submit" className="btn btn-primary mt-3" disabled={busyEdit}>
                    {busyEdit ? 'Saving…' : 'Save changes'}
                  </button>
                </form>
              )}
            </div>
          </div>

          <div className="card shadow-sm mb-4">
            <div className="card-body">
              <div className="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-3">
                <div>
                  <h2 className="h5 mb-1">Promotion And Balance Inspection</h2>
                  <div className="text-muted small">
                    Read-only previews for the selected year using the dedicated promotion and
                    parent-balance endpoints.
                  </div>
                </div>
                <div className="d-flex gap-2">
                  <button
                    type="button"
                    className="btn btn-outline-primary btn-sm"
                    disabled={!selectedYear}
                    onClick={() => void loadPromotionPreview()}
                  >
                    Load promotion preview
                  </button>
                  <button
                    type="button"
                    className="btn btn-outline-primary btn-sm"
                    disabled={!selectedYear}
                    onClick={() => void loadParentBalances()}
                  >
                    Load parent balances
                  </button>
                </div>
              </div>

              <div className="row g-4">
                <div className="col-lg-6">
                  <div className="border rounded p-3 h-100">
                    <h3 className="h6">Promotion preview</h3>
                    {!promotionPreview ? (
                      <p className="text-muted small mb-0">Not loaded yet.</p>
                    ) : (
                      <>
                        <div className="small text-muted mb-3">
                          {promotionPreview.summary.total_students} students scanned,{' '}
                          {promotionPreview.summary.hold} hold rows.
                        </div>
                        <div className="table-responsive">
                          <table className="table table-sm align-middle mb-0">
                            <thead>
                              <tr>
                                <th>Student</th>
                                <th>Current</th>
                                <th>Action</th>
                                <th>Target</th>
                              </tr>
                            </thead>
                            <tbody>
                              {promotionRows.length === 0 ? (
                                <tr>
                                  <td colSpan={4} className="text-muted">
                                    No rows found.
                                  </td>
                                </tr>
                              ) : (
                                promotionRows.slice(0, 8).map((row) => (
                                  <tr key={row.student_id}>
                                    <td>
                                      <div>{row.student_name}</div>
                                      {row.warnings?.length ? (
                                        <div className="small text-warning">
                                          {row.warnings.join('; ')}
                                        </div>
                                      ) : null}
                                    </td>
                                    <td>{row.current_grade ?? '—'}</td>
                                    <td>{row.promotion_action}</td>
                                    <td>{row.target_grade ?? '—'}</td>
                                  </tr>
                                ))
                              )}
                            </tbody>
                          </table>
                        </div>
                      </>
                    )}
                  </div>
                </div>

                <div className="col-lg-6">
                  <div className="border rounded p-3 h-100">
                    <h3 className="h6">Parent balance preview</h3>
                    {!parentBalances ? (
                      <p className="text-muted small mb-0">Not loaded yet.</p>
                    ) : (
                      <>
                        <div className="small text-muted mb-3">
                          {parentBalances.summary.parents_with_non_zero_balance} parents with
                          balances, net ${formatMoney(parentBalances.summary.net_balance_to_transfer)}.
                        </div>
                        <div className="table-responsive">
                          <table className="table table-sm align-middle mb-0">
                            <thead>
                              <tr>
                                <th>Parent</th>
                                <th>Students</th>
                                <th>Transfer</th>
                              </tr>
                            </thead>
                            <tbody>
                              {balanceRows.length === 0 ? (
                                <tr>
                                  <td colSpan={3} className="text-muted">
                                    No rows found.
                                  </td>
                                </tr>
                              ) : (
                                balanceRows.slice(0, 8).map((row) => (
                                  <tr key={row.parent_id}>
                                    <td>{row.parent_name || `Parent #${row.parent_id}`}</td>
                                    <td>{row.student_names?.join(', ') || '—'}</td>
                                    <td>${formatMoney(row.amount_to_transfer)}</td>
                                  </tr>
                                ))
                              )}
                            </tbody>
                          </table>
                        </div>
                      </>
                    )}
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div className="card shadow-sm">
            <div className="card-body">
              <div className="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-3">
                <div>
                  <h2 className="h5 mb-1">Close Active Year</h2>
                  <div className="text-muted small">
                    Validate, preview, and close the current year through the transactional
                    school-year closure endpoints.
                  </div>
                </div>
                {activeYear ? (
                  <span className="badge text-bg-success">{activeYear.name}</span>
                ) : null}
              </div>

              {!activeYear ? (
                <p className="text-muted mb-0">No active school year is available to close.</p>
              ) : (
                <form onSubmit={(event) => void handleClose(event)}>
                  <div className="row g-3">
                    <div className="col-md-4">
                      <label className="form-label" htmlFor="close_school_year_name">
                        New year name
                      </label>
                      <input
                        id="close_school_year_name"
                        className="form-control"
                        value={closeForm.name}
                        onChange={(event) =>
                          setCloseForm((current) => ({ ...current, name: event.target.value }))
                        }
                        required
                      />
                    </div>
                    <div className="col-md-4">
                      <label className="form-label" htmlFor="close_school_year_start">
                        New year start date
                      </label>
                      <input
                        id="close_school_year_start"
                        className="form-control"
                        type="date"
                        value={closeForm.start_date}
                        onChange={(event) =>
                          setCloseForm((current) => ({ ...current, start_date: event.target.value }))
                        }
                        required
                      />
                    </div>
                    <div className="col-md-4">
                      <label className="form-label" htmlFor="close_school_year_end">
                        New year end date
                      </label>
                      <input
                        id="close_school_year_end"
                        className="form-control"
                        type="date"
                        value={closeForm.end_date}
                        onChange={(event) =>
                          setCloseForm((current) => ({ ...current, end_date: event.target.value }))
                        }
                        required
                      />
                    </div>
                  </div>

                  <div className="row g-3 mt-0">
                    <div className="col-md-8">
                      <label className="form-label" htmlFor="close_school_year_confirmation">
                        Confirmation text
                      </label>
                      <input
                        id="close_school_year_confirmation"
                        className="form-control"
                        value={closeForm.confirmation}
                        onChange={(event) =>
                          setCloseForm((current) => ({
                            ...current,
                            confirmation: event.target.value,
                          }))
                        }
                        placeholder={`CLOSE ${activeYear.name}`}
                        required
                      />
                    </div>
                    <div className="col-md-4 d-flex align-items-end">
                      <div className="form-check mb-2">
                        <input
                          id="close_school_year_balances"
                          type="checkbox"
                          className="form-check-input"
                          checked={closeForm.transfer_unpaid_balances}
                          onChange={(event) =>
                            setCloseForm((current) => ({
                              ...current,
                              transfer_unpaid_balances: event.target.checked,
                            }))
                          }
                        />
                        <label className="form-check-label" htmlFor="close_school_year_balances">
                          Transfer unpaid balances
                        </label>
                      </div>
                    </div>
                  </div>

                  <div className="d-flex flex-wrap gap-2 mt-3">
                    <button
                      type="button"
                      className="btn btn-outline-primary"
                      disabled={busyCloseAction === 'validate'}
                      onClick={() => void handleValidateClose()}
                    >
                      {busyCloseAction === 'validate' ? 'Validating…' : 'Validate'}
                    </button>
                    <button
                      type="button"
                      className="btn btn-outline-secondary"
                      disabled={busyCloseAction === 'preview'}
                      onClick={() => void handlePreviewClose()}
                    >
                      {busyCloseAction === 'preview' ? 'Previewing…' : 'Preview'}
                    </button>
                    <button type="submit" className="btn btn-danger" disabled={busyCloseAction === 'close'}>
                      {busyCloseAction === 'close' ? 'Closing…' : `Close ${activeYear.name}`}
                    </button>
                  </div>
                </form>
              )}

              {closeValidation ? (
                <div className={`alert mt-4 ${closeValidation.can_close ? 'alert-success' : 'alert-warning'}`}>
                  <div className="fw-semibold mb-2">
                    {closeValidation.can_close ? 'Closure validation passed.' : 'Closure is currently blocked.'}
                  </div>
                  <div className="row g-3 small">
                    <div className="col-md-3">
                      Students scanned: <strong>{closeValidation.promotion_summary.total_students}</strong>
                    </div>
                    <div className="col-md-3">
                      Missing decisions: <strong>{closeValidation.promotion_summary.hold}</strong>
                    </div>
                    <div className="col-md-3">
                      Parents with balances: <strong>{closeValidation.financial_summary.parents_with_non_zero_balance}</strong>
                    </div>
                    <div className="col-md-3">
                      Net transfer: <strong>${formatMoney(closeValidation.financial_summary.net_balance_to_transfer)}</strong>
                    </div>
                  </div>
                  {closeValidation.errors.length > 0 ? (
                    <ul className="mb-0 mt-3">
                      {closeValidation.errors.map((message) => (
                        <li key={message}>{message}</li>
                      ))}
                    </ul>
                  ) : null}
                  {closeValidation.warnings.length > 0 ? (
                    <ul className="mb-0 mt-3">
                      {closeValidation.warnings.map((message) => (
                        <li key={message}>{message}</li>
                      ))}
                    </ul>
                  ) : null}
                </div>
              ) : null}

              {closePreview ? (
                <div className="mt-4">
                  <div className="row g-4">
                    <div className="col-lg-6">
                      <div className="border rounded p-3 h-100">
                        <h3 className="h6">Closure promotion preview</h3>
                        <div className="table-responsive">
                          <table className="table table-sm align-middle mb-0">
                            <thead>
                              <tr>
                                <th>Student</th>
                                <th>Action</th>
                                <th>Target</th>
                              </tr>
                            </thead>
                            <tbody>
                              {closePromotionRows.slice(0, 8).map((row) => (
                                <tr key={`close-${row.student_id}`}>
                                  <td>{row.student_name}</td>
                                  <td>{row.promotion_action}</td>
                                  <td>{row.target_grade ?? '—'}</td>
                                </tr>
                              ))}
                            </tbody>
                          </table>
                        </div>
                      </div>
                    </div>
                    <div className="col-lg-6">
                      <div className="border rounded p-3 h-100">
                        <h3 className="h6">Closure balance preview</h3>
                        <div className="table-responsive">
                          <table className="table table-sm align-middle mb-0">
                            <thead>
                              <tr>
                                <th>Parent</th>
                                <th>Students</th>
                                <th>Transfer</th>
                              </tr>
                            </thead>
                            <tbody>
                              {closeBalanceRows.slice(0, 8).map((row) => (
                                <tr key={`balance-${row.parent_id}`}>
                                  <td>{row.parent_name || `Parent #${row.parent_id}`}</td>
                                  <td>{row.student_names?.join(', ') || '—'}</td>
                                  <td>${formatMoney(row.amount_to_transfer)}</td>
                                </tr>
                              ))}
                            </tbody>
                          </table>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              ) : null}
            </div>
          </div>
        </div>
      </div>
    </div>
  )
}
