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

module.exports = (sequelize) => {
  const GimmickReceiver = sequelize.define(
    "gimmickreceiver",
    {
      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: false },
      owner: { type: DataTypes.INTEGER, allowNull: false },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      // fk
      bankaccount_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: "bankaccounts", key: "id" },
      },
      gimmickreq_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: "gimmickrequests", key: "id" },
      },
      type: { type: DataTypes.STRING, allowNull: false },
    },
    {
      tableName: "gimmickreceivers",
      freezeTableName: true,
    }
  );

  /**
   * Associates the GimmickReceiver model with other models.
   * @param {object} models - The models to associate with.
   * @description
   */
  GimmickReceiver.associate = (models) => {
    GimmickReceiver.belongsTo(models.GimmickRequest, {
      foreignKey: "gimmickreq_id",
      as: "gimmickrequest",
      onDelete: "CASCADE",
    });
    GimmickReceiver.belongsTo(models.BankAccount, {
      foreignKey: "bankaccount_id",
      as: "bankaccount",
      onDelete: "CASCADE",
    });
  };
  return GimmickReceiver;
};
