import { api } from "@/lib/api-client";
import type { Exam, ExamModule, Rubric, RubricGroup, ExamStructure, PaginatedResponse, PaginationParams, ApiSuccessResponse } from "@/types";

export interface ExamListParams extends PaginationParams {
  module?: ExamModule;
  subject_id?: number;
  entity_id?: number;
}

export const examsService = {
  async list(module: ExamModule, params?: PaginationParams): Promise<PaginatedResponse<Exam>> {
    return api.get<PaginatedResponse<Exam>>(`/exam/${module}`, params as Record<string, string | number | boolean | undefined>);
  },

  async getById(module: ExamModule, id: number): Promise<Exam> {
    return api.get<Exam>(`/exam/${module}/${id}`);
  },

  async create(data: Partial<Exam>): Promise<Exam> {
    return api.post<Exam>("/exam", data);
  },

  async update(id: number, data: Partial<Exam>): Promise<Exam> {
    return api.patch<Exam>(`/exam/${id}`, data);
  },

  async delete(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/exam/${id}`);
  },

  async setAccess(id: number, access: "public" | "private"): Promise<Exam> {
    return api.patch<Exam>(`/exam/${id}/access`, { access });
  },

  async listRubrics(params?: PaginationParams): Promise<PaginatedResponse<Rubric>> {
    return api.get<PaginatedResponse<Rubric>>("/rubrics", params as Record<string, string | number | boolean | undefined>);
  },

  async createRubric(data: Partial<Rubric>): Promise<Rubric> {
    return api.post<Rubric>("/rubrics", data);
  },

  async updateRubric(id: number, data: Partial<Rubric>): Promise<Rubric> {
    return api.put<Rubric>(`/rubrics/${id}`, data);
  },

  async deleteRubric(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/rubrics/${id}`);
  },

  async listRubricGroups(params?: PaginationParams): Promise<PaginatedResponse<RubricGroup>> {
    return api.get<PaginatedResponse<RubricGroup>>("/rubric-groups", params as Record<string, string | number | boolean | undefined>);
  },

  async createRubricGroup(data: { name: string; rubric_ids: number[] }): Promise<RubricGroup> {
    return api.post<RubricGroup>("/rubric-groups", data);
  },

  async updateRubricGroup(id: number, data: { name?: string; rubric_ids?: number[] }): Promise<RubricGroup> {
    return api.put<RubricGroup>(`/rubric-groups/${id}`, data);
  },

  async deleteRubricGroup(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/rubric-groups/${id}`);
  },

  async listStructures(params?: PaginationParams & { entity_id?: number }): Promise<PaginatedResponse<ExamStructure>> {
    return api.get<PaginatedResponse<ExamStructure>>("/exam-structures", params as Record<string, string | number | boolean | undefined>);
  },

  async createStructure(data: Partial<ExamStructure>): Promise<ExamStructure> {
    return api.post<ExamStructure>("/exam-structures", data);
  },

  async updateStructure(id: number, data: Partial<ExamStructure>): Promise<ExamStructure> {
    return api.put<ExamStructure>(`/exam-structures/${id}`, data);
  },

  async deleteStructure(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/exam-structures/${id}`);
  },

  async suggestRubricCriteria(data: {
    name?: string;
    skill: string;
    exam_type: string;
    levels: string[];
  }): Promise<{ criteria: string[]; suggested_levels?: string[] }> {
    return api.post("/ai/suggest-rubric-criteria", data);
  },

  async getAvatars(): Promise<{ id: number; name: string; thumbnail: string }[]> {
    return api.get("/exam/avatars");
  },
};
