import { EntitySchema, QuantityUnitTypeSchema } from '@isa/common/data-access';
import { z } from 'zod';
import { PriceSchema } from './price.schema';
import { ProductSchema } from './product.schema';
import { PromotionSchema } from './promotion.schema';
import { DisplayOrderItemSubsetSchema } from './display-order-item-subset.schema';

// Forward declaration for circular reference
export const DisplayOrderItemSchema = z
  .object({
    buyerComment: z.string().describe('Buyer comment').optional(),
    description: z.string().describe('Description text').optional(),
    features: z.record(z.string().describe('Features'), z.string()).optional(),
    order: z
      .lazy(() => z.any())
      .describe('Order')
      .optional(), // Circular reference to DisplayOrder
    orderDate: z.string().describe('Order date').optional(),
    orderItemNumber: z.string().describe('OrderItem number').optional(),
    price: PriceSchema.describe('Price information').optional(),
    product: ProductSchema.describe('Product').optional(),
    promotion: PromotionSchema.describe('Promotion information').optional(),
    quantity: z.number().describe('Quantity').optional(),
    quantityUnit: z.string().describe('Quantity unit').optional(),
    quantityUnitType: QuantityUnitTypeSchema.describe('QuantityUnit type'),
    subsetItems: z
      .array(DisplayOrderItemSubsetSchema)
      .describe('Subset items')
      .optional(),
  })
  .extend(EntitySchema.shape);

export type DisplayOrderItem = z.infer<typeof DisplayOrderItemSchema>;
