import {
  SettingsFormCard,
  type SettingFieldDef,
} from '@/components/admin/shared/settings-form-card';

const FIELDS: SettingFieldDef[] = [
  {
    key: 'email_from_name',
    label: 'From name',
    description: 'Display name shown in the From: header on outgoing email.',
    type: 'string',
    placeholder: 'Port Nimara',
    defaultValue: '',
  },
  {
    key: 'email_from_address',
    label: 'From address',
    description: 'Sender email address. Falls back to SMTP_FROM env when blank.',
    type: 'string',
    placeholder: 'noreply@example.com',
    defaultValue: '',
  },
  {
    key: 'email_reply_to',
    label: 'Reply-to address',
    description: 'Optional Reply-To: header for replies (e.g. sales@example.com).',
    type: 'string',
    placeholder: 'sales@example.com',
    defaultValue: '',
  },
  {
    key: 'email_signature_html',
    label: 'Default signature (HTML)',
    description: 'Appended to the bottom of system-generated emails.',
    type: 'html',
    placeholder: '<p>—<br>The Port Nimara team</p>',
    defaultValue: '',
  },
  {
    key: 'email_footer_html',
    label: 'Email footer (HTML)',
    description: 'Legal/contact footer rendered at the very bottom of all emails.',
    type: 'html',
    placeholder: '<p style="font-size:11px;color:#888;">© Port Nimara · ul. ...</p>',
    defaultValue: '',
  },
  {
    key: 'smtp_host_override',
    label: 'SMTP host override',
    description: 'Optional. Falls back to SMTP_HOST env when blank.',
    type: 'string',
    placeholder: 'mail.example.com',
    defaultValue: '',
  },
  {
    key: 'smtp_port_override',
    label: 'SMTP port override',
    description: 'Optional. Falls back to SMTP_PORT env when blank.',
    type: 'number',
    placeholder: '587',
    defaultValue: null,
  },
  {
    key: 'smtp_user_override',
    label: 'SMTP username override',
    description: 'Optional. Falls back to SMTP_USER env when blank.',
    type: 'string',
    defaultValue: '',
  },
  {
    key: 'smtp_pass_override',
    label: 'SMTP password override',
    description: 'Optional. Stored in plain text — only set when overriding env credentials.',
    type: 'password',
    defaultValue: '',
  },
];

export default function EmailSettingsPage() {
  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-semibold">Email Settings</h1>
        <p className="text-sm text-muted-foreground">
          Per-port outgoing email configuration. SMTP credentials and the From address default to
          environment variables when these fields are blank.
        </p>
      </div>
      <SettingsFormCard
        title="From address & signature"
        description="Identity headers and shared HTML used by system-generated emails."
        fields={FIELDS.slice(0, 5)}
      />
      <SettingsFormCard
        title="SMTP transport overrides"
        description="Optional per-port SMTP credentials. Leave blank to use the global env defaults."
        fields={FIELDS.slice(5)}
      />
    </div>
  );
}
