import { z } from 'zod';

const CHANNELS = ['email', 'phone', 'whatsapp', 'in_person', 'video', 'other'] as const;
const DIRECTIONS = ['outbound', 'inbound'] as const;

/** Cap summary length so a rep can't accidentally paste a 10MB email body. */
const SUMMARY_MAX = 4000;
/** Voice transcripts can run long for meetings; cap a bit higher than summary. */
const TRANSCRIPT_MAX = 20_000;
const TEMPLATES = ['call', 'visit', 'email'] as const;

export const createContactLogSchema = z.object({
  occurredAt: z.coerce.date(),
  channel: z.enum(CHANNELS),
  direction: z.enum(DIRECTIONS).default('outbound'),
  summary: z.string().min(1).max(SUMMARY_MAX),
  /** Raw Web Speech transcript, optional. Kept verbatim alongside the
   *  rep-polished summary so an edit to summary doesn't lose the original. */
  voiceTranscript: z.string().max(TRANSCRIPT_MAX).optional().nullable(),
  /** Which quick-template button (if any) seeded this entry. */
  templateUsed: z.enum(TEMPLATES).optional().nullable(),
  followUpAt: z.coerce.date().optional().nullable(),
});

export const updateContactLogSchema = z
  .object({
    occurredAt: z.coerce.date(),
    channel: z.enum(CHANNELS),
    direction: z.enum(DIRECTIONS),
    summary: z.string().min(1).max(SUMMARY_MAX),
    voiceTranscript: z.string().max(TRANSCRIPT_MAX).nullable(),
    templateUsed: z.enum(TEMPLATES).nullable(),
    followUpAt: z.coerce.date().nullable(),
  })
  .partial();

export type CreateContactLogPayload = z.infer<typeof createContactLogSchema>;
export type UpdateContactLogPayload = z.infer<typeof updateContactLogSchema>;
