import type { JSX } from 'react'
import {
  BelowSixtyPerformanceBody,
  DismissalBody,
  FinalWarningBody,
  FollowUpBody,
  ParentAttendanceAdminBody,
  ParentAttendanceParentBody,
} from './bodies/AttendanceEmailBodies'
import {
  ExtraChargeNoticeBody,
  PaymentReceiptBody,
  StatusAdmissionReviewBody,
  StatusDeniedBody,
  StatusEnrolledBody,
  StatusNotEnrolledBody,
  StatusPaymentPendingBody,
  StatusRefundPendingBody,
  StatusWaitlistBody,
  StatusWithdrawReviewBody,
  StatusWithdrawnBody,
} from './bodies/EnrollmentEmailBodies'
import {
  AdminStudentRegisteredBody,
  BroadcastWrapperSampleBody,
  CalendarNotificationBody,
  CustomHtmlBody,
  EventBroadcastBody,
  ResetPasswordBody,
  StudentRemovedBody,
  SupportNewAccountBody,
  WelcomeParentBody,
  WelcomeStaffBody,
  WelcomeUserBody,
  WrapLayoutSampleBody,
} from './bodies/MiscEmailBodies'
import { WhatsAppGroupInvitationDraftBody, WhatsAppInviteBody } from './bodies/WhatsAppEmailBodies'

export type EmailTemplateSlug =
  | 'final-warning'
  | 'follow-up'
  | 'parent-attendance-admin'
  | 'parent-attendance-parent'
  | 'below-sixty-performance'
  | 'dismissal'
  | 'payment-receipt'
  | 'extra-charge-notice'
  | 'reset-password'
  | 'welcome-parent'
  | 'welcome-staff'
  | 'welcome-user'
  | 'student-removed'
  | 'support-new-account'
  | 'admin-student-registered'
  | 'status-admission-review'
  | 'status-denied'
  | 'status-enrolled'
  | 'status-not-enrolled'
  | 'status-payment-pending'
  | 'status-refund-pending'
  | 'status-waitlist'
  | 'status-withdraw-review'
  | 'status-withdrawn'
  | 'calendar-notification'
  | 'event-broadcast'
  | 'custom-html'
  | 'wrap-layout-sample'
  | 'broadcast-wrapper-sample'
  | 'whatsapp-invite'
  | 'whatsapp-group-invitation'

export type RegistryEntry = {
  title: string
  sourceTemplatePath: string
  render: () => JSX.Element
}

