module.exports = (sequelize, DataTypes) => {
  const CustomerPOItem = sequelize.define('CustomerPOItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    po_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'CustomerPO',
        key: 'id'
      }
    },
    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
    },
    delivered_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_type: {
      type: DataTypes.ENUM('GST', 'IGST'),
      allowNull: true,
      defaultValue: null,
      comment: 'GST for same state (CGST+SGST), IGST for different state'
    },
    tax_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    tax_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
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    hsn_code: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    storage_location_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'StorageLocation',
        key: 'id'
      }
    },
    delivery_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    }
  }, {
    tableName: 'customer_po_item',
    indexes: [
      {
        fields: ['po_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  CustomerPOItem.associate = function(models) {
    CustomerPOItem.belongsTo(models.CustomerPO, { foreignKey: 'po_id', as: 'customerPO' });
    CustomerPOItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    CustomerPOItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    CustomerPOItem.belongsTo(models.StorageLocation, { foreignKey: 'storage_location_id', as: 'storageLocation' });
  };

  return CustomerPOItem;
};

