{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://schemas.bakery-ia.com/demo/production/batch/v1",
  "type": "object",
  "title": "ProductionBatch",
  "description": "Production batch for demo cloning",
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid",
      "description": "Unique batch identifier"
    },
    "tenant_id": {
      "type": "string",
      "format": "uuid",
      "description": "Tenant owner (replaced during cloning)"
    },
    "batch_number": {
      "type": "string",
      "pattern": "^BATCH-[0-9]{8}-[A-Z0-9]{6}$",
      "description": "Unique batch code"
    },
    "product_id": {
      "type": "string",
      "format": "uuid",
      "description": "Cross-service ref to inventory.Ingredient (type=FINISHED_PRODUCT)"
    },
    "product_name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 255
    },
    "recipe_id": {
      "type": ["string", "null"],
      "format": "uuid",
      "description": "Cross-service ref to recipes.Recipe"
    },
    "planned_start_time": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601 datetime with timezone"
    },
    "planned_end_time": {
      "type": "string",
      "format": "date-time"
    },
    "planned_quantity": {
      "type": "number",
      "minimum": 0.1,
      "description": "Quantity in product's unit of measure"
    },
    "planned_duration_minutes": {
      "type": "integer",
      "minimum": 1
    },
    "actual_start_time": {
      "type": ["string", "null"],
      "format": "date-time",
      "description": "Set when status becomes IN_PROGRESS"
    },
    "actual_end_time": {
      "type": ["string", "null"],
      "format": "date-time",
      "description": "Set when status becomes COMPLETED"
    },
    "actual_quantity": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "actual_duration_minutes": {
      "type": ["integer", "null"],
      "minimum": 1
    },
    "status": {
      "type": "string",
      "enum": ["PENDING", "IN_PROGRESS", "COMPLETED", "CANCELLED", "ON_HOLD", "QUALITY_CHECK", "FAILED"],
      "default": "PENDING"
    },
    "priority": {
      "type": "string",
      "enum": ["LOW", "MEDIUM", "HIGH", "URGENT"],
      "default": "MEDIUM"
    },
    "current_process_stage": {
      "type": ["string", "null"],
      "enum": ["mixing", "proofing", "shaping", "baking", "cooling", "packaging", "finishing", null]
    },
    "process_stage_history": {
      "type": ["array", "null"],
      "items": {
        "type": "object",
        "properties": {
          "stage": {"type": "string"},
          "timestamp": {"type": "string", "format": "date-time"}
        }
      }
    },
    "pending_quality_checks": {
      "type": ["array", "null"],
      "items": {"type": "string"}
    },
    "completed_quality_checks": {
      "type": ["array", "null"],
      "items": {
        "type": "object",
        "properties": {
          "stage": {"type": "string"},
          "score": {"type": "number"},
          "timestamp": {"type": "string", "format": "date-time"}
        }
      }
    },
    "estimated_cost": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "actual_cost": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "labor_cost": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "material_cost": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "overhead_cost": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "yield_percentage": {
      "type": ["number", "null"],
      "minimum": 0,
      "maximum": 100
    },
    "quality_score": {
      "type": ["number", "null"],
      "minimum": 0,
      "maximum": 10
    },
    "waste_quantity": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "defect_quantity": {
      "type": ["number", "null"],
      "minimum": 0
    },
    "waste_defect_type": {
      "type": ["string", "null"],
      "enum": ["burnt", "misshapen", "underproofed", "temperature_issues", "expired", null]
    },
    "equipment_used": {
      "type": ["array", "null"],
      "items": {"type": "string", "format": "uuid"},
      "minItems": 1,
      "description": "Array of Equipment IDs"
    },
    "staff_assigned": {
      "type": ["array", "null"],
      "items": {"type": "string", "format": "uuid"}
    },
    "station_id": {
      "type": ["string", "null"],
      "maxLength": 50
    },
    "order_id": {
      "type": ["string", "null"],
      "format": "uuid",
      "description": "Cross-service ref to orders.CustomerOrder"
    },
    "forecast_id": {
      "type": ["string", "null"],
      "format": "uuid",
      "description": "Cross-service ref to forecasting.Forecast"
    },
    "is_rush_order": {
      "type": "boolean",
      "default": false
    },
    "is_special_recipe": {
      "type": "boolean",
      "default": false
    },
    "is_ai_assisted": {
      "type": "boolean",
      "default": false
    },
    "production_notes": {
      "type": ["string", "null"]
    },
    "quality_notes": {
      "type": ["string", "null"]
    },
    "delay_reason": {
      "type": ["string", "null"],
      "maxLength": 255
    },
    "cancellation_reason": {
      "type": ["string", "null"],
      "maxLength": 255
    },
    "reasoning_data": {
      "type": ["object", "null"],
      "properties": {
        "type": {
          "type": "string",
          "enum": ["forecast_demand", "customer_order", "stock_replenishment"]
        },
        "parameters": {"type": "object"},
        "urgency": {
          "type": "object",
          "properties": {
            "level": {"type": "string"},
            "ready_by_time": {"type": "string"},
            "customer_commitment": {"type": "boolean"}
          }
        },
        "metadata": {"type": "object"}
      }
    },
    "created_at": {
      "type": ["string", "null"],
      "format": "date-time"
    },
    "updated_at": {
      "type": ["string", "null"],
      "format": "date-time"
    },
    "completed_at": {
      "type": ["string", "null"],
      "format": "date-time"
    }
  },
  "required": [
    "id", "tenant_id", "batch_number", "product_id", "product_name",
    "planned_start_time", "planned_end_time", "planned_quantity",
    "planned_duration_minutes", "status", "priority"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {"status": {"const": "IN_PROGRESS"}}
      },
      "then": {
        "required": ["actual_start_time"]
      }
    },
    {
      "if": {
        "properties": {"status": {"const": "COMPLETED"}}
      },
      "then": {
        "required": ["actual_start_time", "actual_end_time", "actual_quantity"]
      }
    }
  ]
}