import {
  AttachmentSearch,
  AttachmentUpload,
} from "@/components/models/management/Attachments";

import { request } from "@/components/service/index";
export const AttachmentsService = {
  upload: (payload: AttachmentUpload) => {
    return request.post("/api/attachments/upload", payload);
  },
  delete: (id: Number) => {
    return request.delete(`/api/attachments/${id}`, {});
  },
  view: (id: number) => {
    return request.get(`/api/attachments/${id}`, {});
  },
  update: (id: number, payload: AttachmentUpload) => {
    return request.put(`/api/attachments/${id}/update`, payload);
  },

  readTextByPath: (objectName: string) => {
    return request.get(
      `/api/attachments/readYamlText?objectName=${objectName}`,
      {},
    );
  },
  downloadFile: (objectName: string) => {
    return request.getFile(
      `/api/attachments/download?objectName=${objectName}`,
      {},
    );
  },
  search: (payload: AttachmentSearch) => {
    return request.get("/api/attachments/search", payload);
  },

  /** 여러 Training Script를 머지하여 master.py 생성 */
  mergeScripts: (payload: {
    scriptIds: number[];
    title?: string;
    description?: string;
    refId?: number | null;
    refType?: string;
    regUserId: string;
    projectId: number;
  }) => {
    return request.post("/api/attachments/merge-scripts", payload);
  },

  /** 스크립트 컴파일 요청 */
  compile: (id: number) => {
    return request.post(`/api/attachments/${id}/compile`, {});
  },

  /** 기존 컴파일 결과 정보 조회 (이미 만들어진 YAML 경로 확인) */
  getCompiledInfo: (id: number) => {
    return request.get(`/api/attachments/${id}/compiled-info`, {});
  },

  /** 컴파일된 YAML + 원본 py 스크립트 ZIP 다운로드 */
  downloadCompiledBundle: (attachmentId: number, yamlObjectName: string) => {
    return request.getFile(
      `/api/attachments/download-compiled-bundle?id=${attachmentId}&yamlObjectName=${encodeURIComponent(yamlObjectName)}`,
      {},
    );
  },

  /** 스크립트 저장 시 YAML에 반영할 MinIO 설정 (백엔드 저장값) */
  getMinioConfig: () => request.get<Record<string, string>>("/api/attachments/minio-config"),

  /** Auto Script MLflow 사용 시 YAML에 넣을 설정 (백엔드 저장값) */
  getMlflowConfig: () => request.get<Record<string, string>>("/api/attachments/mlflow-config"),
};
