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

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

type RequestParams = Record<string, unknown>;
type RequestPayload = Record<string, unknown>;

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

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

export function syncAnnotationTaskUsingPost(data: RequestPayload) {
  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: RequestPayload) {
  return put(`/api/annotation/project/${taskId}`, data);
}

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

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

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

export function updateAnnotationTemplateByIdUsingPut(
  templateId: string | number,
  data: RequestPayload
) {
  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?: RequestParams) {
  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 getEditorTaskSegmentUsingGet(
  projectId: string,
  fileId: string,
  params: { segmentIndex: number }
) {
  return get(`/api/annotation/editor/projects/${projectId}/tasks/${fileId}/segments`, params);
}

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

export interface FileVersionCheckResponse {
  fileId: string;
  currentFileVersion: number;
  annotationFileVersion: number | null;
  annotationVersionUnknown: boolean;
  hasNewVersion: boolean;
}

export function checkFileVersionUsingGet(projectId: string, fileId: string) {
  return get(`/api/annotation/editor/projects/${projectId}/files/${fileId}/version`);
}

export interface UseNewVersionResponse {
  fileId: string;
  previousFileVersion: number | null;
  currentFileVersion: number;
  message: string;
}

export function applyNewVersionUsingPost(projectId: string, fileId: string) {
  return post(`/api/annotation/editor/projects/${projectId}/files/${fileId}/use-new-version`, {});
}


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

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,
  filename?: string
) {
  const params = new URLSearchParams({
    format,
    only_annotated: String(onlyAnnotated),
    include_data: String(includeData),
  });
  return download(`/api/annotation/export/projects/${projectId}/download?${params.toString()}`, null, filename);
}
