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

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

      type: { type: DataTypes.STRING(50), allowNull: false },
      sub_type: { type: DataTypes.STRING(50), allowNull: false },
      category: { type: DataTypes.STRING(50), allowNull: false },
      sub_category: { type: DataTypes.STRING(50), allowNull: false },

      //   fk
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "companies",
          key: "id",
        },
      },
    },
    {
      tableName: "dmslegals",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Applicant model with other models.
   * @param {object} models - The models to associate with.
   */
  DMSLegals.associate = (models) => {
    /**
     * Associates the DMSLegals model with the Company model.
     * A Udk belongs to a Company.
     * @param {object} models - The models to associate with.
     */
    DMSLegals.belongsTo(models.Company, {
      foreignKey: "company_id",
      as: "company",
    });
    // /**
    //  * Associates the DMSLegals model with the dmsLegalChild model.
    //  * A DMSLegal has many DMSLegalChilds.
    //  * @param {object} models - The models to associate with.
    //  */
    DMSLegals.hasMany(models.DMSLegalChild, {
      foreignKey: "dmslegal_id",
      as: "dmslegalchilds",
      onDelete: "CASCADE",
    });
  };

  return DMSLegals;
};
