import { createCoreAPIClient } from "@/lib/features/coreClient";
import type { CoreResponse } from "@/lib/features/types";
import type {
  GuruAllowlistEntry,
  GuruAuditEntry,
  GuruPendingAction,
  GuruTicket,
  GuruTicketFilters,
} from "./types";

const coreClient = createCoreAPIClient();
const BASE = "/v1/admin-console/guru";

type TicketList = {
  tickets: GuruTicket[];
  total: number;
  page: number;
  limit: number;
};

export async function listMyTickets(
  filters: GuruTicketFilters,
): Promise<CoreResponse<TicketList>> {
  return coreClient(`${BASE}/tickets/list`, {
    method: "POST",
    body: JSON.stringify(filters),
  });
}

export async function listAllTickets(
  filters: GuruTicketFilters,
): Promise<CoreResponse<TicketList>> {
  return coreClient(`${BASE}/admin/tickets/list`, {
    method: "POST",
    body: JSON.stringify(filters),
  });
}

export async function getTicket(
  reference: string,
  opts: { admin?: boolean } = {},
): Promise<CoreResponse<{ ticket: GuruTicket; actions: GuruPendingAction[] }>> {
  const prefix = opts.admin ? `${BASE}/admin/tickets` : `${BASE}/tickets`;
  return coreClient(`${prefix}/${encodeURIComponent(reference)}`, {
    method: "GET",
  });
}

export async function listTicketActions(
  reference: string,
): Promise<CoreResponse<{ actions: GuruPendingAction[] }>> {
  return coreClient(
    `${BASE}/tickets/${encodeURIComponent(reference)}/actions`,
    { method: "GET" },
  );
}

export interface GuruTranscriptConversation {
  conversation_id: string;
  started_at: string | null;
  turns: Array<{ role: "user" | "agent"; text: string }>;
  unavailable?: boolean;
}

export async function getTicketTranscript(
  reference: string,
  opts: { admin?: boolean } = {},
): Promise<CoreResponse<{ conversations: GuruTranscriptConversation[] }>> {
  const prefix = opts.admin ? `${BASE}/admin/tickets` : `${BASE}/tickets`;
  return coreClient(`${prefix}/${encodeURIComponent(reference)}/transcript`, {
    method: "GET",
  });
}

export interface GuruTicketUpdate {
  at: string | null;
  by: string;
  text: string;
}

export async function getTicketUpdates(
  reference: string,
  opts: { admin?: boolean } = {},
): Promise<
  CoreResponse<{
    updates: GuruTicketUpdate[];
    task_status: { status: string; type: string } | null;
  }>
> {
  const prefix = opts.admin ? `${BASE}/admin/tickets` : `${BASE}/tickets`;
  return coreClient(`${prefix}/${encodeURIComponent(reference)}/updates`, {
    method: "GET",
  });
}

export interface GuruStats {
  by_category: Array<{ category: string; count: number }>;
  by_outcome: Array<{ outcome: string; count: number }>;
  weekly: Array<{ week: string; count: number }>;
  avg_resolution_hours: number | null;
}

export async function getAnalytics(): Promise<CoreResponse<GuruStats>> {
  return coreClient(`${BASE}/analytics`, { method: "GET" });
}

export async function listAllowlist(): Promise<
  CoreResponse<{ allowlist: GuruAllowlistEntry[] }>
> {
  return coreClient(`${BASE}/allowlist`, { method: "GET" });
}

export async function listAudit(filters: {
  ticket_id?: string;
  page?: number;
  limit?: number;
}): Promise<
  CoreResponse<{
    entries: GuruAuditEntry[];
    total: number;
    page: number;
    limit: number;
  }>
> {
  return coreClient(`${BASE}/audit/list`, {
    method: "POST",
    body: JSON.stringify(filters),
  });
}
