import { api } from "@nice/client";
import { createContext, useContext, useState } from "react";
import { ShareCodeResponse } from "../quick-file/quickFileContext";
interface DashboardContextType {
    shareCodeAll: ShareCodeResourcesSizeByDateRange;
    shareCodeToday: ShareCodeResourcesSizeByDateRange;
    shareCodeYesterday: ShareCodeResourcesSizeByDateRange;
    isShareCodeAllLoading: boolean;
    isShareCodeTodayLoading: boolean;
    isShareCodeYesterdayLoading: boolean;
    distinctUploadIPs: { thisWeek: number; lastWeek: number; all: number };
    isDistinctUploadIPsLoading: boolean;
    shareCodeList: ShareCodeResponse[];
    isShareCodeListLoading: boolean;
    deletedData: { totalSize?: number; resourceCount?: number; };
    isDeletedLoading: boolean;
}
interface ShareCodeResourcesSizeByDateRange {
    totalSize: number;
    resourceCount: number;
}
export const DashboardContext = createContext<DashboardContextType | null>(null);

export const DashboardProvider = ({ children }: { children: React.ReactNode }) => {
    const { data: shareCodeAll, isLoading: isShareCodeAllLoading }:
        { data: ShareCodeResourcesSizeByDateRange, isLoading: boolean }
        = api.shareCode.getShareCodeResourcesTotalSize.useQuery()
    const { data: shareCodeToday, isLoading: isShareCodeTodayLoading }:
        { data: ShareCodeResourcesSizeByDateRange, isLoading: boolean }
        = api.shareCode.getShareCodeResourcesSizeByDateRange.useQuery(
            { dateType: "today" })
    const { data: shareCodeYesterday, isLoading: isShareCodeYesterdayLoading }:
        { data: ShareCodeResourcesSizeByDateRange, isLoading: boolean }
        = api.shareCode.getShareCodeResourcesSizeByDateRange.useQuery(
            { dateType: "yesterday" })
    const { data: distinctUploadIPs, isLoading: isDistinctUploadIPsLoading }:
        { data: { thisWeek: number; lastWeek: number; all: number }, isLoading: boolean }
        = api.shareCode.countDistinctUploadIPs.useQuery()
    const { data: shareCodeList, isLoading: isShareCodeListLoading }:
        { data: ShareCodeResponse[], isLoading: boolean }
        = api.shareCode.findShareCodes.useQuery({
            where: {
                deletedAt: null
            },
            take: 8,
            orderBy: {
                createdAt: 'desc',
            },
        })
    const {data:deletedData , isLoading:isDeletedLoading} = api.shareCode.getAllreadlyDeletedShareCodes.useQuery()
    return <>
        <DashboardContext.Provider value={{
            shareCodeAll,
            shareCodeToday,
            shareCodeYesterday,
            isShareCodeAllLoading,
            isShareCodeTodayLoading,
            isShareCodeYesterdayLoading,
            distinctUploadIPs,
            isDistinctUploadIPsLoading,
            shareCodeList,
            isShareCodeListLoading,
            deletedData,
            isDeletedLoading
        }}>
            {children}
        </DashboardContext.Provider>
    </>
};

export const useDashboardContext = () => {
    const context = useContext(DashboardContext);
    if (!context) {
        throw new Error("useDashboardContext must be used within a DashboardProvider");
    }
    return context;
};