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

module.exports = (sequelize) => {
	const LeavePeriod = sequelize.define(
		"leaveperiod",
		{
			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: true },

			// main
			name: { type: DataTypes.STRING(150), allowNull: false },
			display_name: { type: DataTypes.STRING(150), allowNull: false },
			description: { type: DataTypes.TEXT, allowNull: true },

			from_date: { type: DataTypes.DATEONLY, allowNull: false },
			to_date: { type: DataTypes.DATEONLY, allowNull: false },

			// additional
			is_active: { type: DataTypes.BOOLEAN, allowNull: true },

			// fk
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
			holidaylist_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
			},
		},
		{
			tableName: "leaveperiods",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the LeavePeriod model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	LeavePeriod.associate = (models) => {
		LeavePeriod.belongsTo(models.Company, {
			foreignKey: "company_id",
			as: "company",
		});
		LeavePeriod.belongsTo(models.HolidayList, {
			foreignKey: "holidaylist_id",
			as: "holidaylist",
		});

        LeavePeriod.hasMany(models.LeaveAllocation, {
            foreignKey: "leaveperiod_id",
            as: "leaveallocations",
        });
	};

	return LeavePeriod;
};
