module.exports = (sequelize, DataTypes) => {
  const VendorInvoiceItem = sequelize.define('VendorInvoiceItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    invoice_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'VendorInvoice',
        key: 'id'
      }
    },
    grn_item_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'GoodsReceiptNoteItem',
        key: 'id'
      }
    },
    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
    },
    quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    unit_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false
    },
    discount_percent: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    discount_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    taxable_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    cgst_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    cgst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    sgst_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    sgst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    igst_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    igst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    cess_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    cess_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    line_total: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    hsn_code: {
      type: DataTypes.STRING(8),
      allowNull: true
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'vendor_invoice_item',
    indexes: [
      {
        fields: ['invoice_id']
      },
      {
        fields: ['grn_item_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  VendorInvoiceItem.associate = function(models) {
    VendorInvoiceItem.belongsTo(models.VendorInvoice, { foreignKey: 'invoice_id', as: 'vendorInvoice' });
    VendorInvoiceItem.belongsTo(models.GoodsReceiptNoteItem, { foreignKey: 'grn_item_id', as: 'grnItem' });
    VendorInvoiceItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    VendorInvoiceItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
  };

  return VendorInvoiceItem;
};

