/**
 * Consent API client — per-video consent settings and audit history.
 */

import { request, BASE } from "./client";

// ── Types ────────────────────────────────────────────────────────────────────

export interface VideoConsentRead {
  source_video_id: string;
  video_filename: string;
  creator_id: string;
  kb_inclusion: boolean;
  training_usage: boolean;
  public_display: boolean;
  updated_at: string;
}

export interface VideoConsentUpdate {
  kb_inclusion?: boolean;
  training_usage?: boolean;
  public_display?: boolean;
}

export interface ConsentAuditEntry {
  version: number;
  field_name: string;
  old_value: boolean | null;
  new_value: boolean;
  changed_by: string;
  created_at: string;
}

export interface ConsentListResponse {
  items: VideoConsentRead[];
  total: number;
}

export interface ConsentSummary {
  total_videos: number;
  kb_inclusion_granted: number;
  training_usage_granted: number;
  public_display_granted: number;
}

// ── Functions ────────────────────────────────────────────────────────────────

export async function fetchConsentList(): Promise<ConsentListResponse> {
  return request<ConsentListResponse>(`${BASE}/consent/videos`);
}

export async function fetchVideoConsent(
  videoId: string,
): Promise<VideoConsentRead> {
  return request<VideoConsentRead>(`${BASE}/consent/videos/${videoId}`);
}

export async function updateVideoConsent(
  videoId: string,
  updates: VideoConsentUpdate,
): Promise<VideoConsentRead> {
  return request<VideoConsentRead>(`${BASE}/consent/videos/${videoId}`, {
    method: "PUT",
    body: JSON.stringify(updates),
  });
}

export async function fetchConsentHistory(
  videoId: string,
): Promise<ConsentAuditEntry[]> {
  return request<ConsentAuditEntry[]>(
    `${BASE}/consent/videos/${videoId}/history`,
  );
}

export async function fetchConsentSummary(): Promise<ConsentSummary> {
  return request<ConsentSummary>(`${BASE}/consent/summary`);
}
