/**
 * Manufacturing Domain Template
 *
 * A comprehensive manufacturing data domain following domain-driven design principles.
 * This template covers the complete manufacturing lifecycle including:
 *
 * - Product Management: Products, SKUs, specifications, BOMs
 * - Inventory Management: Stock levels, warehouses, suppliers
 * - Production Planning: Work orders, scheduling, capacity
 * - Quality Control: Inspections, testing, compliance
 * - Equipment Management: Machinery, maintenance, calibration
 * - Supply Chain: Suppliers, procurement, logistics
 *
 * Key Features:
 * - Complete product lifecycle management
 * - Inventory tracking and warehouse management
 * - Production planning and work order management
 * - Quality control and compliance tracking
 * - Equipment maintenance and calibration
 * - Supplier and procurement management
 * - Supply chain logistics and tracking
 *
 * Use Cases:
 * - Manufacturing companies
 * - Production facilities
 * - Quality management systems
 * - Supply chain management
 * - Equipment maintenance tracking
 */

import { DataDomain } from '../../../DataDomain.js'
import {
  addIdField,
  addNameField,
  addDescriptionField,
  addEmailField,
  addPhoneField,
  addCustomStatusField,
  addCreatedAtField,
  addUpdatedAtField,
  addCurrencyAmountField,
} from '../../../helpers/Intelisense.js'
import type { CreateTemplateOptions } from '../../types.js'

function createDomain(): DataDomain {
  return new DataDomain({
    info: {
      name: 'Manufacturing Platform',
      displayName: 'Manufacturing Platform',
      description:
        'A comprehensive manufacturing platform with product management, inventory control, ' +
        'production planning, quality control, equipment management, and supply chain capabilities',
    },
  })
}

/**
 * Creates a comprehensive manufacturing data domain following domain-driven design principles.
 *
 * This function demonstrates the proper hierarchy and organization for a manufacturing platform:
 * 1. Creates the root DataDomain for the manufacturing platform
 * 2. Organizes functionality into logical namespaces (Product Management, Inventory Management, etc.)
 * 3. Groups related entities into models within each namespace
 * 4. Defines entities with proper semantic annotations for manufacturing workflows
 * 5. Establishes associations with appropriate cardinality for manufacturing relationships
 *
 * @returns A fully configured DataDomain with all manufacturing entities and relationships
 */
