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

module.exports = (sequelize) => {
  const ClientType = sequelize.define(
    "clienttype",
    {
      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
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "companies",
          key: "id",
        },
      },
    },
    {
      tableName: "clienttypes",
      freezeTableName: true,
    }
  );

  /**
   * Associates the ClientType model with other models.
   * @param {object} models - The models to associate with.
   */
  ClientType.associate = (models) => {
    /**
     * A ClientType belongs to a Company.
     * Defines a many-to-one relationship between ClientType and Company.
     * @param {object} models - The models to associate with.
     */
    ClientType.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

    /**
     * A ClientType has many Specialists.
     * Defines a one-to-many relationship between ClientType and Specialist.
     * @param {object} models - The models to associate with.
     */
    ClientType.hasMany(models.Specialist, { foreignKey: "clienttype_id", as: "specialists" });

    /**
     * A ClientType has many ClientSubtypes.
     * Defines a one-to-many relationship between ClientType and ClientSubtype.
     * @param {object} models - The models to associate with.
     */
    ClientType.hasMany(models.ClientSubtype, { foreignKey: "clienttype_id", as: "clientsubtypes" });

    /**
     * A ClientType has many Clients.
     * Defines a one-to-many relationship between ClientType and Client.
     * @param {object} models - The models to associate with.
     */
    ClientType.hasMany(models.Client, { foreignKey: "clienttype_id", as: "clienttypes" });
  };

  return ClientType;
};
