import { NextFunction, Request, Response } from "express";
import { CatchAsyncError } from "../../middleware/Catch";
import { WebsiteReportService } from "../report/report.service";
import { ApiError } from "../../middleware/ApiError";
import { ApiResponse } from "../../middleware/ApiResponse";
import { WebsiteService } from "../websites/website.service";
import { OverviewService } from "./overview.service";

export const getAllOrderReport = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const { todate, fromdate } = req.query as { todate: string, fromdate: string }

    console.log(todate, fromdate)

    if (!todate || !fromdate) {
        throw new ApiError("Date Invalid", 404)
    }

    const response = await WebsiteReportService.getBramikalpreport(todate, fromdate)


    const count = response?.data.reduce((accu: number, curr: { total_orders: number }) => {
        return accu + curr?.total_orders
    }, 0)




    return res.status(200).json({
        response: response?.data,
        count
    })

})




export const getAllLeadReport = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {


    const { todate, fromdate } = req.query as { todate: string, fromdate: string }



    if (!todate || !fromdate) {
        throw new ApiError("Date Invalid", 404)
    }

    const getAllWebsites = await WebsiteService.getAllWebsites();


    const getOverAllReport = await OverviewService.getWholeReport(getAllWebsites, { todate, fromdate });

    return res.status(200).json(getOverAllReport)

})