/**
 * Catalog of transactional email templates that admins can customize from
 * /admin/email-templates. v1 supports subject-line overrides only; body
 * overrides (HTML / merge-token authoring) are a follow-on iteration.
 *
 * To add a template here:
 *   1. Pick a stable `key` and add it to TEMPLATE_KEYS (used for the
 *      `system_settings` row name).
 *   2. List the merge tokens that the template renders so the admin
 *      knows what placeholders are valid in any future override.
 *   3. Provide a `defaultSubject` string identical to what the code
 *      template emits when no override is set. Subject comparisons in
 *      the admin UI rely on this.
 */

export const TEMPLATE_KEYS = [
  'portal_activation',
  'portal_reset',
  'portal_invite_resend',
  'crm_invite',
  'inquiry_client_confirmation',
  'inquiry_sales_notification',
  'residential_inquiry_client_confirmation',
  'residential_inquiry_sales_alert',
  'contact_form_sales_alert',
  'contact_form_client_confirmation',
  // M-EM04: daily notification digest. The digest service previously
  // resolved its subject via `'crm_invite' as any` because no entry
  // existed; making it a first-class key removes the cast and lets
  // admins override the subject from /admin/email like every other
  // template.
  'notification_digest',
] as const;

export type TemplateKey = (typeof TEMPLATE_KEYS)[number];

export interface TemplateMetadata {
  key: TemplateKey;
  label: string;
  description: string;
  /** Token names available inside the subject (and future body) overrides. */
  mergeTokens: string[];
  /** The literal subject the code template uses when no override is set. */
  defaultSubject: string;
}

export const TEMPLATE_CATALOG: Record<TemplateKey, TemplateMetadata> = {
  portal_activation: {
    key: 'portal_activation',
    label: 'Portal - activation invite',
    description:
      'Sent to a client when an admin invites them to activate their portal account. Contains the activation link.',
    mergeTokens: ['portName', 'recipientName', 'ttlHours'],
    defaultSubject: 'Activate your {{portName}} client portal account',
  },
  portal_reset: {
    key: 'portal_reset',
    label: 'Portal - password reset',
    description:
      'Sent when a portal user requests a password reset. Contains the reset link with a short TTL.',
    mergeTokens: ['portName', 'recipientName', 'ttlMinutes'],
    defaultSubject: 'Reset your {{portName}} client portal password',
  },
  portal_invite_resend: {
    key: 'portal_invite_resend',
    label: 'Portal - invite resend',
    description: 'Re-sent activation email when an admin resends a pending portal invite.',
    mergeTokens: ['portName', 'recipientName', 'ttlHours'],
    defaultSubject: 'Activate your {{portName}} client portal account',
  },
  crm_invite: {
    key: 'crm_invite',
    label: 'CRM - staff invite',
    description: 'Sent to a new staff user when an admin invites them to the CRM.',
    mergeTokens: ['portName', 'recipientName', 'ttlHours'],
    defaultSubject: 'You have been invited to {{portName}} CRM',
  },
  inquiry_client_confirmation: {
    key: 'inquiry_client_confirmation',
    label: 'Inquiry - client confirmation',
    description: 'Auto-reply confirmation sent to the client after a website berth inquiry.',
    mergeTokens: ['portName', 'recipientName', 'mooringNumber'],
    defaultSubject: 'We received your inquiry - {{portName}}',
  },
  inquiry_sales_notification: {
    key: 'inquiry_sales_notification',
    label: 'Inquiry - sales notification',
    description: 'Internal alert sent to the sales team when a new website inquiry arrives.',
    mergeTokens: ['portName', 'clientName', 'mooringNumber', 'email'],
    defaultSubject: 'New berth inquiry - {{clientName}}',
  },
  residential_inquiry_client_confirmation: {
    key: 'residential_inquiry_client_confirmation',
    label: 'Residential inquiry - client confirmation',
    description: 'Auto-reply sent to the client after a residential property inquiry.',
    mergeTokens: ['portName', 'recipientName'],
    defaultSubject: 'We received your residential inquiry - {{portName}}',
  },
  residential_inquiry_sales_alert: {
    key: 'residential_inquiry_sales_alert',
    label: 'Residential inquiry - sales alert',
    description: 'Internal alert sent to residential sales recipients when an inquiry arrives.',
    mergeTokens: ['portName', 'clientName', 'email', 'phone'],
    defaultSubject: 'New residential inquiry - {{clientName}}',
  },
  contact_form_sales_alert: {
    key: 'contact_form_sales_alert',
    label: 'Contact form - sales alert',
    description: 'Internal alert sent to the sales team when a website contact form is submitted.',
    mergeTokens: ['portName', 'clientName', 'email'],
    defaultSubject: 'New contact form submission - {{clientName}}',
  },
  contact_form_client_confirmation: {
    key: 'contact_form_client_confirmation',
    label: 'Contact form - client confirmation',
    description: 'Auto-reply sent to a visitor after they submit the general website contact form.',
    mergeTokens: ['portName', 'recipientName'],
    defaultSubject: 'Thank you for contacting {{portName}}',
  },
  notification_digest: {
    key: 'notification_digest',
    label: 'Notification digest',
    description:
      "Daily roll-up of a rep's pending notifications. Fires from the digest worker; respects per-user opt-out.",
    mergeTokens: ['portName', 'recipientName', 'unreadCount'],
    defaultSubject: 'Your {{portName}} CRM digest - {{unreadCount}} updates',
  },
};

/** system_settings key for a template's subject override. */
export function settingKeyForSubject(key: TemplateKey): string {
  return `email_template_${key}_subject`;
}
