module.exports = (sequelize, DataTypes) => {
  const SalesQuotationItem = sequelize.define('SalesQuotationItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    sq_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'SalesQuotation',
        key: 'id',
        onDelete: 'CASCADE'
      }
    },
    item_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
    },
    quantity: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: false,
      defaultValue: 0
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    unit_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    discount_percentage: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    discount_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    net_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    tax_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    tax_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    hsn_code: {
      type: DataTypes.STRING(8),
      allowNull: true
    },
    delivery_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'sales_quotation_item',
    indexes: [
      {
        fields: ['sq_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  SalesQuotationItem.associate = function(models) {
    SalesQuotationItem.belongsTo(models.SalesQuotation, { foreignKey: 'sq_id', as: 'salesQuotation' });
    SalesQuotationItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    SalesQuotationItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
  };

  return SalesQuotationItem;
};

