const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const FseItem = sequelize.define(
    "fseitem",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: true },
      owner: { type: DataTypes.INTEGER, allowNull: true },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      // fk
      fse_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "fses",
          key: "id",
        },
      },
      customer_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "customerlists",
          key: "id",
        },
      },
      item_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "items",
          key: "id",
        },
      },

      sku_code: { type: DataTypes.STRING(100), allowNull: true },
      quantity: { type: DataTypes.INTEGER, allowNull: true },
     
    },
    {
      tableName: "fseitems",
      freezeTableName: true,
    }
  );

 FseItem.associate = function(models) {
   FseItem.belongsTo(models.Fse, { foreignKey: "fse_id", as: "fse", onDelete: "CASCADE" });
   FseItem.belongsTo(models.CustomerList, { foreignKey: "customer_id", as: "customer" });
   FseItem.belongsTo(models.Item, { foreignKey: "item_id", as: "item" }); 
 }
  return FseItem;
};
