module.exports = (sequelize, DataTypes) => {
  const VendorQuotationRequestItem = sequelize.define('VendorQuotationRequestItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    vqr_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'VendorQuotationRequest',
        key: 'id'
      }
    },
    rfq_item_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'RequestForQuotationItem',
        key: 'id'
      }
    },
    line_number: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    // Vendor response fields
    quoted_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: true
    },
    quoted_delivery_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    vendor_remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'vendor_quotation_request_item',
    // This table was migrated with camelCase timestamp columns (createdAt/updatedAt).
    // Override the global Sequelize setting (underscored: true) for this model only.
    timestamps: true,
    underscored: false,
    createdAt: 'createdAt',
    updatedAt: 'updatedAt',
    indexes: [
      {
        fields: ['vqr_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  VendorQuotationRequestItem.associate = function(models) {
    VendorQuotationRequestItem.belongsTo(models.VendorQuotationRequest, { foreignKey: 'vqr_id', as: 'vendorQuotationRequest' });
    VendorQuotationRequestItem.belongsTo(models.RequestForQuotationItem, { foreignKey: 'rfq_item_id', as: 'rfqItem' });
    VendorQuotationRequestItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    VendorQuotationRequestItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
  };

  return VendorQuotationRequestItem;
};

