// [DEF:frontend.src.lib.api.assistant:Module]
// @TIER: STANDARD
// @SEMANTICS: assistant, api, client, chat, confirmation
// @PURPOSE: API client wrapper for assistant chat, confirmation actions, and history retrieval.
// @LAYER: Infra-API
// @RELATION: DEPENDS_ON -> frontend.src.lib.api.api_module
// @INVARIANT: All assistant requests must use requestApi wrapper (no native fetch).

import { requestApi } from '$lib/api.js';

// [DEF:sendAssistantMessage:Function]
// @PURPOSE: Send a user message to assistant orchestrator endpoint.
// @PRE: payload.message is a non-empty string.
// @POST: Returns assistant response object with deterministic state.
export function sendAssistantMessage(payload) {
  return requestApi('/assistant/messages', 'POST', payload);
}
// [/DEF:sendAssistantMessage:Function]

// [DEF:confirmAssistantOperation:Function]
// @PURPOSE: Confirm a pending risky assistant operation.
// @PRE: confirmationId references an existing pending token.
// @POST: Returns execution response (started/success/failed).
export function confirmAssistantOperation(confirmationId) {
  return requestApi(`/assistant/confirmations/${confirmationId}/confirm`, 'POST');
}
// [/DEF:confirmAssistantOperation:Function]

// [DEF:cancelAssistantOperation:Function]
// @PURPOSE: Cancel a pending risky assistant operation.
// @PRE: confirmationId references an existing pending token.
// @POST: Operation is cancelled and cannot be executed by this token.
export function cancelAssistantOperation(confirmationId) {
  return requestApi(`/assistant/confirmations/${confirmationId}/cancel`, 'POST');
}
// [/DEF:cancelAssistantOperation:Function]

// [DEF:getAssistantHistory:Function]
// @PURPOSE: Retrieve paginated assistant conversation history.
// @PRE: page/pageSize are positive integers.
// @POST: Returns a paginated payload with history items.
export function getAssistantHistory(page = 1, pageSize = 20, conversationId = null, fromLatest = false) {
  const params = new URLSearchParams({ page: String(page), page_size: String(pageSize) });
  if (conversationId) {
    params.append('conversation_id', conversationId);
  }
  if (fromLatest) {
    params.append('from_latest', 'true');
  }
  return requestApi(`/assistant/history?${params.toString()}`, 'GET');
}
// [/DEF:getAssistantHistory:Function]

// [DEF:getAssistantConversations:Function]
// @PURPOSE: Retrieve paginated conversation list for assistant sidebar/history switcher.
// @PRE: page/pageSize are positive integers.
// @POST: Returns paginated conversation summaries.
export function getAssistantConversations(
  page = 1,
  pageSize = 20,
  includeArchived = false,
  search = '',
  archivedOnly = false,
) {
  const params = new URLSearchParams({ page: String(page), page_size: String(pageSize) });
  if (includeArchived) {
    params.append('include_archived', 'true');
  }
  if (archivedOnly) {
    params.append('archived_only', 'true');
  }
  if (search?.trim()) {
    params.append('search', search.trim());
  }
  return requestApi(`/assistant/conversations?${params.toString()}`, 'GET');
}
// [/DEF:getAssistantConversations:Function]

// [DEF:deleteAssistantConversation:Function]
// @PURPOSE: Soft-delete or hard-delete a conversation.
// @PRE: conversationId string is provided.
// @POST: Returns success status.
export function deleteAssistantConversation(conversationId) {
  return requestApi(`/assistant/conversations/${conversationId}`, 'DELETE');
}
// [/DEF:deleteAssistantConversation:Function]

// [/DEF:frontend.src.lib.api.assistant:Module]
