import dbConnect from "@/database/dbConnect";
import { NextResponse } from "next/server";
import userModel from "@/database/models/userModel";
import memberModel from "@/database/models/memberModel";
import servicesModel from "@/database/models/serviceModel";
import statisticsModel from "@/database/models/statisticsModel";
import workerModel from "@/database/models/workerModel";
import { Types } from 'mongoose'
import { IStatisticsSchema } from '@/types/IDB'

// set the revalidate variable
export const revalidate = 5;
// GET METHOD
export async function GET(req:Request)
{
    try{
        // connect to the db
        dbConnect();
        // get needed docs
        let membersCount : number = await memberModel.count({})
        let activeMembersCount : number = await memberModel.count({active: true})
        let disActiveMembersCount : number = await memberModel.count({active: false})
        let activeSubscriptionsCount : number = await memberModel.count({planExpAt_unix: {$gt : Math.floor(new Date().getTime() / 1000)}})        
        let expiredSoonSubscriptionsCount : number = await memberModel.count({planExpAt_unix: {$gt : Math.floor(new Date().getTime() / 1000) + 259200}})
        let expiredSubscriptionsCount : number = await memberModel.count({planExpAt_unix: {$lte : Math.floor(new Date().getTime() / 1000)}}) 
        let servicesCount : number = await servicesModel.count({})
        let activeServicesCount : number = await servicesModel.count({status: "active"})
        let disActiveServicesCount : number =  servicesCount - activeServicesCount   //await servicesModel.count({status: "inactive"})

        let servicesNameAndSubscribers :
            {
                _id: Types.ObjectId,
                name: string,
                totalSubscribers: number
            }[]
        = await servicesModel.find({} , {name: 1 , totalSubscribers : 1})
        let workersNameAndJobType : { count: number , jobType: string }[] = await workerModel.aggregate([
            {
                $group: {
                    _id: "$jobType",
                    count: { $sum: 1 }
                }
            },
            {
                $project: {
                    jobType: "$_id",
                    count: 1,
                    _id: 0
                }
            }
        ])
        let membersGendre : { count: number , gendre: string }[] = await memberModel.aggregate([
            {
                $group: {
                    _id: "$gendre",
                    count: { $sum: 1 }
                }
            },
            {
                $project: {
                    gendre: "$_id",
                    count: 1,
                    _id: 0
                }
            }
        ])
        // get members general overview
        let membersGeneralOverview : {
            thisWeek : {
                'year': number,
                'month': number,
                'week': number,
                'days': {
                    'sat': {
                        'totalMembers': number,
                        'totalActiveSubs': number,
                        'totalUnActiveSubs': number,
                        'totalMansMembers': number,
                        'totalGirlsMembers': number,
                    },
                    'sun': {
                        'totalMembers': number,
                        'totalActiveSubs': number,
                        'totalUnActiveSubs': number,
                        'totalMansMembers': number,
                        'totalGirlsMembers': number,
                    },
                    'mon': {
                        'totalMembers': number,
                        'totalActiveSubs': number,
                        'totalUnActiveSubs': number,
                        'totalMansMembers': number,
                        'totalGirlsMembers': number,
                    },
                    'tue': {
                        'totalMembers': number,
                        'totalActiveSubs': number,
                        'totalUnActiveSubs': number,
                        'totalMansMembers': number,
                        'totalGirlsMembers': number,
                    },
                    'wed': {
                        'totalMembers': number,
                        'totalActiveSubs': number,
                        'totalUnActiveSubs': number,
                        'totalMansMembers': number,
                        'totalGirlsMembers': number,
                    },
                    'thu': {
                        'totalMembers': number,
                        'totalActiveSubs': number,
                        'totalUnActiveSubs': number,
                        'totalMansMembers': number,
                        'totalGirlsMembers': number,
                    },
                    'fri': {
                        'totalMembers': number,
                        'totalActiveSubs': number,
                        'totalUnActiveSubs': number,
                        'totalMansMembers': number,
                        'totalGirlsMembers': number,
                    }
                }
            },
            months : {
                'year': number,
                'month': number,
                'weeks': {
                    'week': number,
                    'days': {
                        'sat': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'sun': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'mon': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'tue': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'wed': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'thu': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'fri': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        }
                    }[]
                }[]
            }[],
            years : {
                'year': number,
                'months': {
                    'month': number,
                    'weeks': {
                        'week': number,
                        'days': {
                            'sat': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'sun': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'mon': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'tue': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'wed': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'thu': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'fri': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            }
                        }[]
                    }[]
                }[]
            }[],
            thisMonth: {
                'year': number,
                'month': number,
                'weeks': {
                    'week': number,
                    'days': {
                        'sat': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'sun': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'mon': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'tue': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'wed': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'thu': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'fri': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        }
                    }[]
                }[]
            },
            thisYear: {
                'year': number,
                'months': {
                    'month': number,
                    'weeks': {
                        'week': number,
                        'days': {
                            'sat': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'sun': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'mon': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'tue': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'wed': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'thu': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'fri': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            }
                        }[]
                    }[]
                }[]
            }
        } =
        {
            thisWeek : {
                'year': 0,
                'month': 0,
                'week': 0,
                'days': {
                    'sat': {
                        'totalMembers': 0,
                        'totalActiveSubs': 0,
                        'totalUnActiveSubs': 0,
                        'totalMansMembers': 0,
                        'totalGirlsMembers': 0,
                    },
                    'sun': {
                        'totalMembers': 0,
                        'totalActiveSubs': 0,
                        'totalUnActiveSubs': 0,
                        'totalMansMembers': 0,
                        'totalGirlsMembers': 0,
                    },
                    'mon': {
                        'totalMembers': 0,
                        'totalActiveSubs': 0,
                        'totalUnActiveSubs': 0,
                        'totalMansMembers': 0,
                        'totalGirlsMembers': 0,
                    },
                    'tue': {
                        'totalMembers': 0,
                        'totalActiveSubs': 0,
                        'totalUnActiveSubs': 0,
                        'totalMansMembers': 0,
                        'totalGirlsMembers': 0,
                    },
                    'wed': {
                        'totalMembers': 0,
                        'totalActiveSubs': 0,
                        'totalUnActiveSubs': 0,
                        'totalMansMembers': 0,
                        'totalGirlsMembers': 0,
                    },
                    'thu': {
                        'totalMembers': 0,
                        'totalActiveSubs': 0,
                        'totalUnActiveSubs': 0,
                        'totalMansMembers': 0,
                        'totalGirlsMembers': 0,
                    },
                    'fri': {
                        'totalMembers': 0,
                        'totalActiveSubs': 0,
                        'totalUnActiveSubs': 0,
                        'totalMansMembers': 0,
                        'totalGirlsMembers': 0,
                    }
                }
            },
            months : [{
                'year': 0,
                'month': 0,
                'weeks': [{
                    'week': 0,
                    'days': [{
                        'sat': {
                            'totalMembers': 0,
                            'totalActiveSubs': 0,
                            'totalUnActiveSubs': 0,
                            'totalMansMembers': 0,
                            'totalGirlsMembers': 0,
                        },
                        'sun': {
                            'totalMembers': 0,
                            'totalActiveSubs': 0,
                            'totalUnActiveSubs': 0,
                            'totalMansMembers': 0,
                            'totalGirlsMembers': 0,
                        },
                        'mon': {
                            'totalMembers': 0,
                            'totalActiveSubs': 0,
                            'totalUnActiveSubs': 0,
                            'totalMansMembers': 0,
                            'totalGirlsMembers': 0,
                        },
                        'tue': {
                            'totalMembers': 0,
                            'totalActiveSubs': 0,
                            'totalUnActiveSubs': 0,
                            'totalMansMembers': 0,
                            'totalGirlsMembers': 0,
                        },
                        'wed': {
                            'totalMembers': 0,
                            'totalActiveSubs': 0,
                            'totalUnActiveSubs': 0,
                            'totalMansMembers': 0,
                            'totalGirlsMembers': 0,
                        },
                        'thu': {
                            'totalMembers': 0,
                            'totalActiveSubs': 0,
                            'totalUnActiveSubs': 0,
                            'totalMansMembers': 0,
                            'totalGirlsMembers': 0,
                        },
                        'fri': {
                            'totalMembers': 0,
                            'totalActiveSubs': 0,
                            'totalUnActiveSubs': 0,
                            'totalMansMembers': 0,
                            'totalGirlsMembers': 0,
                        }
                    }]
                }]
            }],
            years : [{
                'year': 0,
                'months': [{
                    'month': 0,
                    'weeks': [{
                        'week': 0,
                        'days': {
                            'sat': {
                                'totalMembers': 0,
                                'totalActiveSubs': 0,
                                'totalUnActiveSubs': 0,
                                'totalMansMembers': 0,
                                'totalGirlsMembers': 0,
                            },
                            'sun': {
                                'totalMembers': 0,
                                'totalActiveSubs': 0,
                                'totalUnActiveSubs': 0,
                                'totalMansMembers': 0,
                                'totalGirlsMembers': 0,
                            },
                            'mon': {
                                'totalMembers': 0,
                                'totalActiveSubs': 0,
                                'totalUnActiveSubs': 0,
                                'totalMansMembers': 0,
                                'totalGirlsMembers': 0,
                            },
                            'tue': {
                                'totalMembers': 0,
                                'totalActiveSubs': 0,
                                'totalUnActiveSubs': 0,
                                'totalMansMembers': 0,
                                'totalGirlsMembers': 0,
                            },
                            'wed': {
                                'totalMembers': 0,
                                'totalActiveSubs': 0,
                                'totalUnActiveSubs': 0,
                                'totalMansMembers': 0,
                                'totalGirlsMembers': 0,
                            },
                            'thu': {
                                'totalMembers': 0,
                                'totalActiveSubs': 0,
                                'totalUnActiveSubs': 0,
                                'totalMansMembers': 0,
                                'totalGirlsMembers': 0,
                            },
                            'fri': {
                                'totalMembers': 0,
                                'totalActiveSubs': 0,
                                'totalUnActiveSubs': 0,
                                'totalMansMembers': 0,
                                'totalGirlsMembers': 0,
                            }
                        }
                    }]
                }]
            }]
        };
        let statistics = await statisticsModel.findOne({});
        if(statistics?.listsTracking)
        {
            membersGeneralOverview.thisWeek = statistics?.listsTracking.thisWeek
            membersGeneralOverview.thisMonth = statistics?.listsTracking.months.filter((month) => month.month == (new Date()).getMonth())[0]
            membersGeneralOverview.thisYear = statistics?.listsTracking.years.filter((year) => year.year == (new Date()).getFullYear())[0]
        }
        // prepare the report
        let report = {
            membersCount: {
                name: "membersCount",
                value: membersCount,
            },
            activeSubscriptionsCount: {
                name: "activeSubscriptionsCount",
                value: activeSubscriptionsCount,
            },
            expiredSoonSubscriptionsCount: {
                name: "expiredSoonSubscriptionsCount",
                value: expiredSoonSubscriptionsCount,
            },
            expiredSubscriptionsCount: {
                name: "expiredSubscriptionsCount",
                value: expiredSubscriptionsCount,
            },
            activeMembersCount: {
                name: "activeMembersCount",
                value: activeMembersCount,
            },
            disActiveMembersCount: {
                name: "disActiveMembersCount",
                value: disActiveMembersCount,
            },
            servicesCount: {
                name: "servicesCount",
                value: servicesCount,
            },
            activeServicesCount: {
                name: "activeServicesCount",
                value: activeServicesCount,
            },
            disActiveServicesCount: {
                name: "disActiveServicesCount",
                value: disActiveServicesCount,
            },
            membersGeneralOverview: {
                name: "membersGeneralOverview",
                value: membersGeneralOverview,
            },
            servicesNameAndSubscribers: {
                name: "servicesNameAndSubscribers",
                value: servicesNameAndSubscribers,
            },
            workersNameAndJobType: {
                name: "workersNameAndJobType",
                value: workersNameAndJobType,
            },
            membersGendre: {
                name: "membersGendre",
                value: membersGendre,
            },
            totalIncome: statistics?.totalIncome,
            totalOutcome: statistics?.totalOutcome
        }        
        // return the success response
        return NextResponse.json({
            success: true,
            message: "requestTerminatedWithSuccess",
            data: report,
        }, {
            status: 200,
            headers: {
                "content-type": "application/json"
            }
        })
    }catch(e)
    {
        console.log(e)
        // catch any error and return an error response
        return NextResponse.json({
            success: false,
            message: "serverError",
        }, {
            status: 500,
            headers: {
                "content-type": "application/json"
            }
        })
    }
}