import { apiClient } from "@/src/lib/api/client";
import type {
  ApiEnvelope,
  CallLog,
  CallStatus,
  CallTranscript,
  CursorPage,
} from "@/src/lib/api/types";

export interface CallListParams {
  agentId?: string;
  status?: CallStatus;
  direction?: "inbound" | "outbound";
  from?: string;
  to?: string;
  limit?: number;
  cursor?: string;
}

export interface TranscriptParams {
  limit?: number;
  cursor?: string;
}

export const callsApi = {
  list: async (params: CallListParams = {}): Promise<CursorPage<CallLog>> => {
    const res = await apiClient.get<ApiEnvelope<CallLog[]>>("/calls", { params });
    return { data: res.data.data, nextCursor: res.data.nextCursor ?? null };
  },
  get: async (callId: string): Promise<CallLog> => {
    const res = await apiClient.get<ApiEnvelope<CallLog>>(`/calls/${callId}`);
    return res.data.data;
  },
  transcripts: async (
    callId: string,
    params: TranscriptParams = {}
  ): Promise<CursorPage<CallTranscript>> => {
    const res = await apiClient.get<ApiEnvelope<CallTranscript[]>>(
      `/calls/${callId}/transcripts`,
      { params }
    );
    return { data: res.data.data, nextCursor: res.data.nextCursor ?? null };
  },
  remove: async (callId: string): Promise<void> => {
    await apiClient.delete(`/calls/${callId}`);
  },
};
