/**
 * API client for AI UI Control endpoints.
 *
 * Task 4.2: REST endpoints for AI agents to send commands and poll status.
 */

import { apiClient } from './client';

export interface UICommandCreate {
  action: string;
  path?: string | null;
  entity?: string | null;
  filter?: Record<string, unknown> | null;
  contact_id?: string | null;
  modal?: string | null;
  tab?: string | null;
  section?: string | null;
  key?: string | null;
  value?: unknown | null;
  description?: string | null;
}

export interface UICommandResponse {
  command_id: string;
  status: string;
  action: string;
  message?: string | null;
}

export interface UICommandStatusResponse {
  command_id: string;
  status: string;
  action?: string | null;
  feedback?: {
    command_id: string;
    status: string;
    action?: string | null;
    current_path?: string | null;
    current_tab?: string | null;
    message?: string | null;
    error?: string | null;
    data?: Record<string, unknown> | null;
  } | null;
}

export async function sendUICommand(
  body: UICommandCreate,
): Promise<UICommandResponse> {
  const res = await apiClient.post<UICommandResponse>(
    '/ai-ui-control/command',
    body,
  );
  return res.data;
}

export async function getCommandStatus(
  commandId: string,
): Promise<UICommandStatusResponse> {
  const res = await apiClient.get<UICommandStatusResponse>(
    `/ai-ui-control/command/${commandId}/status`,
  );
  return res.data;
}

export async function getOnlineUsers(): Promise<{ online_users: string[] }> {
  const res = await apiClient.get<{ online_users: string[] }>(
    '/ai-ui-control/online-users',
  );
  return res.data;
}