export default function createManufacturingDomain(options: CreateTemplateOptions = {}): DataDomain {
  // Ensure the root data domain
  const domain = options.domain ?? createDomain()

  //
  // 1. PRODUCT MANAGEMENT NAMESPACE
  //
  const productManagement = domain.addNamespace({
    info: {
      name: 'ProductManagement',
      displayName: 'Product Management',
      description: 'Product definitions, SKUs, specifications, and bill of materials',
    },
  })

  // Product Model
  const productModel = productManagement.addModel({
    info: {
      name: 'Products',
      displayName: 'Product Management',
      description: 'Product definitions and specifications',
    },
  })

  // Product Entity
  const productEntity = productModel.addEntity({
    info: {
      name: 'product',
      displayName: 'Product',
      description: 'Manufactured product with specifications and BOM',
    },
  })

  addIdField(productEntity, { displayName: 'Product ID', description: 'Unique identifier for the product' })

  addNameField(productEntity, { description: 'Product name and title' })

  addDescriptionField(productEntity, { description: 'Product description and specifications' })

  productEntity.addProperty({
    info: { name: 'sku', displayName: 'SKU', description: 'Stock Keeping Unit identifier' },
    type: 'string',
    required: true,
    unique: true,
  })

  productEntity.addProperty({
    info: { name: 'product_type', displayName: 'Product Type', description: 'Type of manufactured product' },
    type: 'string',
    required: true,
    schema: {
      enum: ['finished_good', 'component', 'raw_material', 'sub_assembly'],
    },
  })

  productEntity.addProperty({
    info: { name: 'unit_of_measure', displayName: 'Unit of Measure', description: 'Standard unit for the product' },
    type: 'string',
    required: true,
    schema: {
      enum: ['pieces', 'kilograms', 'meters', 'liters', 'units'],
    },
  })

  addCustomStatusField(productEntity, ['active', 'discontinued', 'development'], {
    displayName: 'Product Status',
    description: 'Current status of the product',
  })

  addCreatedAtField(productEntity, { description: 'When the product was created' })
  addUpdatedAtField(productEntity, { description: 'When the product was last updated' })

  // Bill of Materials Entity
  const bomEntity = productModel.addEntity({
    info: {
      name: 'bill_of_materials',
      displayName: 'Bill of Materials',
      description: 'Component list and quantities for product assembly',
    },
  })

  addIdField(bomEntity, { displayName: 'BOM ID', description: 'Unique identifier for the BOM' })

  addNameField(bomEntity, { description: 'BOM name and description' })

  bomEntity.addProperty({
    info: { name: 'version', displayName: 'Version', description: 'BOM version number' },
    type: 'string',
    required: true,
  })

  bomEntity.addProperty({
    info: { name: 'quantity', displayName: 'Quantity', description: 'Quantity of the main product' },
    type: 'number',
    required: true,
    schema: {
      minimum: 1,
    },
  })

  addCustomStatusField(bomEntity, ['draft', 'active', 'obsolete'], {
    displayName: 'BOM Status',
    description: 'Current status of the BOM',
  })

  addCreatedAtField(bomEntity, { description: 'When the BOM was created' })

  // BOM-Product Association (Many-to-One)
  bomEntity.addAssociation(
    { key: productEntity.key },
    {
      info: { name: 'product', displayName: 'Product', description: 'Product for this BOM' },
      required: true,
      multiple: false,
    }
  )

  // BOM Component Entity
  const bomComponentEntity = productModel.addEntity({
    info: {
      name: 'bom_component',
      displayName: 'BOM Component',
      description: 'Individual component in a bill of materials',
    },
  })

  addIdField(bomComponentEntity, { displayName: 'Component ID', description: 'Unique identifier for the component' })

  bomComponentEntity.addProperty({
    info: { name: 'quantity', displayName: 'Quantity', description: 'Required quantity of the component' },
    type: 'number',
    required: true,
    schema: {
      minimum: 0,
    },
  })

  bomComponentEntity.addProperty({
    info: { name: 'position', displayName: 'Position', description: 'Position in the BOM hierarchy' },
    type: 'number',
    required: true,
    schema: {
      minimum: 1,
    },
  })

  addCreatedAtField(bomComponentEntity, { description: 'When the component was added' })

  // BOMComponent-BOM Association (Many-to-One)
  bomComponentEntity.addAssociation(
    { key: bomEntity.key },
    {
      info: { name: 'bom', displayName: 'BOM', description: 'BOM for this component' },
      required: true,
      multiple: false,
    }
  )

  // BOMComponent-Product Association (Many-to-One)
  bomComponentEntity.addAssociation(
    { key: productEntity.key },
    {
      info: { name: 'component', displayName: 'Component', description: 'Component product' },
      required: true,
      multiple: false,
    }
  )

  //
  // 2. INVENTORY MANAGEMENT NAMESPACE
  //
  const inventoryManagement = domain.addNamespace({
    info: {
      name: 'InventoryManagement',
      displayName: 'Inventory Management',
      description: 'Stock levels, warehouses, and inventory tracking',
    },
  })

  // Inventory Model
  const inventoryModel = inventoryManagement.addModel({
    info: {
      name: 'Inventory',
      displayName: 'Inventory Management',
      description: 'Inventory tracking and stock management',
    },
  })

  // Warehouse Entity
  const warehouseEntity = inventoryModel.addEntity({
    info: {
      name: 'warehouse',
      displayName: 'Warehouse',
      description: 'Storage facility and location',
    },
  })

  addIdField(warehouseEntity, { displayName: 'Warehouse ID', description: 'Unique identifier for the warehouse' })

  addNameField(warehouseEntity, { description: 'Warehouse name and location' })

  addDescriptionField(warehouseEntity, { description: 'Warehouse description and features' })

  warehouseEntity.addProperty({
    info: { name: 'warehouse_type', displayName: 'Warehouse Type', description: 'Type of warehouse facility' },
    type: 'string',
    required: true,
    schema: {
      enum: ['production', 'distribution', 'raw_materials', 'finished_goods'],
    },
  })

  warehouseEntity.addProperty({
    info: { name: 'address', displayName: 'Address', description: 'Warehouse physical address' },
    type: 'string',
    required: true,
  })

  addCustomStatusField(warehouseEntity, ['active', 'inactive', 'maintenance'], {
    displayName: 'Warehouse Status',
    description: 'Current status of the warehouse',
  })

  addCreatedAtField(warehouseEntity, { description: 'When the warehouse was created' })

  // Inventory Item Entity
  const inventoryItemEntity = inventoryModel.addEntity({
    info: {
      name: 'inventory_item',
      displayName: 'Inventory Item',
      description: 'Stock item with quantity and location',
    },
  })

  addIdField(inventoryItemEntity, {
    displayName: 'Inventory ID',
    description: 'Unique identifier for the inventory item',
  })

  inventoryItemEntity.addProperty({
    info: { name: 'quantity', displayName: 'Quantity', description: 'Current stock quantity' },
    type: 'number',
    required: true,
    schema: {
      minimum: 0,
    },
  })

  inventoryItemEntity.addProperty({
    info: { name: 'minimum_stock', displayName: 'Minimum Stock', description: 'Minimum stock level for reorder' },
    type: 'number',
    required: true,
    schema: {
      minimum: 0,
    },
  })

  inventoryItemEntity.addProperty({
    info: { name: 'maximum_stock', displayName: 'Maximum Stock', description: 'Maximum stock level' },
    type: 'number',
    required: true,
    schema: {
      minimum: 0,
    },
  })

  inventoryItemEntity.addProperty({
    info: { name: 'location', displayName: 'Location', description: 'Storage location within warehouse' },
    type: 'string',
    required: true,
  })

  addCustomStatusField(inventoryItemEntity, ['available', 'reserved', 'quarantine', 'expired'], {
    displayName: 'Stock Status',
    description: 'Current status of the inventory item',
  })

  addCreatedAtField(inventoryItemEntity, { description: 'When the inventory item was created' })
  addUpdatedAtField(inventoryItemEntity, { description: 'When the inventory item was last updated' })

  // InventoryItem-Product Association (Many-to-One)
  inventoryItemEntity.addAssociation(
    { key: productEntity.key },
    {
      info: { name: 'product', displayName: 'Product', description: 'Product for this inventory item' },
      required: true,
      multiple: false,
    }
  )

  // InventoryItem-Warehouse Association (Many-to-One)
  inventoryItemEntity.addAssociation(
    { key: warehouseEntity.key },
    {
      info: { name: 'warehouse', displayName: 'Warehouse', description: 'Warehouse for this inventory item' },
      required: true,
      multiple: false,
    }
  )

  //
  // 3. PRODUCTION PLANNING NAMESPACE
  //
  const productionPlanning = domain.addNamespace({
    info: {
      name: 'ProductionPlanning',
      displayName: 'Production Planning',
      description: 'Work orders, scheduling, and production capacity',
    },
  })

  // Production Model
  const productionModel = productionPlanning.addModel({
    info: {
      name: 'Production',
      displayName: 'Production Management',
      description: 'Production planning and work order management',
    },
  })

  // Work Order Entity
  const workOrderEntity = productionModel.addEntity({
    info: {
      name: 'work_order',
      displayName: 'Work Order',
      description: 'Production work order with scheduling and routing',
    },
  })

  addIdField(workOrderEntity, { displayName: 'Work Order ID', description: 'Unique identifier for the work order' })

  addNameField(workOrderEntity, { description: 'Work order name and description' })

  workOrderEntity.addProperty({
    info: { name: 'order_quantity', displayName: 'Order Quantity', description: 'Quantity to be produced' },
    type: 'number',
    required: true,
    schema: {
      minimum: 1,
    },
  })

  workOrderEntity.addProperty({
    info: { name: 'start_date', displayName: 'Start Date', description: 'Planned start date' },
    type: 'date',
    required: true,
  })

  workOrderEntity.addProperty({
    info: { name: 'due_date', displayName: 'Due Date', description: 'Planned completion date' },
    type: 'date',
    required: true,
  })

  workOrderEntity.addProperty({
    info: { name: 'priority', displayName: 'Priority', description: 'Work order priority level' },
    type: 'string',
    required: true,
    schema: {
      enum: ['low', 'medium', 'high', 'urgent'],
    },
  })

  addCustomStatusField(workOrderEntity, ['planned', 'in_progress', 'completed', 'cancelled'], {
    displayName: 'Work Order Status',
    description: 'Current status of the work order',
  })

  addCreatedAtField(workOrderEntity, { description: 'When the work order was created' })

  // WorkOrder-Product Association (Many-to-One)
  workOrderEntity.addAssociation(
    { key: productEntity.key },
    {
      info: { name: 'product', displayName: 'Product', description: 'Product to be manufactured' },
      required: true,
      multiple: false,
    }
  )

  // Production Step Entity
  const productionStepEntity = productionModel.addEntity({
    info: {
      name: 'production_step',
      displayName: 'Production Step',
      description: 'Individual step in the production process',
    },
  })

  addIdField(productionStepEntity, { displayName: 'Step ID', description: 'Unique identifier for the production step' })

  addNameField(productionStepEntity, { description: 'Step name and description' })

  productionStepEntity.addProperty({
    info: { name: 'sequence', displayName: 'Sequence', description: 'Step sequence number' },
    type: 'number',
    required: true,
    schema: {
      minimum: 1,
    },
  })

  productionStepEntity.addProperty({
    info: { name: 'estimated_duration', displayName: 'Estimated Duration', description: 'Estimated time in minutes' },
    type: 'number',
    required: true,
    schema: {
      minimum: 1,
    },
  })

  addCustomStatusField(productionStepEntity, ['pending', 'in_progress', 'completed', 'skipped'], {
    displayName: 'Step Status',
    description: 'Current status of the production step',
  })

  addCreatedAtField(productionStepEntity, { description: 'When the production step was created' })

  // ProductionStep-WorkOrder Association (Many-to-One)
  productionStepEntity.addAssociation(
    { key: workOrderEntity.key },
    {
      info: { name: 'work_order', displayName: 'Work Order', description: 'Work order for this step' },
      required: true,
      multiple: false,
    }
  )

  //
  // 4. QUALITY CONTROL NAMESPACE
  //
  const qualityControl = domain.addNamespace({
    info: {
      name: 'QualityControl',
      displayName: 'Quality Control',
      description: 'Inspections, testing, and compliance tracking',
    },
  })

  // Quality Model
  const qualityModel = qualityControl.addModel({
    info: {
      name: 'Quality',
      displayName: 'Quality Management',
      description: 'Quality control and inspection processes',
    },
  })

  // Quality Inspection Entity
  const qualityInspectionEntity = qualityModel.addEntity({
    info: {
      name: 'quality_inspection',
      displayName: 'Quality Inspection',
      description: 'Quality inspection and testing records',
    },
  })

  addIdField(qualityInspectionEntity, {
    displayName: 'Inspection ID',
    description: 'Unique identifier for the quality inspection',
  })

  addNameField(qualityInspectionEntity, { description: 'Inspection name and description' })

  qualityInspectionEntity.addProperty({
    info: { name: 'inspection_type', displayName: 'Inspection Type', description: 'Type of quality inspection' },
    type: 'string',
    required: true,
    schema: {
      enum: ['incoming', 'in_process', 'final', 'audit'],
    },
  })

  qualityInspectionEntity.addProperty({
    info: { name: 'inspector', displayName: 'Inspector', description: 'Name of the inspector' },
    type: 'string',
    required: true,
  })

  qualityInspectionEntity.addProperty({
    info: { name: 'inspection_date', displayName: 'Inspection Date', description: 'Date of the inspection' },
    type: 'date',
    required: true,
  })

  qualityInspectionEntity.addProperty({
    info: { name: 'sample_size', displayName: 'Sample Size', description: 'Number of items inspected' },
    type: 'number',
    required: true,
    schema: {
      minimum: 1,
    },
  })

  qualityInspectionEntity.addProperty({
    info: { name: 'defects_found', displayName: 'Defects Found', description: 'Number of defects found' },
    type: 'number',
    required: true,
    schema: {
      minimum: 0,
    },
  })

  addCustomStatusField(qualityInspectionEntity, ['pending', 'in_progress', 'passed', 'failed'], {
    displayName: 'Inspection Status',
    description: 'Current status of the quality inspection',
  })

  addCreatedAtField(qualityInspectionEntity, { description: 'When the inspection was created' })

  // QualityInspection-Product Association (Many-to-One)
  qualityInspectionEntity.addAssociation(
    { key: productEntity.key },
    {
      info: { name: 'product', displayName: 'Product', description: 'Product being inspected' },
      required: true,
      multiple: false,
    }
  )

  // QualityInspection-WorkOrder Association (Many-to-One)
  qualityInspectionEntity.addAssociation(
    { key: workOrderEntity.key },
    {
      info: { name: 'work_order', displayName: 'Work Order', description: 'Work order for this inspection' },
      required: false,
      multiple: false,
    }
  )

  //
  // 5. EQUIPMENT MANAGEMENT NAMESPACE
  //
  const equipmentManagement = domain.addNamespace({
    info: {
      name: 'EquipmentManagement',
      displayName: 'Equipment Management',
      description: 'Machinery, maintenance, and calibration tracking',
    },
  })

  // Equipment Model
  const equipmentModel = equipmentManagement.addModel({
    info: {
      name: 'Equipment',
      displayName: 'Equipment Management',
      description: 'Equipment and machinery management',
    },
  })

  // Equipment Entity
  const equipmentEntity = equipmentModel.addEntity({
    info: {
      name: 'equipment',
      displayName: 'Equipment',
      description: 'Manufacturing equipment and machinery',
    },
  })

  addIdField(equipmentEntity, { displayName: 'Equipment ID', description: 'Unique identifier for the equipment' })

  addNameField(equipmentEntity, { description: 'Equipment name and model' })

  addDescriptionField(equipmentEntity, { description: 'Equipment description and specifications' })

  equipmentEntity.addProperty({
    info: { name: 'equipment_type', displayName: 'Equipment Type', description: 'Type of manufacturing equipment' },
    type: 'string',
    required: true,
    schema: {
      enum: ['machine_tool', 'conveyor', 'robot', 'furnace', 'press', 'other'],
    },
  })

  equipmentEntity.addProperty({
    info: { name: 'serial_number', displayName: 'Serial Number', description: 'Equipment serial number' },
    type: 'string',
    required: true,
    unique: true,
  })

  equipmentEntity.addProperty({
    info: { name: 'manufacturer', displayName: 'Manufacturer', description: 'Equipment manufacturer' },
    type: 'string',
    required: true,
  })

  equipmentEntity.addProperty({
    info: { name: 'installation_date', displayName: 'Installation Date', description: 'Date equipment was installed' },
    type: 'date',
    required: true,
  })

  addCustomStatusField(equipmentEntity, ['operational', 'maintenance', 'broken', 'retired'], {
    displayName: 'Equipment Status',
    description: 'Current status of the equipment',
  })

  addCreatedAtField(equipmentEntity, { description: 'When the equipment was created' })
  addUpdatedAtField(equipmentEntity, { description: 'When the equipment was last updated' })

  // Maintenance Record Entity
  const maintenanceRecordEntity = equipmentModel.addEntity({
    info: {
      name: 'maintenance_record',
      displayName: 'Maintenance Record',
      description: 'Equipment maintenance and calibration records',
    },
  })

  addIdField(maintenanceRecordEntity, {
    displayName: 'Maintenance ID',
    description: 'Unique identifier for the maintenance record',
  })

  addNameField(maintenanceRecordEntity, { description: 'Maintenance description and work performed' })

  maintenanceRecordEntity.addProperty({
    info: { name: 'maintenance_type', displayName: 'Maintenance Type', description: 'Type of maintenance performed' },
    type: 'string',
    required: true,
    schema: {
      enum: ['preventive', 'corrective', 'calibration', 'inspection'],
    },
  })

  maintenanceRecordEntity.addProperty({
    info: { name: 'technician', displayName: 'Technician', description: 'Name of the maintenance technician' },
    type: 'string',
    required: true,
  })

  maintenanceRecordEntity.addProperty({
    info: { name: 'maintenance_date', displayName: 'Maintenance Date', description: 'Date of maintenance' },
    type: 'date',
    required: true,
  })

  maintenanceRecordEntity.addProperty({
    info: { name: 'duration_hours', displayName: 'Duration Hours', description: 'Maintenance duration in hours' },
    type: 'number',
    required: true,
    schema: {
      minimum: 0,
    },
  })

  addCustomStatusField(maintenanceRecordEntity, ['scheduled', 'in_progress', 'completed', 'cancelled'], {
    displayName: 'Maintenance Status',
    description: 'Current status of the maintenance',
  })

  addCreatedAtField(maintenanceRecordEntity, { description: 'When the maintenance record was created' })

  // MaintenanceRecord-Equipment Association (Many-to-One)
  maintenanceRecordEntity.addAssociation(
    { key: equipmentEntity.key },
    {
      info: { name: 'equipment', displayName: 'Equipment', description: 'Equipment for this maintenance' },
      required: true,
      multiple: false,
    }
  )

  //
  // 6. SUPPLY CHAIN NAMESPACE
  //
  const supplyChain = domain.addNamespace({
    info: {
      name: 'SupplyChain',
      displayName: 'Supply Chain',
      description: 'Suppliers, procurement, and logistics management',
    },
  })

  // Supply Chain Model
  const supplyChainModel = supplyChain.addModel({
    info: {
      name: 'SupplyChain',
      displayName: 'Supply Chain Management',
      description: 'Supplier and procurement management',
    },
  })

  // Supplier Entity
  const supplierEntity = supplyChainModel.addEntity({
    info: {
      name: 'supplier',
      displayName: 'Supplier',
      description: 'Material and component suppliers',
    },
  })

  addIdField(supplierEntity, { displayName: 'Supplier ID', description: 'Unique identifier for the supplier' })

  addNameField(supplierEntity, { description: 'Supplier name and company' })

  addDescriptionField(supplierEntity, { description: 'Supplier description and capabilities' })

  addEmailField(supplierEntity, { description: 'Supplier email address' })

  addPhoneField(supplierEntity, { description: 'Supplier phone number' })

  supplierEntity.addProperty({
    info: { name: 'supplier_type', displayName: 'Supplier Type', description: 'Type of supplier' },
    type: 'string',
    required: true,
    schema: {
      enum: ['raw_materials', 'components', 'services', 'equipment'],
    },
  })

  supplierEntity.addProperty({
    info: { name: 'address', displayName: 'Address', description: 'Supplier address' },
    type: 'string',
    required: true,
  })

  addCustomStatusField(supplierEntity, ['active', 'inactive', 'suspended'], {
    displayName: 'Supplier Status',
    description: 'Current status of the supplier',
  })

  addCreatedAtField(supplierEntity, { description: 'When the supplier was created' })
  addUpdatedAtField(supplierEntity, { description: 'When the supplier was last updated' })

  // Purchase Order Entity
  const purchaseOrderEntity = supplyChainModel.addEntity({
    info: {
      name: 'purchase_order',
      displayName: 'Purchase Order',
      description: 'Purchase orders for materials and components',
    },
  })

  addIdField(purchaseOrderEntity, { displayName: 'PO ID', description: 'Unique identifier for the purchase order' })

  addNameField(purchaseOrderEntity, { description: 'Purchase order description' })

  purchaseOrderEntity.addProperty({
    info: { name: 'order_date', displayName: 'Order Date', description: 'Date the order was placed' },
    type: 'date',
    required: true,
  })

  purchaseOrderEntity.addProperty({
    info: { name: 'expected_delivery', displayName: 'Expected Delivery', description: 'Expected delivery date' },
    type: 'date',
    required: true,
  })

  addCurrencyAmountField(purchaseOrderEntity, 'total_amount', 'Total Amount', {
    description: 'Total purchase order amount',
  })

  addCustomStatusField(purchaseOrderEntity, ['draft', 'sent', 'confirmed', 'received', 'cancelled'], {
    displayName: 'PO Status',
    description: 'Current status of the purchase order',
  })

  addCreatedAtField(purchaseOrderEntity, { description: 'When the purchase order was created' })

  // PurchaseOrder-Supplier Association (Many-to-One)
  purchaseOrderEntity.addAssociation(
    { key: supplierEntity.key },
    {
      info: { name: 'supplier', displayName: 'Supplier', description: 'Supplier for this purchase order' },
      required: true,
      multiple: false,
    }
  )

  // Purchase Order Item Entity
  const purchaseOrderItemEntity = supplyChainModel.addEntity({
    info: {
      name: 'purchase_order_item',
      displayName: 'Purchase Order Item',
      description: 'Individual items in a purchase order',
    },
  })

  addIdField(purchaseOrderItemEntity, {
    displayName: 'Item ID',
    description: 'Unique identifier for the purchase order item',
  })

  purchaseOrderItemEntity.addProperty({
    info: { name: 'quantity', displayName: 'Quantity', description: 'Ordered quantity' },
    type: 'number',
    required: true,
    schema: {
      minimum: 1,
    },
  })

  addCurrencyAmountField(purchaseOrderItemEntity, 'unit_price', 'Unit Price', {
    description: 'Unit price for the item',
  })

  addCurrencyAmountField(purchaseOrderItemEntity, 'total_price', 'Total Price', {
    description: 'Total price for this item',
  })

  addCreatedAtField(purchaseOrderItemEntity, { description: 'When the purchase order item was created' })

  // PurchaseOrderItem-PurchaseOrder Association (Many-to-One)
  purchaseOrderItemEntity.addAssociation(
    { key: purchaseOrderEntity.key },
    {
      info: { name: 'purchase_order', displayName: 'Purchase Order', description: 'Purchase order for this item' },
      required: true,
      multiple: false,
    }
  )

  // PurchaseOrderItem-Product Association (Many-to-One)
  purchaseOrderItemEntity.addAssociation(
    { key: productEntity.key },
    {
      info: { name: 'product', displayName: 'Product', description: 'Product for this purchase order item' },
      required: true,
      multiple: false,
    }
  )

  return domain
}
