module.exports = (sequelize, DataTypes) => {
  const GateEntryItem = sequelize.define('GateEntryItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    gate_entry_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'GateEntry',
        key: 'id'
      }
    },
    po_item_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseOrderItem',
        key: 'id'
      },
      comment: 'Reference to Purchase Order Item'
    },
    line_number: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    po_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      comment: 'Quantity as per Purchase Order'
    },
    received_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      comment: 'Quantity actually received at gate'
    },
    excess_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0,
      comment: 'Excess quantity beyond PO quantity, if any'
    },
    balance_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      comment: 'Balance quantity = PO quantity - Received quantity'
    },
    receiving_status: {
      type: DataTypes.ENUM('NOT_RECEIVED', 'PARTIAL_RECEIVED', 'FULLY_RECEIVED'),
      allowNull: true,
      comment: 'Receiving status: NOT_RECEIVED, PARTIAL_RECEIVED, FULLY_RECEIVED'
    },
    accepted_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      comment: 'Quantity accepted after QC check (if QC required)'
    },
    rejected_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0,
      comment: 'Quantity rejected by QC'
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    unit_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    batch_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    condition: {
      type: DataTypes.ENUM('GOOD', 'DAMAGED', 'PARTIAL_DAMAGE'),
      allowNull: true,
      defaultValue: 'GOOD',
      comment: 'Condition of material at gate entry'
    },
    qc_status: {
      type: DataTypes.ENUM('PENDING', 'APPROVED', 'REJECTED', 'NOT_REQUIRED'),
      allowNull: false,
      defaultValue: 'NOT_REQUIRED',
      comment: 'QC status: PENDING, APPROVED, REJECTED, NOT_REQUIRED'
    },
    qc_remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'QC remarks/observations'
    },
    qc_checked_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    qc_checked_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    grn_item_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'GoodsReceiptNoteItem',
        key: 'id'
      },
      comment: 'GRN Item created from this gate entry item'
    },
    line_weight_kg: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      comment: 'Total line weight in KG at transaction time'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'gate_entry_item',
    indexes: [
      {
        fields: ['gate_entry_id']
      },
      {
        fields: ['po_item_id']
      },
      {
        fields: ['material_id']
      },
      {
        fields: ['qc_status']
      },
      {
        fields: ['grn_item_id']
      }
    ]
  });

  GateEntryItem.associate = function (models) {
    GateEntryItem.belongsTo(models.GateEntry, { foreignKey: 'gate_entry_id', as: 'gateEntry' });
    GateEntryItem.belongsTo(models.PurchaseOrderItem, { foreignKey: 'po_item_id', as: 'purchaseOrderItem' });
    GateEntryItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    GateEntryItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    GateEntryItem.belongsTo(models.GoodsReceiptNoteItem, { foreignKey: 'grn_item_id', as: 'grnItem' });
    GateEntryItem.belongsTo(models.User, { foreignKey: 'qc_checked_by', as: 'qcCheckedByUser' });
  };

  return GateEntryItem;
};

