import { api } from '@/services/api';

export interface QuestionsResponse {
    id: string,
    question: string
    score: number,
    img: string
    status: boolean,
    TheoryTrainId: string,
    Choices: {
        id: string,
        choice: string,
        status: boolean,
        QuestionId: string
    }[]
    answer?: {
        id: string,
        status: boolean,
        TheoryTrainAttemptId: string,
        ChoiceId: string
    }
}
export interface ExamGetDataByUserIdAndTheoryTrainIdResponse {
    success: boolean,
    message: string,
    theoryTrain?: {
        id: string
        title: string
        period: number,
        description: string,
        status: boolean
    }
    details?: {
        id: string,
        question: string
        score: number,
        img: string
        status: boolean,
        TheoryTrainId: string,
        Choices: {
            id: string,
            choice: string,
            status: boolean,
            QuestionId: string
        }[]
    }[]
    Attempt?: {
        id: string
        endAt: string
        score: null | number,
        status: boolean,
        UserTheoryTrainId: string
    }
    isFullTime?: boolean,
    lastAttempt?: {
        id: string
        endAt: string
        score: null | number,
        status: boolean,
        UserTheoryTrainId: string
    },
    questionsWithAnswers?: QuestionsResponse[],
    questionsWithNoAnswers?: QuestionsResponse[]
    theoryTrainAttempts?:
    {
        id: string
        endAt: string
        score: number
        status: boolean,
        UserTheoryTrainId: string
    }[]
}

export interface ExamGetDataByTheoryTrainAttemptIdResponse {
    success: boolean,
    message: string,
    totalScore: number,
}

export const Exam = api.injectEndpoints({
    endpoints: (build) => ({
        ExamGetDataByUserIdAndTheoryTrainId: build.query<ExamGetDataByUserIdAndTheoryTrainIdResponse, { userId: string, theoryTrainId: string, }>({
            query: (params) => ({
                url: `exam`,
                method: 'GET',
                params
            }),
            providesTags: ['ExamGetDataByUserIdAndTheoryTrainId'],
        }),
        ExamGetDataByTheoryTrainAttemptId: build.mutation<ExamGetDataByTheoryTrainAttemptIdResponse, { theoryTrainAttemptId: string, }>({
            query: (params) => ({
                url: `/exam`,
                method: 'PUT',
                params
            }),
            invalidatesTags: ['ExamGetDataByTheoryTrainAttemptId'],
        }),
    }),
});

export const {
    useExamGetDataByUserIdAndTheoryTrainIdQuery,
    useLazyExamGetDataByUserIdAndTheoryTrainIdQuery,
    useExamGetDataByTheoryTrainAttemptIdMutation

} = Exam;