import { DataModel } from "@imengyu/js-request-transform";
import { RestfulApi } from "../restful";

/**
 * AI 对话附件（临时文件）
 */
export class AgentChatFile extends DataModel<AgentChatFile> {
  constructor() {
    super(AgentChatFile, "AI对话附件");
    this._convertTable = {
      createdAt: { clientSide: "date" },
      updatedAt: { clientSide: "date" },
      deletedAt: { clientSide: "date" },
    };
  }

  id = 0;
  userId = 0;
  historyItemId = 0;
  path = "";
  url = "";
  aiFileId = '';

  createdAt = new Date();
  updatedAt = new Date();
  deletedAt = new Date();
}

export class AgentChatFileApi extends RestfulApi<AgentChatFile> {
  constructor() {
    super("content/agent/chat-file", AgentChatFile);
  }

  /**
   * 上传对话附件（FormData）。临时文件，不持久化保存。
   * 后端：POST /content/agent/chat-file/upload
   */
  upload(file: File | Blob) {
    const formData = new FormData();
    formData.append("file", file);
    return this.post<{
      path: string;
      url: string;
      id: number;
    }>(
      `/${this.subResKey}/upload`,
      "上传AI对话附件",
      formData,
    );
  }

  /**
   * 将聊天附件关联到聊天记录项
   * 后端：POST /content/agent/chat-file/link-to-history-item
   */
  linkToHistoryItem(historyItemId: number, fileId: number) {
    return this.post<void>(
      `/${this.subResKey}/link-to-history-item`,
      "将聊天附件关联到聊天记录项",
      { historyItemId, fileId },
    );
  }

  /**
   * 删除对话附件（临时文件）
   * 后端：DELETE /content/agent/chat-file/delete
   */
  deleteFile(path: string) {
    return this.delete<void>(
      `/${this.subResKey}/delete`,
      "删除AI对话附件",
      { path },
    );
  }

  /**
   * 将对话附件转为对应AI系统可用的文件
   * 后端：POST /content/agent/chat-file/process-to-ai
   */
  processChatFileToAi(id: number, modelName: string) {
    return this.post<string>(
      `/${this.subResKey}/process-to-ai`,
      "将对话附件转为对应AI系统可用的文件",
      { id, modelName },
    );
  }
}

export default new AgentChatFileApi();

