import { useEffect, useMemo, useState, type FormEvent } from 'react'
import { ParentParityShell } from './ParentParityShell'
import {
  fetchAttendanceReportForm,
  fetchParentEnrollments,
  fetchParentEventsOverview,
  fetchParentInvoices,
  fetchParentProfile,
  fetchParentRegistrationOverview,
  submitParentAttendanceReport,
  submitContactMessage,
  updateParentEnrollments,
} from '../../api/session'
import type {
  ParentAttendanceReportFormResponse,
  ParentEnrollmentStudent,
  ParentEventsOverviewResponse,
  ParentInvoiceRow,
  ParentRegistrationOverviewResponse,
} from '../../api/types'
import { useAuth } from '../../auth/AuthProvider'

/** `Views/parent/contact.php` — POST /api/v1/contact */
export function ParentContactPage() {
  const { user } = useAuth()
  const [name, setName] = useState('')
  const [email, setEmail] = useState('')
  const [subject, setSubject] = useState('')
  const [message, setMessage] = useState('')
  const [status, setStatus] = useState<string | null>(null)
  const [error, setError] = useState<string | null>(null)
  const [loading, setLoading] = useState(false)

  useEffect(() => {
    let cancelled = false
    ;(async () => {
      try {
        const res = await fetchParentProfile()
        const p = res.profile
        if (cancelled || !p) return
        const fn = (p.firstname ?? '').trim()
        const ln = (p.lastname ?? '').trim()
        const full = `${fn} ${ln}`.trim()
        if (full) setName(full)
        if (p.email) setEmail(String(p.email))
      } catch {
        /* optional */
      }
    })()
    return () => {
      cancelled = true
    }
  }, [])

  useEffect(() => {
    if (!user?.name) return
    setName((n) => n || user.name)
  }, [user?.name])

  async function onSubmit(e: FormEvent) {
    e.preventDefault()
    setLoading(true)
    setError(null)
    setStatus(null)
    try {
      const res = await submitContactMessage({ name, email, subject, message })
      if (res.status) {
        setStatus(res.message ?? 'Message sent.')
        setSubject('')
        setMessage('')
      } else {
        setError('Could not send message.')
      }
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Request failed.')
    } finally {
      setLoading(false)
    }
  }

  return (
    <ParentParityShell
      title="Contact"
      ciViewFile="contact.php"
      legacyCiRoutes={['/parent/contact']}
    >
      {status ? <div className="alert alert-success small">{status}</div> : null}
      {error ? <div className="alert alert-danger small">{error}</div> : null}
      <form className="row g-3" onSubmit={onSubmit}>
        <div className="col-md-6">
          <label className="form-label" htmlFor="pc_name">
            Name
          </label>
          <input
            id="pc_name"
            className="form-control"
            value={name}
            onChange={(e) => setName(e.target.value)}
            required
            minLength={3}
          />
        </div>
        <div className="col-md-6">
          <label className="form-label" htmlFor="pc_email">
            Email
          </label>
          <input
            id="pc_email"
            type="email"
            className="form-control"
            value={email}
            onChange={(e) => setEmail(e.target.value)}
            required
          />
        </div>
        <div className="col-12">
          <label className="form-label" htmlFor="pc_subject">
            Subject
          </label>
          <input
            id="pc_subject"
            className="form-control"
            value={subject}
            onChange={(e) => setSubject(e.target.value)}
            required
            minLength={3}
          />
        </div>
        <div className="col-12">
          <label className="form-label" htmlFor="pc_message">
            Message
          </label>
          <textarea
            id="pc_message"
            className="form-control"
            rows={5}
            value={message}
            onChange={(e) => setMessage(e.target.value)}
            required
            minLength={10}
          />
        </div>
        <div className="col-12">
          <button type="submit" className="btn btn-success" disabled={loading}>
            {loading ? 'Sending…' : 'Send'}
          </button>
        </div>
      </form>
    </ParentParityShell>
  )
}

