import {
  SettingsFormCard,
  type SettingFieldDef,
} from '@/components/admin/shared/settings-form-card';
import { PageHeader } from '@/components/shared/page-header';
import { SalesEmailConfigCard } from '@/components/admin/sales-email-config-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: '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">
      <PageHeader
        title="Email Settings"
        description="Per-port outgoing email configuration. SMTP credentials and the From address default to environment variables when these fields are blank. Header/footer HTML lives under Branding."
      />
      <SettingsFormCard
        title="From address"
        description="Identity headers used by system-generated emails."
        fields={FIELDS.slice(0, 3)}
      />
      <SettingsFormCard
        title="SMTP transport overrides"
        description="Optional per-port SMTP credentials. Leave blank to use the global env defaults."
        fields={FIELDS.slice(3)}
      />
      <SalesEmailConfigCard />
    </div>
  );
}
