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

module.exports = (sequelize) => {
  const Specialist = sequelize.define(
    "specialist",
    {
      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
      clienttype_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: 'clienttypes',
          key: 'id'
        }
      }
    },
    {
      tableName: "specialists",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Specialist model with other models.
   * @param {object} models - The models to associate with.
   */
  Specialist.associate = (models) => {
    /**
     * Associates the Specialist model with other models.
     * @param {object} models - The models to associate with.
     */
    Specialist.belongsTo(models.ClientType, { foreignKey: "clienttype_id", as: "clienttype" });
    /**
     * Specialist belongs to User (modifier).
     */
    Specialist.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
    /**
     * A Specialist has many SubSpecialists.
     * Defines a one-to-many relationship between Specialist and SubSpecialist.
     * @param {object} models - The models to associate with.
     */
    Specialist.hasMany(models.SubSpecialist, {
      foreignKey: "specialist_id",
      as: "subspecialists",
    });

    Specialist.hasMany(models.Client, {
      foreignKey: "specialist_id",
      as: "specialists",
    });
  };


  return Specialist;
};
