import { GetRequest } from './BaseType';
import { api } from './api';

type GetQuestionByIdResponse = {
    success: boolean,
    message: string,
    info: CreateQuestionRequest
}
export type GetQuestionsResponse = {
    success: boolean,
    message: string,
    details: {
        docs: CreateQuestionRequest[],
        pages: 1,
        total: 1
    }
}

export type CreateQuestionRequest = {
    id?: string,
    question: string,
    score: number,
    img: string,
    status?: boolean,
    theoryTrainId: string,
    Choices?: {
        id: string,
        choice: string,
        status: boolean,
        QuestionId: string
        Answers: {
            id: string
            status: true,
            UserTheoryTrainId: string
            ChoiceId: string
        }[]
    }[],
    TheoryTrain?: {
        id: string,
        title: string,
        period: number,
        description: string,
        status: boolean
    },
}
type CreateQuestionResponse = {
    success: boolean,
    message: string,
    result: CreateQuestionRequest
}

export const Question = api.injectEndpoints({
    endpoints: (build) => ({
        CreateQuestion: build.mutation<CreateQuestionResponse, CreateQuestionRequest>({
            query: (body) => ({
                url: 'question',
                method: 'POST',
                body,
            }),
            invalidatesTags: ['CreateQuestion'],
        }),
        getQuestions: build.query<GetQuestionsResponse, GetRequest>({
            query: (params) => ({
                url: `questions/showall`,
                method: 'GET',
                params,
            }),
            providesTags: ['getQuestion', "CreateQuestion","CreateAnswer"],
        }),
        getQuestionById: build.query<GetQuestionByIdResponse, { id: string }>({
            query: (params) => ({
                url: `question`,
                method: 'GET',
                params,
            }),
            providesTags: ['getQuestionById', "CreateQuestion","CreateAnswer"],
        }),

    }),
});

export const {
    useCreateQuestionMutation,
    useGetQuestionsQuery,
    useGetQuestionByIdQuery
} = Question;