/** `Views/parent/enroll_classes.php` — GET/POST /api/v1/parents/enrollments */
export function ParentEnrollClassesPage() {
  const [error, setError] = useState<string | null>(null)
  const [message, setMessage] = useState<string | null>(null)
  const [loading, setLoading] = useState(true)
  const [saving, setSaving] = useState(false)
  const [schoolYears, setSchoolYears] = useState<{ school_year: string }[]>([])
  const [year, setYear] = useState<string | null>(null)
  const [dates, setDates] = useState<{
    withdrawalDeadline?: string | null
    lastDayOfRegistration?: string | null
    schoolStartDate?: string | null
  }>({})
  const [students, setStudents] = useState<ParentEnrollmentStudent[]>([])
  const [actions, setActions] = useState<Record<number, 'enroll' | 'withdraw'>>({})

  useEffect(() => {
    let cancelled = false
    ;(async () => {
      setLoading(true)
      try {
        const requestedYear = new URLSearchParams(window.location.search).get('school_year')
        const data = await fetchParentEnrollments(requestedYear)
        if (cancelled) return
        setSchoolYears(data.schoolYears ?? [])
        setYear(data.selectedYear ?? null)
        setDates({
          withdrawalDeadline: data.withdrawalDeadline,
          lastDayOfRegistration: data.lastDayOfRegistration,
          schoolStartDate: data.schoolStartDate,
        })
        setStudents(data.students ?? [])
        setActions({})
        setError(null)
      } catch (e) {
        if (!cancelled) setError(e instanceof Error ? e.message : 'Failed to load enrollments.')
      } finally {
        if (!cancelled) setLoading(false)
      }
    })()
    return () => {
      cancelled = true
    }
  }, [])

  const counts = useMemo(() => {
    let enrollable = 0
    let withdrawable = 0
    for (const student of students) {
      if (canEnroll(student)) enrollable += 1
      if (canWithdraw(student)) withdrawable += 1
    }
    return { enrollable, withdrawable }
  }, [students])

  async function loadYear(nextYear: string | null) {
    setLoading(true)
    setError(null)
    setMessage(null)
    try {
      const data = await fetchParentEnrollments(nextYear)
      setSchoolYears(data.schoolYears ?? [])
      setYear(data.selectedYear ?? nextYear)
      setDates({
        withdrawalDeadline: data.withdrawalDeadline,
        lastDayOfRegistration: data.lastDayOfRegistration,
        schoolStartDate: data.schoolStartDate,
      })
      setStudents(data.students ?? [])
      setActions({})
    } catch (e) {
      setError(e instanceof Error ? e.message : 'Failed to load enrollments.')
    } finally {
      setLoading(false)
    }
  }

  function setAction(studentId: number, action: 'enroll' | 'withdraw' | null) {
    setActions((current) => {
      const next = { ...current }
      if (action) next[studentId] = action
      else delete next[studentId]
      return next
    })
  }

  async function onSubmit(e: FormEvent) {
    e.preventDefault()
    setSaving(true)
    setError(null)
    setMessage(null)
    try {
      const enroll = Object.entries(actions)
        .filter(([, action]) => action === 'enroll')
        .map(([id]) => Number(id))
      const withdraw = Object.entries(actions)
        .filter(([, action]) => action === 'withdraw')
        .map(([id]) => Number(id))
      if (enroll.length === 0 && withdraw.length === 0) {
        setError('Choose at least one enrollment or withdrawal action.')
        return
      }
      const result = await updateParentEnrollments({ enroll, withdraw })
      const enrolledCount = result.enrolled?.length ?? 0
      const withdrawnCount = result.withdrawn?.length ?? 0
      await loadYear(year)
      setMessage(`Saved ${enrolledCount} enrollment request(s) and ${withdrawnCount} withdrawal request(s).`)
    } catch (e) {
      setError(e instanceof Error ? e.message : 'Could not save enrollment changes.')
    } finally {
      setSaving(false)
    }
  }

  return (
    <ParentParityShell
      title="Enroll in classes"
      ciViewFile="enroll_classes.php"
      legacyCiRoutes={['/parent/enroll_classes']}
    >
      {message ? <div className="alert alert-success small">{message}</div> : null}
      {error ? <div className="alert alert-warning small">{error}</div> : null}
      {loading ? (
        <p className="text-muted small">Loading…</p>
      ) : (
        <form onSubmit={onSubmit}>
          <div className="row g-3 align-items-end mb-3">
            <div className="col-md-4">
              <label className="form-label" htmlFor="enrollment_school_year">
                School Year
              </label>
              <select
                id="enrollment_school_year"
                className="form-select"
                value={year ?? ''}
                onChange={(e) => void loadYear(e.target.value || null)}
              >
                {year && !schoolYears.some((item) => item.school_year === year) ? (
                  <option value={year}>{year}</option>
                ) : null}
                {schoolYears.map((item) => (
                  <option key={item.school_year} value={item.school_year}>
                    {item.school_year}
                  </option>
                ))}
              </select>
            </div>
            <div className="col-md-8">
              <div className="d-flex flex-wrap gap-2">
                <button
                  type="button"
                  className="btn btn-outline-success"
                  onClick={() => {
                    const next: Record<number, 'enroll' | 'withdraw'> = { ...actions }
                    students.forEach((student) => {
                      if (canEnroll(student)) next[student.id] = 'enroll'
                    })
                    setActions(next)
                  }}
                  disabled={counts.enrollable === 0 || saving}
                >
                  Enroll all available
                </button>
                <button
                  type="button"
                  className="btn btn-outline-danger"
                  onClick={() => {
                    const next: Record<number, 'enroll' | 'withdraw'> = { ...actions }
                    students.forEach((student) => {
                      if (canWithdraw(student)) next[student.id] = 'withdraw'
                    })
                    setActions(next)
                  }}
                  disabled={counts.withdrawable === 0 || saving}
                >
                  Withdraw all eligible
                </button>
                <button
                  type="button"
                  className="btn btn-outline-secondary"
                  onClick={() => setActions({})}
                  disabled={Object.keys(actions).length === 0 || saving}
                >
                  Reset choices
                </button>
              </div>
            </div>
          </div>

          <div className="row g-3 mb-3">
            <div className="col-md-4">
              <div className="border rounded p-3 h-100">
                <div className="small text-muted">Last day of registration</div>
                <div className="fw-semibold">{formatDate(dates.lastDayOfRegistration)}</div>
              </div>
            </div>
            <div className="col-md-4">
              <div className="border rounded p-3 h-100">
                <div className="small text-muted">Withdrawal deadline</div>
                <div className="fw-semibold">{formatDate(dates.withdrawalDeadline)}</div>
              </div>
            </div>
            <div className="col-md-4">
              <div className="border rounded p-3 h-100">
                <div className="small text-muted">School start date</div>
                <div className="fw-semibold">{formatDate(dates.schoolStartDate)}</div>
              </div>
            </div>
          </div>

          <div className="table-responsive">
            <table className="table table-sm table-bordered align-middle">
              <thead>
                <tr>
                  <th>Student</th>
                  <th>Class</th>
                  <th>Status</th>
                  <th className="text-center">Enroll</th>
                  <th className="text-center">Withdraw</th>
                  <th>Choice</th>
                </tr>
              </thead>
              <tbody>
                {students.length === 0 ? (
                  <tr>
                    <td colSpan={6} className="text-muted">
                      No students for this overview.
                    </td>
                  </tr>
                ) : (
                  students.map((student) => {
                    const selected = actions[student.id] ?? null
                    const enrollEnabled = canEnroll(student)
                    const withdrawEnabled = canWithdraw(student)
                    return (
                      <tr key={student.id}>
                        <td>
                          <div className="fw-semibold">
                            {student.firstname} {student.lastname}
                          </div>
                          <div className="small text-muted">
                            {student.registration_grade ? `Grade ${student.registration_grade}` : 'Grade not set'}
                            {student.gender ? ` · ${student.gender}` : ''}
                          </div>
                        </td>
                        <td>{student.class_section ?? 'Class not Assigned'}</td>
                        <td>{statusBadge(student.enrollment_status ?? student.admission_status)}</td>
                        <td className="text-center">
                          <button
                            type="button"
                            className={`btn btn-sm ${selected === 'enroll' ? 'btn-success' : 'btn-outline-success'}`}
                            onClick={() => setAction(student.id, selected === 'enroll' ? null : 'enroll')}
                            disabled={!enrollEnabled || saving}
                          >
                            Enroll
                          </button>
                        </td>
                        <td className="text-center">
                          <button
                            type="button"
                            className={`btn btn-sm ${selected === 'withdraw' ? 'btn-danger' : 'btn-outline-danger'}`}
                            onClick={() => setAction(student.id, selected === 'withdraw' ? null : 'withdraw')}
                            disabled={!withdrawEnabled || saving}
                          >
                            Withdraw
                          </button>
                        </td>
                        <td>{choiceBadge(selected)}</td>
                      </tr>
                    )
                  })
                )}
              </tbody>
            </table>
          </div>
          <div className="d-flex flex-wrap gap-2 justify-content-end mt-3">
            <button
              type="button"
              className="btn btn-outline-secondary"
              onClick={() => void loadYear(year)}
              disabled={saving}
            >
              Refresh
            </button>
            <button type="submit" className="btn btn-success" disabled={saving || Object.keys(actions).length === 0}>
              {saving ? 'Saving…' : 'Submit changes'}
            </button>
          </div>
        </form>
      )}
    </ParentParityShell>
  )
}

