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

module.exports = {
	getToday,
};

/**
 * Format an array of employee checkins by appending a territory field to each company.
 */
const formatWithTerritory = async (employeeCheckins) => {
	return Promise.all(
		employeeCheckins.map(async (employeeCheckin) => {
			const employeeTerritory = await dbSAPPS.EmployeeTerritory.findOne({
				where: {
					employee_id: employeeCheckin.employee_id,
				},
				attributes: ["territory_id"],
				include: [
					{
						model: dbSAPPS.Territory,
						as: "territory",
						attributes: ["display_name"],
					},
				],
			});

			return {
				...employeeCheckin,
				territory_id: employeeTerritory?.territory_id,
				territory_name: employeeTerritory?.territory?.display_name,
			};
		})
	);
};

async function getToday(params) {
	try {
		const filterConditions = {};

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

		if (params.employeesId && params.employeesId.length > 0) {
			filterConditions.employee_id = {
				[Op.in]: params.employeesId.map((item) => parseInt(item)),
			};
		}

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

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

		let employeeCheckins = [];
		employeeCheckins = await dbSAPPS.EmployeeCheckin.findAll({
			where: filterConditions,
			order: [["modified", "DESC"]],
			include: [
				{
					model: dbSAPPS.Employee,
					as: "employee",
					attributes: ["id", "employee_name", "employee_number", "shifttype_id", "company_id"],
					include: [
						{
							model: dbSAPPS.Designation,
							as: "designation",
							attributes: ["id", "name", "display_name"],
						},
						{
							model: dbSAPPS.Department,
							as: "department",
							attributes: ["id", "name", "display_name"],
						},
					],
				},
				{
					model: dbSAPPS.Company,
					as: "company",
					attributes: ["id", "display_name", "name"],
				},
				{
					model: dbSAPPS.ShiftType,
					as: "shifttype",
					attributes: ["id", "name", "display_name", "start_time", "end_time", "late_time", "early_time"],
				},
			],
		});

		employeeCheckins = employeeCheckins.map((employeeCheckin) => {
			return {
				...employeeCheckin.dataValues,
				company_name: employeeCheckin?.company?.display_name,
				employee_name: employeeCheckin?.employee?.employee_name,
				shifttype_name: employeeCheckin?.shifttype?.display_name,
				designation_id: employeeCheckin?.employee?.designation_id,
				designation_name: employeeCheckin?.employee?.designation?.display_name,
				department_id: employeeCheckin?.employee?.department_id,
				department_name: employeeCheckin?.employee?.department?.display_name,
			};
		});

		employeeCheckins = await formatWithTerritory(employeeCheckins);

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