import { api } from "@/lib/api-client";

export interface AiCourseCreateEnglishRequest {
  cefr_level: string;
  gap_profile_id?: number;
}

export interface AiCourseCreateIeltsRequest {
  skill: "listening" | "reading" | "writing" | "speaking";
  target_band: number;
  brief?: string;
}

export interface AiCourseCreateResponse {
  log_id: number;
  status: string;
  brief?: Record<string, unknown>;
  skill?: string;
}

export interface QualityGateResult {
  status: string;
  readability_score: number;
  cefr_alignment: boolean;
  grammar_issues: string[];
  attempts: number;
}

export interface IELTSValidationResult {
  type: string;
  validation_results: Record<string, unknown>;
  overall_passed: boolean;
}

export const aiCourseService = {
  createEnglish: (data: AiCourseCreateEnglishRequest) =>
    api.post<AiCourseCreateResponse>("/ai-course/english/create", data),

  createIelts: (data: AiCourseCreateIeltsRequest) =>
    api.post<AiCourseCreateResponse>("/ai-course/ielts/create", data),

  getCourse: (courseId: number) =>
    api.get<Record<string, unknown>>(`/ai-course/${courseId}`),

  getTracks: (courseId: number) =>
    api.get<unknown[]>(`/ai-course/${courseId}/tracks`),

  getQualityGate: (courseId: number) =>
    api.get<QualityGateResult>(`/ai-course/${courseId}/quality`),

  approveQuality: (courseId: number) =>
    api.post<{ approved: boolean }>(`/ai-course/${courseId}/approve`),

  rejectQuality: (courseId: number, reason: string) =>
    api.post<{ rejected: boolean; can_retry: boolean }>(`/ai-course/${courseId}/reject`, { reason }),

  getIeltsValidation: (courseId: number) =>
    api.get<IELTSValidationResult>(`/ai-course/${courseId}/validation`),

  submitExaminerReview: (logId: number, data: { action: string; examiner_notes?: string }) =>
    api.post<{ status: string; log_id: number }>(`/ai-course/ielts-review/${logId}`, data),

  getEnglishTaxonomy: () =>
    api.get<Record<string, unknown>>("/ai-course/english/taxonomy"),

  getReviewQueue: (page = 1, size = 20) =>
    api.get<{ total: number; page: number; size: number; items: unknown[] }>("/ai-course/review-queue", { page, size }),

  getIeltsReviewQueue: (page = 1, size = 20) =>
    api.get<{ total: number; page: number; size: number; items: unknown[] }>("/ai-course/ielts-review-queue", { page, size }),
};
