import { api } from "@/lib/api-client";
import type { PaginatedResponse } from "@/types/common";
import type {
  ExamReviewDetail,
  ExamReviewListItem,
} from "@/types/exam-review";

export interface ExamReviewQueueParams {
  page?: number;
  size?: number;
  search?: string;
  status?: "draft" | "pending_review" | "published" | "archived";
  subject_id?: number;
}

/**
 * Admin-facing review queue. Everything here assumes the caller has an admin
 * JWT — the server is authoritative; the frontend just surfaces errors.
 */
export const examReviewService = {
  async queue(
    params: ExamReviewQueueParams = {},
  ): Promise<PaginatedResponse<ExamReviewListItem>> {
    const res = await api.get<PaginatedResponse<ExamReviewListItem>>(
      "/exam/review/queue",
      params,
    );
    const items = res.items ?? res.data ?? [];
    return {
      items,
      total: res.total ?? items.length,
      page: res.page ?? 1,
      size: res.size ?? items.length,
      pages: res.pages ?? 1,
    };
  },

  async detail(examId: number): Promise<ExamReviewDetail> {
    return api.get<ExamReviewDetail>(`/exam/review/${examId}`);
  },

  async approve(
    examId: number,
    notes?: string,
  ): Promise<ExamReviewListItem> {
    return api.post<ExamReviewListItem>(
      `/exam/review/${examId}/approve`,
      notes ? { notes } : {},
    );
  },

  async reject(examId: number, notes: string): Promise<ExamReviewListItem> {
    return api.post<ExamReviewListItem>(`/exam/review/${examId}/reject`, {
      notes,
    });
  },
};
