import axios from "axios";
import { Website } from "../../../generated/prisma";

export class OverviewService {


    static async getWebsiteTotalReport(url: string, token: string, { fromdate, todate }: { fromdate: string, todate: string }) {

        console.log("getWebsitesTotalReport", url, token)

        const response = await axios.get(`${url}?token=${token}`, {
            params: {
                from: fromdate,
                to: todate
            }
        })
        return {
            response: response.data,
            url
        }
    }



    static async getWholeReport(websites: Website[], queryParams: { fromdate: string, todate: string }) {

        console.log("webquery", websites, queryParams)


        const response = await Promise.allSettled(websites.map((site) => this.getWebsiteTotalReport(site.url, site.token, queryParams)));

        const filterData = response.map((e, index) => {

            if (e.status === "fulfilled") {
                return {
                    status: e.status,
                    url: websites[index].alias,
                    result: e.value.response?.data?.all
                }
            }

            if (e.status === "rejected") {
                return {
                    status: e.status,
                    url: websites[index].alias,
                }
            }


        })

        return filterData

    }



}