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

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

			// 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 },
			total_holidays: { type: DataTypes.INTEGER, allowNull: false },
			weekly_off: { type: DataTypes.BOOLEAN, allowNull: true },

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

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

	return HolidayList;
};