export const EMAIL_TEMPLATE_REGISTRY: Record<EmailTemplateSlug, RegistryEntry> = {
  'final-warning': {
    title: 'Final attendance warning',
    sourceTemplatePath: 'emails/final_warning.php',
    render: () => <FinalWarningBody />,
  },
  'follow-up': {
    title: 'Attendance follow-up',
    sourceTemplatePath: 'emails/follow_up.php',
    render: () => <FollowUpBody />,
  },
  dismissal: {
    title: 'Dismissal (absences)',
    sourceTemplatePath: 'emails/dismissal.php',
    render: () => <DismissalBody />,
  },
  'parent-attendance-admin': {
    title: 'Parent attendance — admin notification',
    sourceTemplatePath: 'emails/parent_attendance_admin.php',
    render: () => <ParentAttendanceAdminBody />,
  },
  'parent-attendance-parent': {
    title: 'Parent attendance — confirmation to parent',
    sourceTemplatePath: 'emails/parent_attendance_parent.php',
    render: () => <ParentAttendanceParentBody />,
  },
  'below-sixty-performance': {
    title: 'Below 60 performance',
    sourceTemplatePath: 'emails/below_sixty_performance.php',
    render: () => <BelowSixtyPerformanceBody />,
  },
  'payment-receipt': {
    title: 'Payment receipt',
    sourceTemplatePath: 'emails/payment_receipt.php',
    render: () => <PaymentReceiptBody />,
  },
  'extra-charge-notice': {
    title: 'Extra charge notice',
    sourceTemplatePath: 'emails/extra_charge_notice.php',
    render: () => <ExtraChargeNoticeBody />,
  },
  'reset-password': {
    title: 'Reset password',
    sourceTemplatePath: 'emails/reset_password.php',
    render: () => <ResetPasswordBody />,
  },
  'welcome-parent': {
    title: 'Welcome — parent activation',
    sourceTemplatePath: 'emails/welcome_parent.php',
    render: () => <WelcomeParentBody />,
  },
  'welcome-staff': {
    title: 'Welcome — staff activation',
    sourceTemplatePath: 'emails/welcome_staff.php',
    render: () => <WelcomeStaffBody />,
  },
  'welcome-user': {
    title: 'Welcome — generic user',
    sourceTemplatePath: 'emails/welcome_user.php',
    render: () => <WelcomeUserBody />,
  },
  'student-removed': {
    title: 'Student removed',
    sourceTemplatePath: 'emails/student_removed.php',
    render: () => <StudentRemovedBody />,
  },
  'support-new-account': {
    title: 'Support — new account alert',
    sourceTemplatePath: 'emails/support_new_account.php',
    render: () => <SupportNewAccountBody />,
  },
  'admin-student-registered': {
    title: 'Admin — student registered',
    sourceTemplatePath: 'emails/admin_student_registered.php',
    render: () => <AdminStudentRegisteredBody />,
  },
  'status-admission-review': {
    title: 'Status — admission under review',
    sourceTemplatePath: 'emails/status_admission_review.php',
    render: () => <StatusAdmissionReviewBody />,
  },
  'status-denied': {
    title: 'Status — admission denied',
    sourceTemplatePath: 'emails/status_denied.php',
    render: () => <StatusDeniedBody />,
  },
  'status-enrolled': {
    title: 'Status — enrolled',
    sourceTemplatePath: 'emails/status_enrolled.php',
    render: () => <StatusEnrolledBody />,
  },
  'status-not-enrolled': {
    title: 'Status — not enrolled',
    sourceTemplatePath: 'emails/status_not_enrolled.php',
    render: () => <StatusNotEnrolledBody />,
  },
  'status-payment-pending': {
    title: 'Status — payment pending',
    sourceTemplatePath: 'emails/status_payment_pending.php',
    render: () => <StatusPaymentPendingBody />,
  },
  'status-refund-pending': {
    title: 'Status — refund pending',
    sourceTemplatePath: 'emails/status_refund_pending.php',
    render: () => <StatusRefundPendingBody />,
  },
  'status-waitlist': {
    title: 'Status — waitlist',
    sourceTemplatePath: 'emails/status_waitlist.php',
    render: () => <StatusWaitlistBody />,
  },
  'status-withdraw-review': {
    title: 'Status — withdraw under review',
    sourceTemplatePath: 'emails/status_withdraw_review.php',
    render: () => <StatusWithdrawReviewBody />,
  },
  'status-withdrawn': {
    title: 'Status — withdrawn',
    sourceTemplatePath: 'emails/status_withdrawn.php',
    render: () => <StatusWithdrawnBody />,
  },
  'calendar-notification': {
    title: 'Calendar notification',
    sourceTemplatePath: 'emails/calendar_notification.php',
    render: () => <CalendarNotificationBody />,
  },
  'event-broadcast': {
    title: 'Event broadcast',
    sourceTemplatePath: 'emails/event_broadcast.php',
    render: () => <EventBroadcastBody />,
  },
  'custom-html': {
    title: 'Custom HTML body',
    sourceTemplatePath: 'emails/custom_html.php',
    render: () => <CustomHtmlBody />,
  },
  'wrap-layout-sample': {
    title: 'Wrap layout ($body_html)',
    sourceTemplatePath: 'emails/_wrap_layout.php',
    render: () => <WrapLayoutSampleBody />,
  },
  'broadcast-wrapper-sample': {
    title: 'Broadcast wrapper ($content)',
    sourceTemplatePath: 'emails/broadcast_wrapper.php',
    render: () => <BroadcastWrapperSampleBody />,
  },
  'whatsapp-invite': {
    title: 'WhatsApp invite',
    sourceTemplatePath: 'emails/whatsapp_invite.php',
    render: () => <WhatsAppInviteBody />,
  },
  'whatsapp-group-invitation': {
    title: 'WhatsApp group invitation (draft text)',
    sourceTemplatePath: 'emails/whatsapp_group_invitation.php',
    render: () => <WhatsAppGroupInvitationDraftBody />,
  },
}

export function isEmailTemplateSlug(s: string): s is EmailTemplateSlug {
  return s in EMAIL_TEMPLATE_REGISTRY
}
