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

module.exports = (sequelize) => {
  const BankAccount = sequelize.define(
    "bankaccount",
    {
      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 },

      // main
      name: { type: DataTypes.STRING, allowNull: false },
      display_name: { type: DataTypes.STRING, allowNull: false },
      account_number: { type: DataTypes.STRING, allowNull: false },
      branch: { type: DataTypes.STRING, allowNull: true },
      description: { type: DataTypes.TEXT, allowNull: true },
      type: { type: DataTypes.STRING, allowNull: false },

      is_verified: { type: DataTypes.BOOLEAN, allowNull: true },

      // fk
      bank_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      employee_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      client_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
    },
    {
      tableName: "bankaccounts",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Drug model with other models.
   * @param {object} models - The models to associate with.
   * @description
   */
  BankAccount.associate = (models) => {
    BankAccount.belongsTo(models.Company, {
      foreignKey: "company_id",
      as: "company",
    });
    BankAccount.belongsTo(models.Employee, {
      foreignKey: "employee_id",
      as: "employee",
    });
    BankAccount.belongsTo(models.Client, {
      foreignKey: "client_id",
      as: "client",
    });
    BankAccount.belongsTo(models.Bank, { foreignKey: "bank_id", as: "bank" });
  };

  return BankAccount;
};
