// import { Injectable, NotFoundException, BadRequestException } from "@nestjs/common";
// import { UpsertLeaveRequestDto, UpsertResult } from "src/time-and-attendance/leave-requests/dtos/upsert-leave-request.dto";
// import { LeaveTypes, LeaveApprovalStatus } from "@prisma/client";
// import { LeaveRequestViewDto } from "src/time-and-attendance/leave-requests/dtos/leave-request-view.dto";
// import { leaveRequestsSelect } from "src/time-and-attendance/utils/selects.utils";
// import { roundToQuarterHour } from "src/common/utils/date-utils";
// import { BankCodesResolver } from "src/time-and-attendance/utils/resolve-bank-type-id.utils";
// import { EmailToIdResolver } from "src/time-and-attendance/utils/resolve-email-id.utils";
// import { SickLeaveService } from "src/time-and-attendance/domains/services/sick-leave.service";
// import { PrismaPostgresService } from "src/prisma/prisma.service";
// import { mapRowToView } from "src/time-and-attendance/leave-requests/mappers/leave-requests.mapper";
// import { normalizeDates, toDateOnly } from "src/time-and-attendance/utils/date-time.utils";



// @Injectable()
// export class SickLeaveRequestsService {
//     constructor(
//         private readonly prisma:       PrismaPostgresService,
//         private readonly sickService:  SickLeaveService,
//         private readonly emailResolver: EmailToIdResolver,
//         private readonly typeResolver: BankCodesResolver,
//     ) {}

//     async create(dto: UpsertLeaveRequestDto): Promise<UpsertResult> {
//         const email       = dto.email.trim();
//         const employee_id = await this.emailResolver.findIdByEmail(email);
//         const bank_code   = await this.typeResolver.findIdAndModifierByType(LeaveTypes.SICK);
//         if(!bank_code) throw new NotFoundException(`bank_code not found`);
        
//         const modifier    = bank_code.modifier ?? 1;
//         const dates       = normalizeDates(dto.dates);
//         if (!dates.length) throw new BadRequestException("Dates array must not be empty");
//         const requested_hours_per_day = dto.requested_hours ?? 8;
        
//         const entries        = dates.map((iso) => ({ iso, date: toDateOnly(iso) }));
//         const reference_date = entries.reduce(
//             (latest, entry) => (entry.date > latest ? entry.date : latest),
//             entries[0].date,
//         );
//         const total_payable_hours = await this.sickService.calculateSickLeavePay(
//             employee_id,
//             reference_date,
//             entries.length,
//             requested_hours_per_day,
//             modifier,
//         );
//         let remaining_payable_hours = roundToQuarterHour(Math.max(0, total_payable_hours));
//         const daily_payable_cap     = roundToQuarterHour(requested_hours_per_day * modifier);

//         const created: LeaveRequestViewDto[] = [];

//         for (const { iso, date } of entries) {
//             const existing = await this.prisma.leaveRequests.findUnique({
//                 where: {
//                     leave_per_employee_date: {
//                         employee_id: employee_id,
//                         leave_type:  LeaveTypes.SICK,
//                         date,
//                     },
//                 },
//                 select: { id: true },
//             });
//             if (existing) {
//                 throw new BadRequestException(`Sick request already exists for ${iso}`);
//             }

//             const payable           = Math.min(remaining_payable_hours, daily_payable_cap);
//             const payable_rounded   = roundToQuarterHour(Math.max(0, payable));
//             remaining_payable_hours = roundToQuarterHour(
//                 Math.max(0, remaining_payable_hours - payable_rounded),
//             );

//             const row = await this.prisma.leaveRequests.create({
//                 data: {
//                     employee_id:     employee_id,
//                     bank_code_id:    bank_code.id,
//                     leave_type:      LeaveTypes.SICK,
//                     comment:         dto.comment ?? "",
//                     requested_hours: requested_hours_per_day,
//                     payable_hours:   payable_rounded,
//                     approval_status: dto.approval_status ?? LeaveApprovalStatus.PENDING,
//                     date,
//                 },
//                 select: leaveRequestsSelect,
//             });

//             const hours = Number(row.payable_hours ?? row.requested_hours ?? 0);
//             if (row.approval_status === LeaveApprovalStatus.APPROVED) {
//                 // await this.leaveUtils.syncShift(email, employee_id, iso, hours,LeaveTypes.SICK, row.comment);
//             }

//             created.push({ ...mapRowToView(row), action: "create" });
//         }

//         return { action: "create", leave_requests: created };
//     }
// }
