import { CreateAnswerRequest } from './answer';
import { api } from './api';
import { CreateChoiceRequest, CreateChoiceResponse } from './choice';
import { CreateQuestionRequest } from './questions';
import { CreateTheorytrainsRequest } from './theorytrains';

type GetUsertheorytrainByIdResponse = {
    success: boolean,
    message: string,
    info: CreateUsertheorytrainRequest
}
type GetUsertheorytrainResponse = {
    success: boolean,
    message: string,
    details: {
        docs: CreateUsertheorytrainRequest[],
        pages: 1,
        total: 1
    }
}

export interface CreateUsertheorytrainRequest {
    id?: string,
    expiryTime?: string
    attemptNum?: number,
    finalScore?: number,
    status?: boolean,
    userId?: string,
    theoryTrainId?: string,
    Answers?: {
        id: string,
        status: boolean,
        UserTheoryTrainId: string,
        ChoiceId: string,
        UserTheoryTrain: CreateUsertheorytrainRequest,
        Question: CreateQuestionRequest,
        Choice: CreateChoiceRequest
        score?: number
    }[]
    TheoryTrain?:CreateTheorytrainsRequest
}
type CreateUsertheorytrainResponse = {
    success: boolean,
    message: string,
    result: CreateUsertheorytrainRequest
}
export const Usertheorytrain = api.injectEndpoints({
    endpoints: (build) => ({
        CreateUsertheorytrain: build.mutation<CreateUsertheorytrainResponse, CreateUsertheorytrainRequest>({
            query: (body) => ({
                url: 'usertheorytrain',
                method: 'POST',
                body,
            }),
            invalidatesTags: ['CreateUsertheorytrain'],
        }),
        UpdateUsertheorytrain: build.mutation<CreateUsertheorytrainResponse, CreateUsertheorytrainRequest>({
            query: (body) => ({
                url: 'usertheorytrain',
                method: 'PUT',
                body,
            }),
            invalidatesTags: ['UpdateUsertheorytrain'],
        }),
        getUsertheorytrain: build.query<GetUsertheorytrainResponse, any>({
            query: (params) => ({
                url: `usertheorytrain/showall`,
                method: 'GET',
                // params,
            }),
            providesTags: ['getUsertheorytrain', "CreateUsertheorytrain", 'UpdateUsertheorytrain',"CreateAnswer"],
        }),
        getUsertheorytrainById: build.query<GetUsertheorytrainByIdResponse, { id: string }>({
            query: (params) => ({
                url: `usertheorytrain`,
                method: 'GET',
                params,
            }),
            providesTags: ['getUsertheorytrainById', "CreateUsertheorytrain", 'UpdateUsertheorytrain',"CreateAnswer"],
        }),
        getUsertheorytrainByTheoryTrainIdAndUserId: build.query<GetUsertheorytrainByIdResponse, { userId: string, theoryTrainId: string }>({
            query: (params) => ({
                url: `usertheorytrain/byUserbyTheory`,
                method: 'GET',
                params,
            }),
            providesTags: ['getUsertheorytrainById', "CreateUsertheorytrain", 'CreateUsertheorytrain', 'UpdateUsertheorytrain',"CreateAnswer"],
        }),

    }),
});

export const {
    useCreateUsertheorytrainMutation,
    useUpdateUsertheorytrainMutation,
    useGetUsertheorytrainQuery,
    useGetUsertheorytrainByIdQuery,
    useGetUsertheorytrainByTheoryTrainIdAndUserIdQuery,
    useLazyGetUsertheorytrainByTheoryTrainIdAndUserIdQuery,
} = Usertheorytrain;