import { z } from 'zod';

export const OrderStatusSchema = z.enum([
  'pending',
  'confirmed',
  'processing',
  'shipped',
  'delivered',
  'cancelled',
  'refunded'
]);

export const PaymentMethodSchema = z.enum([
  'credit_card',
  'paypal',
  'bank_transfer',
  'alipay',
  'shopee_pay',
  'tiktok_pay',
  'wechat_pay',
  'apple_pay',
  'google_pay'
]);

export const OrderItemSchema = z.object({
  productId: z.string(),
  productName: z.string(),
  quantity: z.number().int().positive(),
  unitPrice: z.number().positive(),
  totalPrice: z.number().positive()
});

export const ShippingAddressSchema = z.object({
  name: z.string(),
  address: z.string(),
  city: z.string(),
  state: z.string().optional(),
  zip: z.string(),
  country: z.string()
});

export const OrderSchema = z.object({
  id: z.string(),
  customerId: z.string(),
  customerName: z.string().optional(),
  totalAmount: z.number().positive(),
  status: OrderStatusSchema,
  items: z.array(OrderItemSchema),
  shippingAddress: ShippingAddressSchema,
  paymentMethod: PaymentMethodSchema,
  createdAt: z.coerce.date(),
  updatedAt: z.coerce.date()
});

export const CreateOrderItemSchema = z.object({
  productId: z.string(),
  quantity: z.number().int().positive()
});

export const CreateOrderSchema = z.object({
  customerId: z.string(),
  items: z.array(CreateOrderItemSchema).min(1),
  shippingAddress: ShippingAddressSchema,
  paymentMethod: PaymentMethodSchema
});

export const UpdateOrderSchema = z.object({
  status: OrderStatusSchema.optional(),
  shippingAddress: ShippingAddressSchema.partial().optional()
});

export const OrderQuerySchema = z.object({
  customerId: z.string().optional(),
  status: OrderStatusSchema.optional(),
  startDate: z.string().datetime().optional(),
  endDate: z.string().datetime().optional(),
  page: z.coerce.number().int().positive().optional().default(1),
  pageSize: z.coerce.number().int().positive().max(100).optional().default(20)
});

export type Order = z.infer<typeof OrderSchema>;
export type OrderStatus = z.infer<typeof OrderStatusSchema>;
export type PaymentMethod = z.infer<typeof PaymentMethodSchema>;
export type OrderItem = z.infer<typeof OrderItemSchema>;
export type ShippingAddress = z.infer<typeof ShippingAddressSchema>;
export type CreateOrderInput = z.infer<typeof CreateOrderSchema>;
export type UpdateOrderInput = z.infer<typeof UpdateOrderSchema>;
export type OrderQueryInput = z.infer<typeof OrderQuerySchema>;
