import { get, post, put, del, download } from "@/utils/request";

// 导出格式类型
export type ExportFormat = "json" | "jsonl" | "csv" | "coco" | "yolo";

// 标注任务管理相关接口
export function queryAnnotationTasksUsingGet(params?: any) {
  return get("/api/annotation/project", params);
}

export function createAnnotationTaskUsingPost(data: any) {
  return post("/api/annotation/project", data);
}

export function syncAnnotationTaskUsingPost(data: any) {
  return post(`/api/annotation/task/sync`, data);
}

export function deleteAnnotationTaskByIdUsingDelete(mappingId: string) {
  // Backend expects mapping UUID as path parameter
  return del(`/api/annotation/project/${mappingId}`);
}

export function getAnnotationTaskByIdUsingGet(taskId: string) {
  return get(`/api/annotation/project/${taskId}`);
}

export function updateAnnotationTaskByIdUsingPut(taskId: string, data: any) {
  return put(`/api/annotation/project/${taskId}`, data);
}

// 标签配置管理
export function getTagConfigUsingGet() {
  return get("/api/annotation/tags/config");
}

// 标注模板管理
export function queryAnnotationTemplatesUsingGet(params?: any) {
  return get("/api/annotation/template", params);
}

export function createAnnotationTemplateUsingPost(data: any) {
  return post("/api/annotation/template", data);
}

export function updateAnnotationTemplateByIdUsingPut(
  templateId: string | number,
  data: any
) {
  return put(`/api/annotation/template/${templateId}`, data);
}

export function deleteAnnotationTemplateByIdUsingDelete(
  templateId: string | number
) {
  return del(`/api/annotation/template/${templateId}`);
}


// =====================
// Label Studio Editor（内嵌版）
// =====================

export function getEditorProjectInfoUsingGet(projectId: string) {
  return get(`/api/annotation/editor/projects/${projectId}`);
}

export function listEditorTasksUsingGet(projectId: string, params?: any) {
  return get(`/api/annotation/editor/projects/${projectId}/tasks`, params);
}

export function getEditorTaskUsingGet(
  projectId: string,
  fileId: string,
  params?: { segmentIndex?: number }
) {
  return get(`/api/annotation/editor/projects/${projectId}/tasks/${fileId}`, params);
}

export function upsertEditorAnnotationUsingPut(
  projectId: string,
  fileId: string,
  data: {
    annotation: any;
    expectedUpdatedAt?: string;
    segmentIndex?: number;
  }
) {
  return put(`/api/annotation/editor/projects/${projectId}/tasks/${fileId}/annotation`, data);
}

// =====================
// 标注数据导出
// =====================

export interface ExportStatsResponse {
  projectId: string;
  projectName: string;
  totalFiles: number;
  annotatedFiles: number;
  exportFormat: string;
}

export function getExportStatsUsingGet(projectId: string) {
  return get(`/api/annotation/export/projects/${projectId}/stats`);
}

export function downloadAnnotationsUsingGet(
  projectId: string,
  format: ExportFormat = "json",
  onlyAnnotated: boolean = true,
  includeData: boolean = false
) {
  const params = new URLSearchParams({
    format,
    only_annotated: String(onlyAnnotated),
    include_data: String(includeData),
  });
  return download(`/api/annotation/export/projects/${projectId}/download?${params.toString()}`);
}
