import { api } from '@/services/api';
import { PreviewvideosDataResponse } from './previewvideos';
import { CreateChoiceRequest } from './choice';
import { QuestionsResponse } from './exam';
import { CreateAnswerRequest } from './answer';



export interface TheoryTrainAttemptsDataResponse {
    id: string
    endAt: string
    score: null | number,
    status: boolean,
    UserTheoryTrainId: string
    UserTheoryTrain: {
        id: string
        consumedAttempts: 2,
        endAt: string
        status: boolean
        UserId: string
        TheoryTrainId: string
        TheoryTrain: {
            id: string
            title: string
            period: number
            description: string
            status: boolean
            Questions: QuestionsResponse[]
        }
    },
    Answers: CreateAnswerRequest[]
}
export interface TheoryTrainAttemptsDataResponseByID {
    success: boolean,
    message: string
    info: TheoryTrainAttemptsDataResponse
}


export interface TheoryTrainAttemptsGetDataResponse {

    success: boolean,
    message: string
    details: {
        docs: TheoryTrainAttemptsDataResponse[],
        pages: number
        total: number
    }
}
export interface TheoryTrainAttemptsGetDataByUserTheoryTrainAndUserIdResponse {

    success: boolean,
    message: string
    details: TheoryTrainAttemptsDataResponse[],
}

export interface TheoryTrainAttemptsCreate {
    score: number
    status: boolean
    userTheoryTrainId: string
}

export const TheoryTrainAttempts = api.injectEndpoints({
    endpoints: (build) => ({
        TheoryTrainAttemptsGetData: build.query<TheoryTrainAttemptsGetDataResponse, { search?: string, page: number, paginate: number }>({
            query: (params) => ({
                url: `/theorytrainattempts/showall`,
                method: 'GET',
                params
            }),
            providesTags: ['TheoryTrainAttemptsGetData'],
        }),
        TheoryTrainAttemptsGetDataByUserTheoryTrainAndUserId: build.query<TheoryTrainAttemptsGetDataByUserTheoryTrainAndUserIdResponse, { userId: string, userTheoryTrainId: string }>({
            query: (params) => ({
                url: `/theorytrainattempts/showAllByUserTheoryTrainAndUserId`,
                method: 'GET',
                params
            }),
            providesTags: ['TheoryTrainAttemptsGetDataByUserTheoryTrainAndUserId'],
        }),
        TheoryTrainAttemptsGetDataById: build.query<TheoryTrainAttemptsDataResponseByID, { id: string }>({
            query: (params) => ({
                url: `/theoryTrainAttempts`,
                params
            }),
            providesTags: ['TheoryTrainAttemptsGetDataById'],
        }),
        TheoryTrainAttemptsCreate: build.mutation<TheoryTrainAttemptsDataResponse, TheoryTrainAttemptsCreate>({
            query: (body) => ({
                url: '/theoryTrainAttempts',
                method: 'POST',
                body,
            }),
            invalidatesTags: ['TheoryTrainAttemptsGetData', 'TheoryTrainAttemptsCreate', "TheoryTrainAttemptsGetDataById"],
        }),
        TheoryTrainAttemptsUpdate: build.mutation<TheoryTrainAttemptsDataResponse, TheoryTrainAttemptsCreate>({
            query: (body) => ({
                url: '/theoryTrainAttempts',
                body,
                method: 'PUT',

            }),
            invalidatesTags: ['TheoryTrainAttemptsGetData', "TheoryTrainAttemptsGetDataById", 'TheoryTrainAttemptsCreate', 'TheoryTrainAttemptsUpdate'],
        }),

        TheoryTrainAttemptsRemove: build.mutation<void, { id: string }>({
            query: (params) => ({
                url: `/theoryTrainAttempts`,
                method: 'DELETE',
                params
            }),
            invalidatesTags: ['TheoryTrainAttemptsGetData', "TheoryTrainAttemptsGetDataById", 'TheoryTrainAttemptsRemove'],
        }),
    }),
});

export const {
    useTheoryTrainAttemptsGetDataQuery,
    useLazyTheoryTrainAttemptsGetDataQuery,
    useTheoryTrainAttemptsCreateMutation,
    useTheoryTrainAttemptsGetDataByIdQuery,
    useLazyTheoryTrainAttemptsGetDataByIdQuery,
    useTheoryTrainAttemptsUpdateMutation,
    useTheoryTrainAttemptsRemoveMutation,
    useTheoryTrainAttemptsGetDataByUserTheoryTrainAndUserIdQuery,
    useLazyTheoryTrainAttemptsGetDataByUserTheoryTrainAndUserIdQuery,
} = TheoryTrainAttempts;