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

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

      display_name: { type: DataTypes.STRING(100), allowNull: false },
      code: { type: DataTypes.STRING(100), allowNull: true },

      // fk
      specialist_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "specialists",
          key: "id",
        },
      },
    },
    {
      tableName: "subspecialists",
      freezeTableName: true,
    }
  );

  /**
   * Associates the SubSpecialist model with other models.
   * @param {object} models - The models to associate with.
   */
  SubSpecialist.associate = (models) => {
    /**
     * Associates the SubSpecialist model with the Specialist model.
     * Defines a many-to-one relationship between SubSpecialist and Specialist.
     * @param {object} models - The models to associate with.
     */
    SubSpecialist.belongsTo(models.Specialist, {
      foreignKey: "specialist_id",
      as: "specialist"
    });
    SubSpecialist.hasMany(models.Client, {
      foreignKey: "subspecialist_id",
      as: "subspecialists",
    });
  };

  return SubSpecialist;
};
