import { z } from 'zod';
import { REALTIME_MODELS, VAD_DEFAULTS } from '@server/config/ai-defaults';

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;

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(REALTIME_MODELS).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(),
  menu_category_ids: z.array(z.string().uuid()).optional().nullable(),
  knowledge_base_ids: z.array(z.string().uuid()).optional().nullable(),
});

export const VAD_DEFAULT_VALUES = VAD_DEFAULTS;

export const updateAgentSchema = createAgentSchema.partial();

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