import { z } from 'zod';

import { baseListQuerySchema } from '@/lib/api/route-helpers';
import {
  optionalCountryIsoSchema,
  optionalIanaTimezoneSchema,
  optionalPhoneE164Schema,
} from '@/lib/validators/i18n';

// ─── Contact sub-schema ──────────────────────────────────────────────────────

export const contactSchema = z.object({
  channel: z.enum(['email', 'phone', 'whatsapp', 'other']),
  value: z.string().min(1),
  /** E.164-normalized number; required when channel is phone/whatsapp. */
  valueE164: optionalPhoneE164Schema.optional(),
  /** ISO-3166-1 alpha-2 country the number was parsed against. */
  valueCountry: optionalCountryIsoSchema.optional(),
  label: z.string().optional(),
  isPrimary: z.boolean().optional().default(false),
  notes: z.string().optional(),
});

// ─── Create ──────────────────────────────────────────────────────────────────

export const createClientSchema = z.object({
  fullName: z.string().min(1).max(200),
  contacts: z.array(contactSchema).min(1, 'At least one contact is required'),
  /** ISO-3166-1 alpha-2 nationality code. */
  nationalityIso: optionalCountryIsoSchema.optional(),
  preferredContactMethod: z.enum(['email', 'phone', 'whatsapp']).optional(),
  preferredLanguage: z.string().optional(),
  /** IANA timezone (e.g. 'Europe/Warsaw'). */
  timezone: optionalIanaTimezoneSchema.optional(),
  source: z.enum(['website', 'manual', 'referral', 'broker']).optional(),
  sourceDetails: z.string().optional(),
  tagIds: z.array(z.string()).optional().default([]),
});

// ─── Update ──────────────────────────────────────────────────────────────────

export const updateClientSchema = createClientSchema
  .omit({ contacts: true, tagIds: true })
  .partial();

// ─── List ─────────────────────────────────────────────────────────────────────

export const listClientsSchema = baseListQuerySchema.extend({
  source: z.enum(['website', 'manual', 'referral', 'broker']).optional(),
  nationality: z.string().optional(),
  tagIds: z
    .string()
    .transform((v) => v.split(',').filter(Boolean))
    .optional(),
});

// ─── Types ────────────────────────────────────────────────────────────────────

export type ContactInput = z.infer<typeof contactSchema>;
export type CreateClientInput = z.infer<typeof createClientSchema>;
export type UpdateClientInput = z.infer<typeof updateClientSchema>;
export type ListClientsInput = z.infer<typeof listClientsSchema>;
