import { request } from "../../app/api.js";
import { chatsListPath, projectChatsPath } from "./chats-query.js";

export function listChats(cursor, search, archived = false, options = {}) {
  return request(chatsListPath(cursor, search, archived), options);
}

export function listProjectChats(cursor) {
  return request(projectChatsPath(cursor));
}

export function hideProject(projectKey) {
  return request("/thread-projects/hide", { method: "POST", body: { projectKey } });
}

export function openChat(thread, workspaceId) {
  return request(`/accounts/${encodeURIComponent(thread.ownerId)}/threads/${encodeURIComponent(thread.id)}/open`, {
    method: "POST",
    body: { workspaceId: workspaceId || undefined, projectLabel: thread.workspaceLabel || undefined }
  });
}

export function archiveChat(thread) {
  return request(`/accounts/${encodeURIComponent(thread.ownerId)}/threads/${encodeURIComponent(thread.id)}`, {
    method: "DELETE"
  });
}

export function deleteArchivedChat(thread) {
  return request(`/accounts/${encodeURIComponent(thread.ownerId)}/threads/${encodeURIComponent(thread.id)}/local-delete`, {
    method: "DELETE"
  });
}

export function renameChat(thread, name) {
  return request(`/accounts/${encodeURIComponent(thread.ownerId)}/threads/${encodeURIComponent(thread.id)}/name`, {
    method: "PATCH",
    body: { name }
  });
}

export function setChatPinned(thread, pinned) {
  return request(`/accounts/${encodeURIComponent(thread.ownerId)}/threads/${encodeURIComponent(thread.id)}/pin`, {
    method: "PATCH",
    body: { pinned }
  });
}

export function createChat(accountId, workspaceId, clientSessionId) {
  return request("/sessions", { method: "POST", body: { accountId, workspaceId, clientSessionId } });
}

export function sendInitialChatMessage(sessionId, payload) {
  return request(`/sessions/${encodeURIComponent(sessionId)}/messages`, { method: "POST", body: payload });
}

export function uploadChatAttachment(sessionId, attachment) {
  return request(`/sessions/${encodeURIComponent(sessionId)}/attachments`, { method: "POST", body: attachment });
}
