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: [
        "register",
        // ? SchoolService endpoint for backend
        'getSchool',
        "CreateSchool",
        'getSchoolById',
        "CreateSchool",

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

        // ? TheorytrainsGet Service endpoint for backend
        'TheorytrainsGetData', 
        'TheorytrainsGetDataById', 
        'TheorytrainsCreate',
        'TheorytrainsUpdate',
        'TheorytrainsRemove',


        // ? Question Service endpoint for backend
        'QuestionsGetDataById',
        'QuestionsGetData',
        'QuestionsCreate',
        'QuestionsUpdate',
        'QuestionsRemove',

        // ? Choice Service endpoint for backend
        'ChoiceGetData',
        'ChoiceGetDataById',
        'ChoiceCreate',
        'ChoiceUpdate',
        'ChoiceRemove',
        
        // ? PracList Service endpoint for backend
        'PracListGetData',
        'PracListGetDataById',
        'PracListCreate',
        'PracListUpdate',
        'PracListRemove',

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

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

        // ? 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',

        // ? FAQ Service endpoint for backend
        'FAQGetData',
        'FAQGetDataById',
        'FAQCreate',
        'FAQUpdate',
        'FAQRemove',

        // ? Schools Service endpoint for backend
        'SchoolsGetData',
        'SchoolsGetDataById',
        'SchoolsCreate',
        'SchoolsUpdate',
        'SchoolsRemove',
        


        // ? Answer Service endpoint for backend

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

        // ? Usertheorytrain Service endpoint for backend

        'getUsertheorytrain',
        "CreateUsertheorytrain",
        'getUsertheorytrainById',
        "CreateUsertheorytrain",
        'UpdateUsertheorytrain'
    ],
    endpoints: (build) => ({}),
});