// import { PrismaClient as Prisma } from "@prisma/client";
// import { PrismaClient as PrismaLegacy } from "@prisma/client-legacy"
// import { toStringFromDate } from "src/common/utils/date-utils";

// type NewEmployee = {
//     id: number;
//     company_code: number;
//     external_payroll_id: number;
// }

// type OldTimesheets = {
//     id: string;
//     start_date: Date | null;
//     status: boolean | null;
// }

// const prisma_legacy = new PrismaLegacy({});
// const prisma_new = new Prisma({});

// export const extractOldTimesheets = async () => {
//     const list_of_employees = await prisma_new.employees.findMany();

//     for (let id = 1; id <= list_of_employees.length; id++) {
//         const new_employee = await findOneNewEmployee(id);

//         const old_employee_id = await findOneOldEmployee(new_employee);

//         const old_timesheets = await findManyOldTimesheets(old_employee_id);

//         await createManyNewTimesheets(old_timesheets, new_employee);
//         console.log(`${old_timesheets.length} New Timesheets created in new DB for employee ${new_employee.id}`);
//     }

//     await prisma_legacy.$disconnect();
//     await prisma_new.$disconnect();
//     console.log('migration of timesheets finished')
// }

// const findOneNewEmployee = async (id: number): Promise<NewEmployee> => {
//     const new_employee = await prisma_new.employees.findUnique({
//         where: { id: id },
//         select: {
//             id: true,
//             company_code: true,
//             external_payroll_id: true,
//         },
//     });
//     if (!new_employee) throw new Error(`New Employee with id ${id} not found`)
//     return new_employee;
// }

// const findOneOldEmployee = async (new_employee: NewEmployee): Promise<string> => {
//     const employee_number = new_employee.external_payroll_id.toString()
//     const old_employee = await prisma_legacy.employees.findFirst({
//         where: {
//             company: new_employee.company_code,
//             employee_number: employee_number,
//         },
//         select: {
//             id: true,
//         },
//     });
//     if (!old_employee) throw new Error(`Old Employee not found`);
//     return old_employee.id;
// }

// const findManyOldTimesheets = async (old_employee_id: string) => {
//     console.log('trying to find old timesheets ...');
//     const old_timesheets = await prisma_legacy.time_sheets.findMany({
//         where: { employee_id: old_employee_id },
//         select: { id: true, start_date: true, status: true }
//     });
//     console.log(old_timesheets.length, 'old timesheets found')
//     if (!old_timesheets) throw new Error(`old Timesheets not found for employee_id ${old_employee_id}`)
//     return old_timesheets;
// }

// const createManyNewTimesheets = async (old_timesheets: OldTimesheets[], new_employee: NewEmployee) => {
//     console.log(old_timesheets.length, ' timesheets ready for creation')
//     for (const timesheet of old_timesheets) {
//         if (timesheet.start_date == null) {
//             console.warn(`start_date invalid for legacy timesheet ${timesheet.id}`);
//             continue;
//         }
//         if (timesheet.status == null) {
//             console.warn(`status null for legacy timesheet ${timesheet.id}`);
//             continue;
//         }

//         try {
//             console.log(`Timesheet with start_date: ${toStringFromDate(timesheet.start_date!)} for employee ${new_employee.id} created`)
//             const new_timesheet = await prisma_new.timesheets.create({
//                 data: {
//                     employee_id: new_employee.id,
//                     start_date: timesheet.start_date,
//                     is_approved: timesheet.status,
//                 },
//             });
//             if (!new_timesheet) throw new Error(
//                 `Timesheet with start_date: ${toStringFromDate(timesheet.start_date!)} for employee ${new_employee.id} not created`
//             );
//         } catch (error) {
//             throw new Error('An error occured during timesheets creation', error);
//         }
//     }
// }

// // extractOldTimesheets()
// //     .then(() => {
// //         console.log("Migration completed");
// //     })
// //     .catch((error) => {
// //         console.error("Migration failed:", error);
// //     })
// //     .finally(async () => {
// //         await prisma_legacy.$disconnect();
// //         await prisma_new.$disconnect();
// //     });