/**
 * API functions for the blocklist management endpoints.
 */

import { del, get, post, put } from "./client";
import { ENDPOINTS } from "./endpoints";
import type {
  BlocklistListResponse,
  BlocklistSource,
  BlocklistSourceCreate,
  BlocklistSourceUpdate,
  ImportLogListResponse,
  ImportRunResult,
  PreviewResponse,
  ScheduleConfig,
  ScheduleInfo,
} from "../types/blocklist";

// ---------------------------------------------------------------------------
// Sources
// ---------------------------------------------------------------------------

/** Fetch all configured blocklist sources. */
export async function fetchBlocklists(): Promise<BlocklistListResponse> {
  return get<BlocklistListResponse>(ENDPOINTS.blocklists);
}

/** Create a new blocklist source. */
export async function createBlocklist(
  payload: BlocklistSourceCreate,
): Promise<BlocklistSource> {
  return post<BlocklistSource>(ENDPOINTS.blocklists, payload);
}

/** Update a blocklist source. */
export async function updateBlocklist(
  id: number,
  payload: BlocklistSourceUpdate,
): Promise<BlocklistSource> {
  return put<BlocklistSource>(ENDPOINTS.blocklist(id), payload);
}

/** Delete a blocklist source. */
export async function deleteBlocklist(id: number): Promise<void> {
  await del<undefined>(ENDPOINTS.blocklist(id));
}

// ---------------------------------------------------------------------------
// Preview
// ---------------------------------------------------------------------------

/** Preview the contents of a blocklist source URL. */
export async function previewBlocklist(id: number): Promise<PreviewResponse> {
  return get<PreviewResponse>(ENDPOINTS.blocklistPreview(id));
}

// ---------------------------------------------------------------------------
// Import
// ---------------------------------------------------------------------------

/** Trigger a manual import of all enabled sources. */
export async function runImportNow(): Promise<ImportRunResult> {
  return post<ImportRunResult>(ENDPOINTS.blocklistsImport, {});
}

// ---------------------------------------------------------------------------
// Schedule
// ---------------------------------------------------------------------------

/** Fetch the current schedule config and next/last run times. */
export async function fetchSchedule(signal?: AbortSignal): Promise<ScheduleInfo> {
  return get<ScheduleInfo>(ENDPOINTS.blocklistsSchedule, signal);
}

/** Update the import schedule. */
export async function updateSchedule(config: ScheduleConfig): Promise<ScheduleInfo> {
  return put<ScheduleInfo>(ENDPOINTS.blocklistsSchedule, config);
}

// ---------------------------------------------------------------------------
// Import log
// ---------------------------------------------------------------------------

/** Fetch a paginated import log. */
export async function fetchImportLog(
  page = 1,
  pageSize = 50,
  sourceId?: number,
): Promise<ImportLogListResponse> {
  const params = new URLSearchParams();
  params.set("page", String(page));
  params.set("page_size", String(pageSize));
  if (sourceId !== undefined) params.set("source_id", String(sourceId));
  return get<ImportLogListResponse>(
    `${ENDPOINTS.blocklistsLog}?${params.toString()}`,
  );
}
