const orderProductItem = {
  type: 'object',
  required: ['product_id', 'quantity', 'weight'],
  properties: {
    // Present when editing an existing order line (so the update knows
    // which row to modify instead of creating a new one); absent for a
    // brand-new line, whether on create or added during an edit.
    id: { type: 'string', nullable: true },
    product_id: { type: 'string' },
    quantity: { type: 'integer', minimum: 1 },
    weight: { type: 'number', minimum: 0 },
    size: { type: 'string', nullable: true },
    jo_no: { type: 'string', nullable: true },
    remark: { type: 'string', nullable: true },
    hallmark_grade_id: { type: 'string', nullable: true },
  },
};

export const createOrderSchema = {
  body: {
    type: 'object',
    required: ['order_date', 'client_id', 'taken_by_employee_id', 'received_through', 'delivery_date', 'products'],
    properties: {
      order_date: { type: 'string', format: 'date' },
      client_id: { type: 'string' },
      taken_by_employee_id: { type: 'string' },
      received_through: { type: 'string', enum: ['Phone', 'WhatsApp', 'Email', 'Direct Visit'] },
      delivery_date: { type: 'string', format: 'date' },
      priority: { type: 'string', enum: ['Normal', 'Urgent'], default: 'Normal' },
      received_by_client_contact: { type: 'string', nullable: true },
      so_number: { type: 'string', nullable: true },
      po_number: { type: 'string', nullable: true },
      po_date: { type: 'string', format: 'date', nullable: true },
      seal_id: { type: 'string', nullable: true },
      remarks: { type: 'string', nullable: true },
      products: { type: 'array', items: orderProductItem, minItems: 1 },
    },
  },
};

export const updateOrderSchema = {
  body: {
    type: 'object',
    properties: {
      order_date: { type: 'string', format: 'date' },
      client_id: { type: 'string' },
      taken_by_employee_id: { type: 'string' },
      received_through: { type: 'string', enum: ['Phone', 'WhatsApp', 'Email', 'Direct Visit'] },
      delivery_date: { type: 'string', format: 'date' },
      priority: { type: 'string', enum: ['Normal', 'Urgent'] },
      received_by_client_contact: { type: 'string', nullable: true },
      so_number: { type: 'string', nullable: true },
      po_number: { type: 'string', nullable: true },
      po_date: { type: 'string', format: 'date', nullable: true },
      seal_id: { type: 'string', nullable: true },
      remarks: { type: 'string', nullable: true },
      products: { type: 'array', items: orderProductItem },
    },
  },
};

export const updateStageSchema = {
  body: {
    type: 'object',
    required: ['completed_stages'],
    properties: {
      // Upper bound isn't fixed at the schema level since the stage list is
      // now a manageable master (see stage.service.ts) — advanceStage() in
      // order.service.ts checks completed_stages against the live stage
      // count instead.
      completed_stages: { type: 'integer', minimum: 0 },
      remarks: { type: 'string', nullable: true },
    },
  },
};

export const updateJoNoSchema = {
  body: {
    type: 'object',
    required: ['jo_no'],
    properties: {
      jo_no: { type: 'string' },
    },
  },
};

export const dispatchSchema = {
  body: {
    type: 'object',
    // dispatched_by_employee_id and dispatched_by_name are each optional at
    // the schema level — order.service.ts enforces that at least one of the
    // two is actually provided (a "select either a registered employee or
    // type a name" rule that a JSON schema can't express cleanly).
    required: ['products', 'dispatch_date'],
    properties: {
      products: {
        type: 'array',
        items: {
          type: 'object',
          required: ['order_product_id', 'quantity'],
          properties: {
            order_product_id: { type: 'string' },
            quantity: { type: 'integer', minimum: 1 },
            weight: { type: 'number', minimum: 0, nullable: true },
          },
        },
      },
      dispatch_date: { type: 'string', format: 'date-time' },
      dispatched_by_employee_id: { type: 'string', nullable: true },
      dispatched_by_name: { type: 'string', nullable: true },
    },
  },
};

export const cancelSchema = {
  body: {
    type: 'object',
    required: ['products', 'cancel_date', 'cancelled_by_employee_id', 'reason'],
    properties: {
      products: {
        type: 'array',
        items: {
          type: 'object',
          required: ['order_product_id', 'quantity', 'weight'],
          properties: {
            order_product_id: { type: 'string' },
            quantity: { type: 'integer', minimum: 1 },
            weight: { type: 'number', minimum: 0 },
          },
        },
      },
      cancel_date: { type: 'string', format: 'date' },
      cancelled_by_employee_id: { type: 'string' },
      reason: { type: 'string', minLength: 1 },
    },
  },
};

export const addCommentSchema = {
  body: {
    type: 'object',
    required: ['comment'],
    properties: {
      comment: { type: 'string', minLength: 1 },
    },
  },
};
