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

module.exports = (sequelize) => {
  const Holiday = sequelize.define(
    "holiday",
    {
      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(155), allowNull: false },
      holiday_date: { type: DataTypes.DATEONLY, allowNull: false },
      is_national_holiday: { type: DataTypes.BOOLEAN, allowNull: false },
      is_weekend_holiday: { type: DataTypes.BOOLEAN, allowNull: false },

      holidaygroup_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'holidaygroups',
          key: 'id',
        },
      },

    },
    {
      tableName: "holidays",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Holiday model with other models.
   * @param {object} models - The models to associate with.
   */
  Holiday.associate = (models) => {
    /**
     * Associates the Holiday model with the HolidayGroup model.
     * Defines a many-to-one relationship between Holiday and HolidayGroup.
     * @param {object} models - The models to associate with.
     */
    Holiday.belongsTo(models.HolidayGroup, { foreignKey: "holidaygroup_id", as: "holidaygroup" });
  };

  return Holiday;
};
