import { z } from 'zod';
import { KeyValueOfStringAndStringSchema } from '@isa/common/data-access';
import { DBHOrderItemListItemSchema } from './dbh-order-item-list-item.schema';
import { DisplayOrderSchema } from './display-order.schema';
import type { Receipt } from '../models/receipt';

/**
 * Schema for the handle command data structure.
 * Based on ActionHandlerService.handle() method parameters.
 */
export const HandleCommandSchema = z.object({
  /**
   * Action to be executed (from KeyValueDTOOfStringAndString)
   */
  action: KeyValueOfStringAndStringSchema.describe('Action to execute'),

  /**
   * List of order item list items (DBHOrderItemListItemDTO[])
   */
  items: z
    .array(DBHOrderItemListItemSchema)
    .describe('List of DBH order item list items'),

  /**
   * Optional compartment code
   */
  compartmentCode: z.string().describe('Compartment code').optional(),

  /**
   * Optional compartment info
   */
  compartmentInfo: z.string().describe('Compartment information').optional(),

  /**
   * Optional item quantity map (Map<number, number>)
   * Native JavaScript Map with number keys and values
   */
  itemQuantity: z
    .map(z.number(), z.number())
    .describe('Item quantity mapping (orderItemSubsetId -> quantity)')
    .optional(),

  /**
   * Optional receipts (ReceiptDTO[])
   * Using the Receipt model type directly
   */
  receipts: z
    .array(z.custom<Receipt>())
    .describe('List of receipts')
    .optional(),

  /**
   * Optional order data (OrderDTO)
   */
  order: DisplayOrderSchema.describe('Order information').optional(),
});

export type HandleCommand = z.infer<typeof HandleCommandSchema>;