function normalizedStatus(student: ParentEnrollmentStudent): string {
  return String(student.enrollment_status ?? student.admission_status ?? 'not enrolled').trim().toLowerCase()
}

function canEnroll(student: ParentEnrollmentStudent): boolean {
  if (student.disable_enroll) return false
  return ['not enrolled', 'withdrawn', 'refund pending'].includes(normalizedStatus(student))
}

function canWithdraw(student: ParentEnrollmentStudent): boolean {
  return ['admission under review', 'payment pending', 'enrolled', 'waitlist'].includes(normalizedStatus(student))
}

function formatDate(value?: string | null): string {
  if (!value) return 'Not set'
  return value
}

function statusBadge(status?: string | null) {
  const value = String(status ?? 'not enrolled').trim() || 'not enrolled'
  const normalized = value.toLowerCase()
  let className = 'bg-secondary'
  if (normalized === 'enrolled') className = 'bg-success'
  else if (normalized === 'payment pending') className = 'bg-info text-dark'
  else if (normalized === 'admission under review' || normalized === 'waitlist') className = 'bg-warning text-dark'
  else if (normalized === 'withdraw under review' || normalized === 'refund pending') className = 'bg-warning text-dark'
  else if (normalized === 'withdrawn' || normalized === 'denied') className = 'bg-danger'
  return <span className={`badge ${className}`}>{value}</span>
}

