/**
 * Voice Agent Validation Schemas
 *
 * Zod validators for voice agent configuration and API requests
 */

import { z } from 'zod';

// Business hours schedule item
const businessHoursScheduleSchema = z.object({
  day: z.enum(['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']),
  open: z.string().regex(/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/, 'Invalid time format (HH:MM)'),
  close: z.string().regex(/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/, 'Invalid time format (HH:MM)'),
  isOpen: z.boolean(),
});

// Voice agent configuration schema
export const voiceAgentConfigSchema = z.object({
  language: z.enum(['ar', 'en', 'both']),
  voiceTone: z.enum(['professional', 'friendly', 'custom']),
  greetingMessage: z
    .object({
      ar: z.string().optional(),
      en: z.string().optional(),
    })
    .optional(),
  businessHours: z
    .object({
      timezone: z.string(), // e.g., 'Asia/Dubai'
      schedule: z.array(businessHoursScheduleSchema),
    })
    .optional(),
  useKnowledgeBase: z.boolean(),
  escalationKeywords: z.array(z.string()).optional(),
});

// Provision agent request
export const provisionAgentSchema = z.object({
  language: z.enum(['ar', 'en', 'both']).default('both'),
  voiceTone: z.enum(['professional', 'friendly', 'custom']).default('professional'),
  greetingMessage: z
    .object({
      ar: z.string().optional(),
      en: z.string().optional(),
    })
    .optional(),
  businessHours: z
    .object({
      timezone: z.string().default('Asia/Dubai'),
      schedule: z.array(businessHoursScheduleSchema).optional(),
    })
    .optional(),
  useKnowledgeBase: z.boolean().default(true),
  escalationKeywords: z.array(z.string()).optional(),
});

// Update agent config request
export const updateAgentConfigSchema = z.object({
  language: z.enum(['ar', 'en', 'both']).optional(),
  voiceTone: z.enum(['professional', 'friendly', 'custom']).optional(),
  greetingMessage: z
    .object({
      ar: z.string().optional(),
      en: z.string().optional(),
    })
    .optional(),
  businessHours: z
    .object({
      timezone: z.string(),
      schedule: z.array(businessHoursScheduleSchema),
    })
    .optional(),
  useKnowledgeBase: z.boolean().optional(),
  escalationKeywords: z.array(z.string()).optional(),
});

// Call event webhook payload
export const callEventSchema = z.object({
  call_id: z.string(),
  agent_id: z.string(),
  status: z.enum(['started', 'ended', 'failed']),
  start_time: z.string().optional(),
  end_time: z.string().optional(),
  duration_seconds: z.number().optional(),
  transcript: z
    .array(
      z.object({
        speaker: z.enum(['agent', 'user']),
        text: z.string(),
        timestamp: z.string(),
      })
    )
    .optional(),
  user_phone: z.string().optional(),
  metadata: z.record(z.any()).optional(),
});

// ElevenLabs API create agent request
export const createAgentRequestSchema = z.object({
  name: z.string(),
  prompt: z.string(),
  first_message: z.string(),
  language: z.string().optional(),
  voice_id: z.string().optional(),
  llm: z.string().optional().default('gpt-4o'),
  temperature: z.number().min(0).max(1).optional().default(0.7),
  max_tokens: z.number().optional().default(1000),
});

// ElevenLabs API update agent request
export const updateAgentRequestSchema = z.object({
  name: z.string().optional(),
  prompt: z.string().optional(),
  first_message: z.string().optional(),
  language: z.string().optional(),
  voice_id: z.string().optional(),
});

// Export types
export type VoiceAgentConfigInput = z.infer<typeof voiceAgentConfigSchema>;
export type ProvisionAgentInput = z.infer<typeof provisionAgentSchema>;
export type UpdateAgentConfigInput = z.infer<typeof updateAgentConfigSchema>;
export type CallEventInput = z.infer<typeof callEventSchema>;
export type CreateAgentRequestInput = z.infer<typeof createAgentRequestSchema>;
export type UpdateAgentRequestInput = z.infer<typeof updateAgentRequestSchema>;
