const { Op } = require("sequelize");
const dbSAPPS = require("../../helpers/db");
require("dotenv").config();

module.exports = {
	getAll,
};

async function getAll(params) {
	try {
		// filter
		const filterConditions = {};

		if (params.companiesId) {
			filterConditions.company_id = {
				[Op.in]: params.companiesId,
			};
		}

		if (params.company_id) {
			filterConditions.company_id = {
				[Op.in]: params.company_id,
			};
		}

		if (params.employeesId) {
			filterConditions.employee_id = {
				[Op.in]: params.employeesId,
			};
		}

		if (params.employee_id) {
			filterConditions.employee_id = {
				[Op.eq]: params.employee_id,
			};
		}

		if (params.leavetype_id) {
			filterConditions.leavetype_id = {
				[Op.eq]: params.leavetype_id,
			};
		}

		if (params.status) {
			filterConditions.status = {
				[Op.eq]: params.status,
			};
		}

		if (params.from_date && params.to_date) {
			filterConditions.from_date = {
				[Op.lte]: params.from_date,
			};
			filterConditions.to_date = {
				[Op.gte]: params.to_date,
			};
		}

		if (params.year) {
			const date = new Date(params.year, 0, 1);
			filterConditions.from_date = {
				[Op.gte]: date,
			};
			filterConditions.to_date = {
				[Op.lt]: new Date(date.getFullYear() + 1, 0, 1),
			};
		}

		let leaveApplications = [];
		leaveApplications = await dbSAPPS.LeaveApplication.findAll({
			where: filterConditions,
			order: [["submitted_date", "DESC"]],
			include: [
				{
					model: dbSAPPS.Company,
					as: "company",
					attributes: ["id", "display_name", "name"],
				},
				{
					model: dbSAPPS.Employee,
					as: "employee",
					attributes: ["id", "employee_name", "employee_number"],
				},
				{
					model: dbSAPPS.LeaveType,
					as: "leavetype",
					attributes: ["id", "display_name"],
				},
				{
					model: dbSAPPS.Department,
					as: "department",
					attributes: ["id", "display_name"],
				},
				{
					model: dbSAPPS.User,
					as: "modifier", // pastikan alias sesuai relasi di model Attendance
					attributes: ["full_name"],
				},
				{
					model: dbSAPPS.Employee,
					as: "leaveapprover_name", // pastikan alias sesuai relasi di model LeaveApplication
					attributes: ["employee_name"],
				},
			],
		});

		leaveApplications = leaveApplications.map((leaveApplication) => {
			return {
				...leaveApplication.dataValues,
				company_name: leaveApplication?.company?.display_name,
				employee_name: leaveApplication?.employee?.employee_name,
				leavetype_name: leaveApplication?.leavetype?.display_name,
				department_name: leaveApplication?.department?.display_name,
				modifier: leaveApplication?.modifier?.full_name,
				leaveapprover_name: leaveApplication?.leaveapprover_name?.employee_name,
			};
		});

		// leaveApplications = await formatWithApprover(leaveApplications);

		return {
			leaveApplications,
			success: true,
			message: "Loaded successfully!",
		};
	} catch (error) {
		const errorMessage = error?.message || "Failed to load, please try again!";
		return {
			success: false,
			message: errorMessage,
		};
	}
}