function choiceBadge(action: 'enroll' | 'withdraw' | null) {
  if (action === 'enroll') return <span className="badge bg-success">Enroll requested</span>
  if (action === 'withdraw') return <span className="badge bg-danger">Withdraw requested</span>
  return <span className="text-muted small">No change</span>
}

/** `Views/parent/payment_view.php` — GET /api/v1/parents/invoices */
export function ParentPaymentInvoicesPage() {
  const [error, setError] = useState<string | null>(null)
  const [loading, setLoading] = useState(true)
  const [rows, setRows] = useState<ParentInvoiceRow[]>([])

  useEffect(() => {
    let cancelled = false
    ;(async () => {
      setLoading(true)
      try {
        const data = await fetchParentInvoices(null)
        if (cancelled) return
        setRows(data.invoices ?? [])
        setError(null)
      } catch (e) {
        if (!cancelled) setError(e instanceof Error ? e.message : 'Failed to load invoices.')
      } finally {
        if (!cancelled) setLoading(false)
      }
    })()
    return () => {
      cancelled = true
    }
  }, [])

  return (
    <ParentParityShell
      title="Payments & invoices"
      ciViewFile="payment_view.php"
      legacyCiRoutes={['/parent/payment']}
    >
      {error ? <div className="alert alert-warning small">{error}</div> : null}
      {loading ? (
        <p className="text-muted small">Loading…</p>
      ) : (
        <div className="table-responsive">
          <table className="table table-sm table-striped">
            <thead>
              <tr>
                <th>#</th>
                <th>Invoice</th>
                <th>Balance</th>
                <th>Status</th>
                <th>Due</th>
              </tr>
            </thead>
            <tbody>
              {rows.length === 0 ? (
                <tr>
                  <td colSpan={5} className="text-muted">
                    No invoices.
                  </td>
                </tr>
              ) : (
                rows.map((inv) => (
                  <tr key={inv.id}>
                    <td>{inv.id}</td>
                    <td>{inv.invoice_number ?? '—'}</td>
                    <td>
                      {typeof inv.balance === 'number' ? inv.balance.toFixed(2) : '—'}
                    </td>
                    <td>{inv.status ?? '—'}</td>
                    <td>{inv.due_date ?? '—'}</td>
                  </tr>
                ))
              )}
            </tbody>
          </table>
        </div>
      )}
    </ParentParityShell>
  )
}

