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

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

      name: { type: DataTypes.STRING(100), allowNull: false },
      display_name: { type: DataTypes.STRING(150), allowNull: false },
    },
    {
      tableName: "faculties",
    },
    {
      freezeTableName: true,
    }
  );

  /**
   * Associates the Faculty model with other models.
   * @param {object} models - The models to associate with.
   */
  Faculty.associate = (models) => {
    /**
     * Associates the Faculty model with the Major model.
     * Defines a one-to-many relationship between Faculty and Major.
     * When a Faculty is deleted, all associated Majors are also deleted (cascade delete).
     * @param {object} models - The models to associate with.
     */
    Faculty.hasMany(models.Major, { foreignKey: "faculty_id", as: "majors", onDelete: "CASCADE" });

    /**
     * Associates the Faculty model with the EmployeeRequest model.
     * Defines a one-to-many relationship between Faculty and EmployeeRequest.
     * When a Faculty is deleted, all associated EmployeeRequests are also deleted (cascade delete).
     * @param {object} models - The models to associate with.
     */
  };

  return Faculty;
};
