import { baseApi } from '@ema-platform/api';
import type {
  Result,
  ExamAppeal,
  ListResponse,
  CreateResultPayload,
  UpdateResultPayload,
  ModerateResultPayload,
  ResultDecisionPayload,
  DecideAppealPayload,
} from '../types/result';

const resultApi = baseApi.injectEndpoints({
  endpoints: (builder) => ({
    getResults: builder.query<ListResponse<Result>, void>({
      query: () => '/results',
      providesTags: ['Api'],
    }),
    getResult: builder.query<Result, string>({
      query: (id) => `/results/${id}?i=exam,profile`,
      providesTags: ['Api'],
    }),
    createResult: builder.mutation<Result, CreateResultPayload>({
      query: (body) => ({ url: '/results', method: 'POST', body }),
      invalidatesTags: ['Api'],
    }),
    updateResult: builder.mutation<Result, UpdateResultPayload>({
      query: ({ id, ...body }) => ({
        url: `/results/${id}`,
        method: 'PUT',
        body,
      }),
      invalidatesTags: ['Api'],
    }),
    deleteResult: builder.mutation<void, string>({
      query: (id) => ({ url: `/results/${id}`, method: 'DELETE' }),
      invalidatesTags: ['Api'],
    }),
    // --- Quality control (US-EXAM-012/013/014) ---------------------------
    moderateResult: builder.mutation<Result, ModerateResultPayload>({
      query: ({ id, ...body }) => ({
        url: `/results/${id}/moderate`,
        method: 'POST',
        body,
      }),
      invalidatesTags: ['Api'],
    }),
    approveResult: builder.mutation<Result, ResultDecisionPayload>({
      query: ({ id, ...body }) => ({
        url: `/results/${id}/approve`,
        method: 'POST',
        body,
      }),
      invalidatesTags: ['Api'],
    }),
    returnResult: builder.mutation<Result, ResultDecisionPayload>({
      query: ({ id, ...body }) => ({
        url: `/results/${id}/return`,
        method: 'POST',
        body,
      }),
      invalidatesTags: ['Api'],
    }),
    publishExamResults: builder.mutation<
      { examId: string; published: number; skipped: number },
      string
    >({
      query: (examId) => ({
        url: `/results/exam/${examId}/publish`,
        method: 'POST',
      }),
      invalidatesTags: ['Api'],
    }),
    // --- Appeals (US-EXAM-016) -------------------------------------------
    getPendingAppeals: builder.query<ExamAppeal[], void>({
      query: () => '/results/appeals/pending',
      providesTags: ['Api'],
    }),
    decideAppeal: builder.mutation<ExamAppeal, DecideAppealPayload>({
      query: ({ id, ...body }) => ({
        url: `/results/appeals/${id}/decide`,
        method: 'POST',
        body,
      }),
      invalidatesTags: ['Api'],
    }),
  }),
  overrideExisting: false,
});

export const {
  useGetResultsQuery,
  useGetResultQuery,
  useLazyGetResultQuery,
  useCreateResultMutation,
  useUpdateResultMutation,
  useDeleteResultMutation,
  useModerateResultMutation,
  useApproveResultMutation,
  useReturnResultMutation,
  usePublishExamResultsMutation,
  useGetPendingAppealsQuery,
  useDecideAppealMutation,
} = resultApi;