/** `Views/parent/event_participation.php` — GET/POST /api/v1/parents/events … */
export function ParentEventsPage() {
  const [error, setError] = useState<string | null>(null)
  const [loading, setLoading] = useState(true)
  const [data, setData] = useState<ParentEventsOverviewResponse | null>(null)

  useEffect(() => {
    let cancelled = false
    ;(async () => {
      setLoading(true)
      try {
        const data = await fetchParentEventsOverview()
        if (cancelled) return
        setData(data)
        setError(null)
      } catch (e) {
        if (!cancelled) setError(e instanceof Error ? e.message : 'Failed to load events.')
      } finally {
        if (!cancelled) setLoading(false)
      }
    })()
    return () => {
      cancelled = true
    }
  }, [])

  return (
    <ParentParityShell
      title="Event participation"
      ciViewFile="event_participation.php"
      legacyCiRoutes={['/parent/events']}
    >
      {error ? <div className="alert alert-warning small">{error}</div> : null}
      {loading ? (
        <p className="text-muted small">Loading…</p>
      ) : (
        <div className="table-responsive">
          <table className="table table-sm table-striped align-middle">
            <thead>
              <tr><th>Event</th><th>Date</th><th>Description</th></tr>
            </thead>
            <tbody>
              {(data?.activeEvents ?? []).length === 0 ? (
                <tr><td colSpan={3} className="text-muted">No active events.</td></tr>
              ) : (data?.activeEvents ?? []).map((event) => (
                <tr key={String(event.id ?? event.title ?? event.event_name)}>
                  <td>{event.title ?? event.event_name ?? '—'}</td>
                  <td>{event.event_date ?? event.start_date ?? '—'}</td>
                  <td>{event.description ?? '—'}</td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      )}
    </ParentParityShell>
  )
}

/** `Views/parent/register_student.php` — GET /api/v1/parents/registration */
export function ParentRegisterStudentPage() {
  const [error, setError] = useState<string | null>(null)
  const [loading, setLoading] = useState(true)
  const [data, setData] = useState<ParentRegistrationOverviewResponse | null>(null)

  useEffect(() => {
    let cancelled = false
    ;(async () => {
      setLoading(true)
      try {
        const data = await fetchParentRegistrationOverview()
        if (cancelled) return
        setData(data)
        setError(null)
      } catch (e) {
        if (!cancelled) setError(e instanceof Error ? e.message : 'Failed to load registration.')
      } finally {
        if (!cancelled) setLoading(false)
      }
    })()
    return () => {
      cancelled = true
    }
  }, [])

  return (
    <ParentParityShell
      title="Register student"
      ciViewFile="register_student.php"
      legacyCiRoutes={['/parent/register_student', '/parent/child_register']}
    >
      {error ? <div className="alert alert-warning small">{error}</div> : null}
      {loading ? (
        <p className="text-muted small">Loading…</p>
      ) : (
        <>
          <div className="d-flex flex-wrap gap-2 mb-3">
            <a className="btn btn-success btn-sm" href="/app/parent/add-student-form">Add student</a>
            <a className="btn btn-outline-success btn-sm" href="/app/parent/add-emergency-form">Add emergency contact</a>
          </div>
          <h3 className="h6">Students</h3>
          <div className="table-responsive mb-3">
            <table className="table table-sm table-striped">
              <thead><tr><th>Name</th><th>Grade</th><th>Class</th><th>Status</th></tr></thead>
              <tbody>
                {(data?.existingKids ?? []).length === 0 ? (
                  <tr><td colSpan={4} className="text-muted">No students registered.</td></tr>
                ) : (data?.existingKids ?? []).map((student) => (
                  <tr key={student.id}>
                    <td>{student.firstname} {student.lastname}</td>
                    <td>{student.registration_grade ?? '—'}</td>
                    <td>{student.class_section ?? '—'}</td>
                    <td>{student.admission_status ?? student.enrollment_status ?? '—'}</td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
          <h3 className="h6">Emergency contacts</h3>
          <div className="table-responsive">
            <table className="table table-sm table-striped">
              <thead><tr><th>Name</th><th>Phone</th><th>Email</th><th>Relation</th></tr></thead>
              <tbody>
                {(data?.emergencies ?? []).length === 0 ? (
                  <tr><td colSpan={4} className="text-muted">No emergency contacts.</td></tr>
                ) : (data?.emergencies ?? []).map((contact) => (
                  <tr key={contact.id}>
                    <td>{contact.name}</td>
                    <td>{contact.cellphone}</td>
                    <td>{contact.email ?? '—'}</td>
                    <td>{contact.relation ?? '—'}</td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </>
      )}
    </ParentParityShell>
  )
}

/** `Views/parent/report_attendance.php` — form: GET /api/v1/parents/attendance-reports/form */
export function ParentReportAttendancePage() {
  const [error, setError] = useState<string | null>(null)
  const [status, setStatus] = useState<string | null>(null)
  const [loading, setLoading] = useState(true)
  const [data, setData] = useState<ParentAttendanceReportFormResponse | null>(null)

  useEffect(() => {
    let cancelled = false
    ;(async () => {
      setLoading(true)
      try {
        const data = await fetchAttendanceReportForm()
        if (cancelled) return
        setData(data)
        setError(null)
      } catch (e) {
        if (!cancelled)
          setError(e instanceof Error ? e.message : 'Failed to load attendance report form.')
      } finally {
        if (!cancelled) setLoading(false)
      }
    })()
    return () => {
      cancelled = true
    }
  }, [])

  return (
    <ParentParityShell
      title="Report attendance"
      ciViewFile="report_attendance.php"
      legacyCiRoutes={['/parent/report-attendance']}
    >
      {status ? <div className="alert alert-success small">{status}</div> : null}
      {error ? <div className="alert alert-warning small">{error}</div> : null}
      {loading ? (
        <p className="text-muted small">Loading…</p>
      ) : (
        <form
          className="row g-3"
          onSubmit={async (e) => {
            e.preventDefault()
            setStatus(null)
            setError(null)
            const form = new FormData(e.currentTarget)
            try {
              const res = await submitParentAttendanceReport({
                student_ids: [Number(form.get('student_id'))],
                date: String(form.get('date') ?? ''),
                type: String(form.get('type') ?? ''),
                arrival_time: String(form.get('arrival_time') ?? ''),
                dismiss_time: String(form.get('dismiss_time') ?? ''),
                reason: String(form.get('reason') ?? ''),
              })
              setStatus(`Report submitted. Inserted: ${res.inserted ?? 0}`)
              e.currentTarget.reset()
            } catch (err) {
              setError(err instanceof Error ? err.message : 'Failed to submit report.')
            }
          }}
        >
          <div className="col-md-6">
            <label className="form-label">Student</label>
            <select name="student_id" className="form-control" required>
              <option value="">Select student</option>
              {Array.isArray(data?.students)
                ? data.students.map((student: { id: number; firstname: string; lastname: string }) => (
                    <option key={student.id} value={student.id}>
                      {student.firstname} {student.lastname}
                    </option>
                  ))
                : null}
            </select>
          </div>
          <div className="col-md-3">
            <label className="form-label">Date</label>
            <input name="date" type="date" className="form-control" defaultValue={String(data?.defaultDate ?? '')} required />
          </div>
          <div className="col-md-3">
            <label className="form-label">Type</label>
            <select name="type" className="form-control" required>
              <option value="absent">Absent</option>
              <option value="late">Late</option>
              <option value="early_dismissal">Early dismissal</option>
            </select>
          </div>
          <div className="col-md-6"><label className="form-label">Arrival time</label><input name="arrival_time" type="time" className="form-control" /></div>
          <div className="col-md-6"><label className="form-label">Dismiss time</label><input name="dismiss_time" type="time" className="form-control" /></div>
          <div className="col-12"><label className="form-label">Reason</label><textarea name="reason" className="form-control" rows={3} /></div>
          <div className="col-12"><button className="btn btn-success" type="submit">Submit report</button></div>
        </form>
      )}
    </ParentParityShell>
  )
}
