import { EntityContainerSchema, EntitySchema } from '@isa/common/data-access';
import { z } from 'zod';
import { CategorySchema } from './category.schema';
import { CompanySchema } from './company.schema';
import { ComponentsSchema } from './components.schema';
import { ContributorHelperSchema } from './contributor-helper.schema';
import { FileSchema } from './file.schema';
import { FoodSchema } from './food.schema';
import { ItemLabelSchema } from './item-label.schema';
import { ItemTypeSchema } from './item-type.schema';
import { SizeSchema } from './size.schema';
import { TenantSchema } from './tenant.schema';
import { TextSchema } from './text.schema';
import { WeightSchema } from './weight.schema';

export const ItemSchema = z
  .object({
    itemNumber: z.string().describe('Unique item number identifier').optional(),
    name: z.string().describe('Item name').optional(),
    subtitle: z.string().describe('Item subtitle').optional(),
    description: z.string().describe('Item description text').optional(),
    ean: z.string().describe('European Article Number barcode').optional(),
    secondaryEAN: z.string().describe('Secondary EAN barcode').optional(),
    precedingItem: EntityContainerSchema(
      z.lazy((): z.ZodType<any> => ItemSchema),
    ).describe('Reference to preceding item version').optional(),
    precedingItemEAN: z.string().describe('EAN of preceding item version').optional(),
    contributors: z.array(ContributorHelperSchema).describe('List of contributors (authors, editors, etc)').optional(),
    manufacturer: EntityContainerSchema(CompanySchema).describe('Manufacturer company information').optional(),
    publicationDate: z.string().describe('Publication date of the item').optional(),
    categories: z.array(EntityContainerSchema(CategorySchema)).describe('List of item categories').optional(),
    manufacturingCosts: z.number().describe('Manufacturing costs').optional(),
    size: SizeSchema.describe('Physical size dimensions').optional(),
    weight: WeightSchema.describe('Total weight').optional(),
    netWeight: WeightSchema.describe('Net weight excluding packaging').optional(),
    weightOfPackaging: WeightSchema.describe('Weight of packaging only').optional(),
    itemType: ItemTypeSchema.describe('Type classification of the item').optional(),
    edition: z.string().describe('Edition information').optional(),
    serial: z.string().describe('Serial number').optional(),
    format: z.string().describe('Format code (e.g., HC, PB, EB)').optional(),
    formatDetail: z.string().describe('Detailed format description').optional(),
    volume: z.string().describe('Volume information').optional(),
    toxins: z.string().describe('Toxin warnings or information').optional(),
    files: z.array(EntityContainerSchema(FileSchema)).describe('List of associated files').optional(),
    texts: z.array(EntityContainerSchema(TextSchema)).describe('List of associated text content').optional(),
    set: EntityContainerSchema(ComponentsSchema).describe('Set components if item is a set').optional(),
    accessories: EntityContainerSchema(ComponentsSchema).describe('Accessory components').optional(),
    labels: z.array(ItemLabelSchema).describe('List of item labels').optional(),
    food: FoodSchema.describe('Food-specific information if applicable').optional(),
    tenant: EntityContainerSchema(TenantSchema).describe('Tenant identifier').optional(),
  })
  .extend(EntitySchema.shape);

export type Item = z.infer<typeof ItemSchema>;
