import { FetchArgs, createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { getSession } from 'next-auth/react';


// import { HYDRATE } from "next-redux-wrapper";

export const BASE_URL = process.env.BASIC_URL;

const baseQuery = fetchBaseQuery({
    baseUrl: BASE_URL,
    // credentials: 'include',
    prepareHeaders: async (headers: any) => {
        let token: string;
        const session = await getSession();
        token = session?.user.accessToken || '';
        if (token) {
            headers.set('Authorization', `Bearer ${token}`);
        }
        return headers;
    },
});
const baseQueryWithReAuth = async (args: FetchArgs, api: any, extraOptions: any) => {
    let result: any = await baseQuery(args, api, extraOptions);

    // if (result.error && result.error.originalStatus === 401 && result.error.status === 401) {
    //     const refreshResult = await baseQuery(
    //         {
    //             url: '/auth/refresh',
    //             method: 'POST',
    //         },
    //         api,
    //         extraOptions
    //     );
    //     if (refreshResult.data) {
    //         result = await baseQuery(args, api, extraOptions);
    //     } else {
    //         if (typeof window !== 'undefined') {
    //             window.location.assign(window.location.origin + '/auth');
    //         }
    //         throw new Error('Failed to refresh token');
    //     }
    // }

    return result;
};

export const api = createApi({
    reducerPath: 'api',
    baseQuery: baseQueryWithReAuth,
    // extractRehydrationInfo(action, { reducerPath }) {
    //     if (action.type === HYDRATE) {
    //         return action.payload[reducerPath];
    //     }
    // },
    tagTypes: [
        // ? Exam endpoint for backend
        "ExamGetDataByUserIdAndTheoryTrainId",
        "ExamGetDataByTheoryTrainAttemptId",
        "register",
        // ? SchoolService endpoint for backend
        'getSchool',
        "CreateSchool",
        'getSchoolById',
        "CreateSchool",

        // ? BlogService endpoint for backend
        'getBlog',
        "CreateBlog",
        'getBlogById',
        "CreateBlog",

        // ? Theorytrains Service endpoint for backend
        'getTheorytrains',
        "CreateTheorytrains",
        'getTheorytrainsById',
        "CreateTheorytrains",

        // ? Question Service endpoint for backend
        'getQuestion',
        "CreateQuestion",
        'getQuestionById',
        "CreateQuestion",

        // ? Choice Service endpoint for backend

        'getChoice',
        "CreateChoice",
        'getChoiceById',
        "CreateChoice",


        // ? Answer Service endpoint for backend

        'getAnswer',
        "CreateAnswer",
        'getAnswerById',
        "CreateAnswer",
        "UpdateAnswer",

        // ? Usertheorytrain Service endpoint for backend

        'getUsertheorytrain',
        "CreateUsertheorytrain",
        'getUsertheorytrainById',
        "CreateUsertheorytrain",
        'UpdateUsertheorytrain',

        // ? PracList Service endpoint for backend
        'PracListGetData',
        'PracListGetDataById',
        'PracListCreate',
        'PracListUpdate',
        'PracListRemove',

        // ? PracVideo Service endpoint for backend
        'PracVideoGetData',
        'PracVideoGetDataById',
        'PracVideoCreate',
        'PracVideoUpdate',
        'PracVideoRemove',

        // ? Subscriptions Service endpoint for backend
        'SubscriptionsGetData',
        'SubscriptionsGetDataById',
        'SubscriptionsCreate',
        'SubscriptionsUpdate',
        'SubscriptionsRemove',

        // ? UserSubscriptions Service endpoint for backend
        'UserSubscriptionsGetData',
        'UserSubscriptionsGetDataById',
        'UserSubscriptionsCreate',
        'UserSubscriptionsUpdate',
        'UserSubscriptionsRemove',

        // ? User Service endpoint for backend
        'UserGetData',
        'UserGetDataById',
        'UserCreate',
        'UserUpdate',
        'UserRemove',

        // ? Previewvideos Service endpoint for backend
        'PreviewvideosGetData',
        'PreviewvideosGetDataById',
        'PreviewvideosCreate',
        'PreviewvideosUpdate',
        'PreviewvideosRemove',

        // ? TheoryTrainAttempts Service endpoint for backend
        'TheoryTrainAttemptsGetData',
        'TheoryTrainAttemptsGetDataByUserTheoryTrainAndUserId',
        'TheoryTrainAttemptsGetDataById',
        'TheoryTrainAttemptsCreate',
        'TheoryTrainAttemptsUpdate',
        'TheoryTrainAttemptsRemove',


    ],
    endpoints: (build) => ({}),
});