import { z } from 'zod';

/**
 * Schema for a single order item
 */
export const orderItemSchema = z.object({
  productId: z.coerce.number(),
  description: z.string(),
  pacth: z.string(),
  color: z.coerce.number(),
  stockId: z.coerce.number(),
  quantity: z.coerce.number(),
  salePrice: z.coerce.number(),
  deliveryType: z.string(),
  total: z.coerce.number(),
  weight: z.coerce.number(),
  department: z.string(),
  brand: z.string(),
});

export const shipmentSchema = z.object({
  placeId: z.number(),
  placeName: z.string(),
  street: z.string(),
  addressNumber: z.string(),
  bairro: z.string(),
  city: z.string(),
  state: z.string(),
  addressComplement: z.string().nullable(),
  cep: z.string(),
  commentOrder1: z.string().nullable(),
  commentOrder2: z.string().nullable(),
  commentDelivery1: z.string().nullable(),
  commentDelivery2: z.string().nullable(),
  commentDelivery3: z.string().nullable(),
  commentDelivery4: z.string().nullable(),
  shippimentId: z.number(),
  shippimentDate: z.string(), // Ou z.coerce.date() se quiser converter para Objeto Date
  shippimentComment: z.string().nullable(),
  place: z.any().nullable(),
  driver: z.string(),
  car: z.string(),
  closeDate: z.string().nullable(),
  separatorName: z.string().nullable(),
  confName: z.string().nullable(),
  releaseDate: z.string().nullable(),
});

export type Shipment = z.infer<typeof shipmentSchema>;

/**
 * Schema for the delivery API response
 * A API pode retornar data como objeto único ou array
 */
export const shipmentResponseSchema = z.object({
  success: z.boolean(),
  data: z.union([shipmentSchema, z.array(shipmentSchema)]).transform((data) =>
    Array.isArray(data) ? data : [data]
  ),
});



/**
 * Schema for the order items API response
 */
export const orderItemsResponseSchema = z.object({
  success: z.boolean(),
  data: z.array(orderItemSchema),
});


/**
 * TypeScript type inferred from the Zod schema
 */
export type OrderItem = z.infer<typeof orderItemSchema>;

/**
 * TypeScript type for the API response
 */
export type OrderItemsResponse = z.infer<typeof orderItemsResponseSchema>;

/**
 * Schema for cargo movement/transfer
 */
export const cargoMovementSchema = z.object({
  orderId: z.number().nullable(),
  transferDate: z.string(),
  invoiceId: z.number(),
  transactionId: z.number(),
  oldShipment: z.number(),
  newShipment: z.number(),
  transferText: z.string(),
  cause: z.string(),
  userName: z.string(),
  program: z.string(),
});

export type CargoMovement = z.infer<typeof cargoMovementSchema>;

export const cargoMovementResponseSchema = z.object({
  success: z.boolean(),
  data: z.array(cargoMovementSchema),
});

/**
 * Schema for cutting items
 */
export const cuttingItemSchema = z.object({
  productId: z.number(),
  description: z.string(),
  pacth: z.string(),
  stockId: z.number(),
  saleQuantity: z.number(),
  cutQuantity: z.number(),
  separedQuantity: z.number(),
});

export type CuttingItem = z.infer<typeof cuttingItemSchema>;

export const cuttingItemResponseSchema = z.object({
  success: z.boolean(),
  data: z.array(cuttingItemSchema),
});
