import { z } from 'zod';

/**
 * Per-port sales-email config update payload (Phase 7).
 *
 * Password fields accept:
 *   - undefined / omitted => leave unchanged
 *   - empty string ""     => leave unchanged (UI placeholder round-trip)
 *   - explicit null       => clear the value
 *   - non-empty string    => set to this value (encrypted before storage)
 */
export const updateSalesEmailConfigSchema = z.object({
  fromAddress: z.string().email().optional().nullable(),
  smtpHost: z.string().min(1).max(255).optional().nullable(),
  smtpPort: z.number().int().min(1).max(65535).optional().nullable(),
  smtpSecure: z.boolean().optional().nullable(),
  smtpUser: z.string().max(255).optional().nullable(),
  smtpPass: z.string().max(255).optional().nullable(),
  imapHost: z.string().min(1).max(255).optional().nullable(),
  imapPort: z.number().int().min(1).max(65535).optional().nullable(),
  imapUser: z.string().max(255).optional().nullable(),
  imapPass: z.string().max(255).optional().nullable(),
  authMethod: z.enum(['app_password', 'oauth_google', 'oauth_microsoft']).optional().nullable(),
  noreplyFromAddress: z.string().email().optional().nullable(),
  templateBerthPdfBody: z.string().max(50_000).optional().nullable(),
  templateBrochureBody: z.string().max(50_000).optional().nullable(),
  brochureMaxUploadMb: z.number().int().min(1).max(500).optional().nullable(),
  emailAttachThresholdMb: z.number().int().min(1).max(50).optional().nullable(),
});

export type UpdateSalesEmailConfigInput = z.infer<typeof updateSalesEmailConfigSchema>;
