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

module.exports = (sequelize) => {
  const Major = sequelize.define(
    "major",
    {
      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(100), allowNull: false },

      // fk
      faculty_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "faculties",
          key: "id",
        },
      },
    },
    {
      tableName: "majors",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Major model with other models.
   * @param {object} models - The models to associate with.
   */
  Major.associate = (models) => {
    /**
     * Associates the Major model with the Faculty model.
     * @param {object} models - The models to associate with.
     */
    Major.belongsTo(models.Faculty, { foreignKey: "faculty_id", as: "faculty" });
  };

  return Major;
};
