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

module.exports = (sequelize) => {
    const HolidayGroup = sequelize.define(
        "holidaygroup",
        {
            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 },
            code: { type: DataTypes.STRING(255), allowNull: false },

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

    /**
     * Define associations for the HolidayGroup model.
     * @param {object} models - The models to associate with.
     */
    HolidayGroup.associate = (models) => {
        /**
         * Associates the HolidayGroup model with the Holiday model.
         * Defines a one-to-many relationship between HolidayGroup and Holiday.
         * @param {object} models - The models to associate with.
         */
        HolidayGroup.hasMany(models.Holiday, { foreignKey: "holidaygroup_id", as: "holidays", 
            onDelete: 'CASCADE'});

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

        /**
         * Associates the HolidayGroup model with the ShiftType model.
         * Defines a one-to-many relationship between HolidayGroup and ShiftType.
         * @param {object} models - The models to associate with.
         */
        HolidayGroup.hasMany(models.ShiftType, { foreignKey: "holidaygroup_id", as: "shifttypes" });
    };

    return HolidayGroup;
};
