import { Injectable } from "@nestjs/common";
import { Result } from "src/common/errors/result-error.factory";
import { PrismaPostgresService } from "prisma/postgres/prisma-postgres.service";

@Injectable()
export class SchedulePresetDeleteService {
    constructor(private readonly prisma: PrismaPostgresService) { }

    //_________________________________________________________________
    //                              DELETE
    //_________________________________________________________________
    async deletePreset(preset_id: number): Promise<Result<boolean, string>> {
        
        const preset = await this.prisma.schedulePresets.findUnique({
            where: { id: preset_id },
            select: { id: true },
        });
        if (!preset) return { success: false, error: `SCHEDULE_PRESET_NOT_FOUND` };
        
        const updated_employees = await this.prisma.employees.updateMany({
            where: { schedule_preset_id: preset_id },
            data: {
                schedule_preset_id: 0,
            },
        });
        
        await this.prisma.$transaction(async (tx) => {
            await tx.schedulePresetShifts.deleteMany({ where: { preset_id: preset_id } });
            await tx.schedulePresets.delete({ where: { id: preset_id } });
        });
        return { success: true, data: true };
    }
}