import { z } from 'zod';

export const MessageTypeSchema = z.enum([
  'COLLECT_ORDERS',
  'SYNC_ALL_ORDERS',
  'SYNC_RETURNS',
  'SYNC_ALL_RETURNS',
  'QUERY_REFUNDS',
  'AUTO_SHIP',
  'GET_SHOP_INFO',
  'UPDATE_SHOP_STATUS',
  'GET_FINGERPRINT',
  'VALIDATE_SESSION',
  'HEALTH_CHECK'
]);

export const BaseMessageSchema = z.object({
  type: MessageTypeSchema,
  payload: z.record(z.string(), z.unknown()),
  traceId: z.string().optional(),
  shopId: z.string().optional(),
  platform: z.string().optional()
});

export const CollectOrdersPayloadSchema = z.object({
  shopId: z.string(),
  platform: z.string(),
  dateRange: z.object({
    start: z.string(),
    end: z.string()
  }).optional()
});

export const SyncReturnsPayloadSchema = z.object({
  shopId: z.string(),
  platform: z.string(),
  returnIds: z.array(z.string()).optional()
});

export const AutoShipPayloadSchema = z.object({
  orderId: z.string(),
  trackingNumber: z.string(),
  carrier: z.string()
});

export const MessageResponseSchema = z.object({
  success: z.boolean(),
  data: z.unknown().optional(),
  error: z.object({
    code: z.string(),
    message: z.string()
  }).optional(),
  traceId: z.string()
});

export type MessageType = z.infer<typeof MessageTypeSchema>;
export type BaseMessage = z.infer<typeof BaseMessageSchema>;
export type CollectOrdersPayload = z.infer<typeof CollectOrdersPayloadSchema>;
export type SyncReturnsPayload = z.infer<typeof SyncReturnsPayloadSchema>;
export type AutoShipPayload = z.infer<typeof AutoShipPayloadSchema>;
export type MessageResponse = z.infer<typeof MessageResponseSchema>;
