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

module.exports = (sequelize) => {
	const LeaveAllocation = sequelize.define(
		"leaveallocation",
		{
			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 },
			new_leaves_allocated: { type: DataTypes.FLOAT, allowNull: false },
			total_leaves_allocated: { type: DataTypes.FLOAT, allowNull: true },
			carry_forward: { type: DataTypes.BOOLEAN, allowNull: true },
			unused_leaves: { type: DataTypes.FLOAT, allowNull: true },
			used_leaves: { type: DataTypes.FLOAT, allowNull: true },
			expired_leaves: { type: DataTypes.FLOAT, allowNull: true },
			expired: { type: DataTypes.BOOLEAN, allowNull: true },
			leaves_pending_approval: { type: DataTypes.INTEGER, allowNull: true },

			//fk
			leaveperiod_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
			leavetype_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
			employee_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
		},
		{
			tableName: "leaveallocations",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the LeaveAllocation model with other models.
	 * @param {object} models - The models to associate with.
	 */
	LeaveAllocation.associate = (models) => {
		/**
		 * A LeaveAllocation belongs to an Employee.
		 * Defines a many-to-one relationship between LeaveAllocation and Employee.
		 * @param {object} models - The models to associate with.
		 */
		LeaveAllocation.belongsTo(models.Employee, {
			foreignKey: "employee_id",
			as: "employee",
		});

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

		/**
		 * A LeaveAllocation belongs to a LeaveType.
		 * Defines a many-to-one relationship between LeaveAllocation and LeaveType.
		 * @param {object} models - The models to associate with.
		 */
		LeaveAllocation.belongsTo(models.LeaveType, {
			foreignKey: "leavetype_id",
			as: "leavetype",
		});

		/**
		 * A LeaveAllocation belongs to a LeavePeriod.
		 * Defines a many-to-one relationship between LeaveAllocation and LeavePeriod.
		 * @param {object} models - The models to associate with.
		 */
		LeaveAllocation.belongsTo(models.LeavePeriod, {
			foreignKey: "leaveperiod_id",
			as: "leaveperiod",
		});

		LeaveAllocation.hasMany(models.LeaveApplication, {
			foreignKey: "leaveallocation_id",
			as: "leaveapplications",
		});

		LeaveAllocation.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
	};

	return LeaveAllocation;
};
