import { z } from 'zod';

const orderItemSchema = z.object({
  name: z.string(),
  quantity: z.number().int().positive(),
  price: z.number().nonnegative().optional(),
  modifiers: z.array(z.string()).optional(),
  special_instructions: z.string().optional(),
});

export const createOrderSchema = z.object({
  branch_id: z.string().uuid().optional(),
  customer_id: z.string().uuid().optional(),
  customer_name: z.string().optional(),
  customer_phone: z.string().optional(),
  items: z.array(orderItemSchema).min(1, 'Order must have at least one item'),
  channel: z.enum(['chat', 'voice', 'whatsapp', 'email', 'sms', 'pos', 'zomato', 'swiggy', 'online']).optional(),
  delivery_type: z.string().optional(),
  table_number: z.string().optional(),
  subtotal: z.number().nonnegative().optional(),
  tax: z.number().nonnegative().optional(),
  total: z.number().nonnegative().optional(),
  special_instructions: z.string().optional(),
  coupon_code: z.string().trim().min(1).max(64).optional(),
  customer_email: z.string().email().optional(),
  delivery_address: z.string().optional(),
  agent_id: z.string().uuid().optional(),
  conversation_id: z.string().uuid().optional(),
  ai_confidence: z.number().min(0).max(1).optional(),
});

export const updateOrderSchema = z.object({
  status: z.enum(['pending','confirmed','kitchen','ready','delivered','completed','cancelled','failed','escalated','processing','rider']).optional(),
  modification_note: z.string().optional(),
  is_modified: z.boolean().optional(),
  customer_name: z.string().optional(),
  customer_phone: z.string().nullable().optional(),
  items: z.array(orderItemSchema).optional(),
  delivery_type: z.string().optional(),
  delivery_address: z.string().nullable().optional(),
  table_number: z.string().nullable().optional(),
  special_instructions: z.string().nullable().optional(),
});

export type CreateOrderInput = z.infer<typeof createOrderSchema>;
export type UpdateOrderInput = z.infer<typeof updateOrderSchema>;
