import { z } from 'zod';

const fallbackRuleSchema = z.object({
  trigger: z.string(),
  action: z.string(),
  priority: z.enum(['low', 'medium', 'high']).optional(),
});

const agentRoles = ['orders', 'reservations', 'campaigns', 'general'] as const;
const agentChannels = ['voice', 'chat'] as const;

const realtimeModels = ['gpt-realtime-1.5', 'gpt-realtime-mini', 'gpt-realtime', 'gpt-audio-1.5'] as const;

export const createAgentSchema = z.object({
  name: z.string().min(1).max(100),
  description: z.string().max(500).optional().nullable(),
  role: z.enum(agentRoles).default('general'),
  channels: z.array(z.enum(agentChannels)).min(1).default(['chat', 'voice']),
  branch_id: z.string().uuid().optional().nullable(),
  llm_model_id: z.string().uuid().optional().nullable(),
  voice_model_id: z.string().uuid().optional().nullable(),
  voice_language_code: z.string().max(10).optional().nullable(),
  realtime_model: z.enum(realtimeModels).optional().nullable(),
  system_prompt: z.string().max(10000).optional().nullable(),
  greeting_script: z.string().max(2000).optional().nullable(),
  closing_script: z.string().max(2000).optional().nullable(),
  fallback_rules: z.array(fallbackRuleSchema).optional(),
  capabilities: z.record(z.string(), z.any()).optional(),
  is_active: z.boolean().optional(),
  is_default: z.boolean().optional(),
  vad_threshold: z.number().min(0).max(1).optional().nullable(),
  vad_prefix_padding_ms: z.number().int().min(0).max(1000).optional().nullable(),
  vad_silence_duration_ms: z.number().int().min(200).max(3000).optional().nullable(),
});

export const updateAgentSchema = createAgentSchema.partial();

export type CreateAgentInput = z.infer<typeof createAgentSchema>;
export type UpdateAgentInput = z.infer<typeof updateAgentSchema>;
