/**
 * Voice Agent Phone Configuration Validators
 *
 * Zod schemas for phone/SIP config validation
 */

import { z } from 'zod';

export const twilioConfigSchema = z.object({
  twilioPhoneNumber: z.string().min(10, 'Phone number must be at least 10 digits'),
  twilioAccountSid: z.string().regex(/^AC[a-f0-9]{32}$/, 'Invalid Twilio Account SID format'),
  twilioAuthToken: z.string().min(32, 'Auth token must be at least 32 characters'),
});

export const sipConfigSchema = z.object({
  sipUri: z.string().url('Invalid SIP URI').or(z.string().regex(/^sip:/, 'SIP URI must start with sip:')),
  sipUsername: z.string().optional(),
  sipPassword: z.string().optional(),
  sipRegistrar: z.string().optional(),
  sipTransport: z.enum(['udp', 'tcp', 'tls']).default('udp'),
});

export const phoneConfigSchema = z.discriminatedUnion('connectionType', [
  z.object({
    connectionType: z.literal('twilio_native'),
    phoneNumber: z.string().optional(),
    twilio: twilioConfigSchema,
  }),
  z.object({
    connectionType: z.literal('custom_sip'),
    phoneNumber: z.string().optional(),
    sip: sipConfigSchema,
  }),
]);

export const phoneTestSchema = z.object({
  connectionType: z.enum(['twilio_native', 'custom_sip']),
  testNumber: z.string().optional(),
});

export type TwilioConfigInput = z.infer<typeof twilioConfigSchema>;
export type SIPConfigInput = z.infer<typeof sipConfigSchema>;
export type PhoneConfigInput = z.infer<typeof phoneConfigSchema>;
export type PhoneTestInput = z.infer<typeof phoneTestSchema>;
