
const cron = require("node-cron");
const { BookingAppointment } = require('../../models/booking');
const moment = require('moment');

exports.initializeCronTasks = async () => 
{
    try
    {
        cron.schedule("* * * * *", async () => {
            // remove every expire booking requests 
            const currentDate = new Date();
            const currentTime = moment(currentDate).format('HH:mm');
            const todayStartDate = currentDate;
            todayStartDate.setHours(0,0);
            console.log("---------- current Date -------" , currentDate.toISOString() , "\n" , "---------- current time -------" , currentTime    )
            console.log(await BookingAppointment.find({ $and: [
                { reschedule_date: { $eq: null } },
                { booking_date: { $lte: currentDate } },
                {
                    slot: { $regex: / - / }, // Ensures format "07:00 - 19:00"
                    $expr: {
                        $lte: [
                            { $arrayElemAt: [{ $split: ["$slot", " - "] }, 1] }, // Extract "19:00"
                            currentTime
                        ]
                    }
                }
            ] , status : "PENDING"}));
    
            let filteredQuery = {
                $or: [
                   
                    {
                        $and: [
                            { reschedule_date: { $eq: null } },
                            { booking_date: { $lte: currentDate } },
                            {
                                slot: {
                                    $not: { $regex: " - " } // Exclude slots with a range
                                }
                            },
                            {
                                slot: {
                                    $lte: moment(currentDate).format('HH:mm') // Comparing current time with slot time
                                }
                            }
                        ]
                    },
                   
                    {
                        $and: [
                            { reschedule_date: { $lte: currentDate } },
                            {
                                slot: {
                                    $not: { $regex: " - " } // Exclude slots with a range
                                }
                            },
                            {
                                reschedule_slot: {
                                    $lte: moment(currentDate).format('HH:mm') // Comparing current time with reschedule slot time
                                }
                            }
                        ]
                    },
                    // full time end

                    
                    {
                        $and: [
                            { reschedule_date: { $eq: null } },
                            { booking_date: { $lte: currentDate } },
                            {
                                slot: { $regex: / - / }, // Ensures format "07:00 - 19:00"
                                $expr: {
                                    $lte: [
                                        { $arrayElemAt: [{ $split: ["$slot", " - "] }, 1] }, // Extract "19:00"
                                        currentTime
                                    ]
                                }
                            }
                        ]
                    },
                    {
                        $and: [
                            { reschedule_date: { $lte: currentDate } },
                            {
                                
                                reschedule_slot: { $regex: / - / }, // Ensures format "07:00 - 19:00"
                                $expr: {
                                    $lte: [
                                        { $arrayElemAt: [{ $split: ["$slot", " - "] }, 1] }, // Extract "19:00"
                                        currentTime
                                    ]
                                }
                                
                            }
                        ]
                    },
                    

                    //part time end 
                ],
                status: "PENDING"
            };
            console.log(await BookingAppointment.find(filteredQuery))
            const result = await BookingAppointment.updateMany(
                filteredQuery,
                { 
                    $set: { 
                        status: "REJECTED", 
                        rejected_reason: "Request is expired" 
                    } 
                }
            );
          });

        console.log("Cron initialized successfully.");
    }
    catch(error)
    {
        console.log("Something went wrong with cron !");
        throw new Error(error);
    }
}