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

module.exports = (sequelize) => {
  const ClientSubtype = sequelize.define(
    "clientsubtype",
    {
      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: "clientsubtypes",
      freezeTableName: true,
    }
  );

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

    /**
     * A ClientSubtype belongs to a User (modifier).
     */
    ClientSubtype.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });

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

  return ClientSubtype;
};
