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

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

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

// type OldExpense = {
//     time_sheet_id: string | null;
//     date: string | null;
//     code: string | null;
//     description: string | null;
//     value: number | null;
//     status: boolean | null;
// }

// export const extractOldExpenses = async () => {
//     const list_of_employees = await prisma.employees.findMany();
//     for (let id = 1; id <= list_of_employees.length+1; id++) {

//         console.log(`Start of Expense migration ***************************************************************`);

//         const new_employee = await findOneNewEmployee(id);
//         console.log(`Employee ${id} found in new DB`);

//         const new_timesheets = await findManyNewTimesheets(new_employee.id);
//         console.log(`New Timesheets found for employee ${id}`);

//         const old_employee_id = await findOneOldEmployee(new_employee);
//         console.log(`Employee ${new_employee.id} found in old DB`);
        
//         const old_timesheets = await findManyOldTimesheets(old_employee_id);
//         console.log(`Timesheets for employee ${old_employee_id}/${new_employee.id} found in old DB`);
        
//         console.log('Start of Expense creation*****************************************************************');
//         for (const old_timesheet of old_timesheets) {
//             if (!old_timesheet.start_date) continue;
//             const new_timesheet = new_timesheets.find((ts) => ts.start_date.getTime() === old_timesheet.start_date!.getTime());
//             if (!new_timesheet) {
//                 console.warn(`No new timesheet matching legacy timesheet ${old_timesheet.id}`);
//                 continue;
//             }
//             const old_expenses = await prisma_legacy.expenses.findMany({
//                 where: { time_sheet_id: old_timesheet.id },
//                 select: {
//                     time_sheet_id: true,
//                     date: true,
//                     code: true,
//                     description: true,
//                     value: true,
//                     status: true,

//                 },
//             });
//             await createManyNewExpenses(new_timesheet.id, old_expenses);
//         }

//     }
//     await prisma_legacy.$disconnect();
//     await prisma.$disconnect();
//     console.log('finished migrating expenses ***************************');
// }

// const findOneNewEmployee = async (id: number): Promise<NewEmployee> => {
//     const new_employee = await prisma.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 old_employee = await prisma_legacy.employees.findFirst({
//         where: {
//             company: new_employee.company_code,
//             employee_number: new_employee.external_payroll_id.toString(),
//         },
//         select: {
//             id: true,
//         },
//     });
//     if (!old_employee) throw new Error(`Old Employee not found`);
//     return old_employee.id;
// }

// const findManyOldTimesheets = async (old_employee_id: string) => {
//     const old_timesheets = await prisma_legacy.time_sheets.findMany({
//         where: { employee_id: old_employee_id },
//         select: { id: true, start_date: true, status: true }
//     });
//     return old_timesheets;
// }

// const findManyNewTimesheets = async (employee_id: number) => {
//     const timesheets = await prisma.timesheets.findMany({
//         where: { employee_id: employee_id },
//         select: { id: true, start_date: true }
//     })
//     return timesheets;
// }

// const createManyNewExpenses = async (timesheet_id: number, old_expenses: OldExpense[]) => {
//     for (const old_expense of old_expenses) {
//         let mileage: number = 0;
//         let amount: number = old_expense.value ?? 0;
//         if (old_expense.code === 'G503') {
//             mileage = old_expense.value!;
//             amount = mileage * 0.72;
//         }
//         if (mileage < 0 || amount < 0) {
//             console.warn(`expense of value less than '0' found`)
//             continue;
//         }

//         if (old_expense.date == null) {
//             console.warn(`Expense date invalid ${old_expense.date}`);
//             continue;
//         }
//         const date = toDateFromString(old_expense.date);

//         if (old_expense.status == null) {
//             console.warn(`status null for legacy expense ${old_expense}`);
//             continue;
//         }

//         if (old_expense.code == null) {
//             console.warn(`Code null for legacy expense ${old_expense.code}`);
//             continue;
//         }
//         const bank_code_id = await findBankCodeIdUsingOldCode(old_expense.code);

//         await prisma.expenses.upsert({
//             where: { unique_ts_id_date_amount_mileage: { timesheet_id: timesheet_id, date, amount, mileage } },
//             update: {
//                 is_approved: old_expense.status,
//             },
//             create: {
//                 date: date,
//                 comment: old_expense.description ?? '',
//                 timesheet_id: timesheet_id, 
//                 bank_code_id: bank_code_id,
//                 amount: amount,
//                 mileage: mileage,
//             }
//         });
//     }
// }

// const findBankCodeIdUsingOldCode = async (code: string): Promise<number> => {
//     const bank_code = await prisma.bankCodes.findFirst({
//         where: { bank_code: code },
//         select: { id: true },
//     });
//     if (!bank_code) throw new Error(`Bank_code_id not found for Code ${code}`)
//     return bank_code.id;
// }   