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

module.exports = (sequelize) => {
	const HolidayListItem = sequelize.define(
		"holidaylistitem",
		{
			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
			holiday_date: { type: DataTypes.DATEONLY, allowNull: false },
			display_name: { type: DataTypes.STRING(255), allowNull: false },
			description: { type: DataTypes.STRING(255), allowNull: true },
			weekly_off: { type: DataTypes.BOOLEAN, allowNull: false },
			is_national_holiday: { type: DataTypes.BOOLEAN, allowNull: false },
			is_joint_holiday: { type: DataTypes.BOOLEAN, allowNull: false },
			is_weekend_holiday: { type: DataTypes.BOOLEAN, allowNull: false },

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

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

	return HolidayListItem;
};
