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

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

export interface ScoreBreakdown {
  duration_score: number;
  content_density_score: number;
  technique_relevance_score: number;
  position_score: number;
  uniqueness_score: number;
  engagement_proxy_score: number;
  plugin_diversity_score: number;
}

export interface KeyMomentInfo {
  id: string;
  title: string;
  start_time: number;
  end_time: number;
}

export interface HighlightCandidate {
  id: string;
  key_moment_id: string;
  source_video_id: string;
  score: number;
  score_breakdown?: ScoreBreakdown | null;
  duration_secs: number;
  status: string;
  trim_start: number | null;
  trim_end: number | null;
  key_moment: KeyMomentInfo | null;
}

export interface HighlightListResponse {
  highlights: HighlightCandidate[];
}

// ── API functions ────────────────────────────────────────────────────────────

export function fetchCreatorHighlights(params?: {
  status?: string;
  shorts_only?: boolean;
}): Promise<HighlightListResponse> {
  const query = new URLSearchParams();
  if (params?.status) query.set("status", params.status);
  if (params?.shorts_only) query.set("shorts_only", "true");
  const qs = query.toString();
  return request<HighlightListResponse>(
    `${BASE}/creator/highlights${qs ? `?${qs}` : ""}`,
  );
}

export function fetchHighlightDetail(
  id: string,
): Promise<HighlightCandidate> {
  return request<HighlightCandidate>(
    `${BASE}/creator/highlights/${encodeURIComponent(id)}`,
  );
}

export function updateHighlightStatus(
  id: string,
  status: string,
): Promise<HighlightCandidate> {
  return request<HighlightCandidate>(
    `${BASE}/creator/highlights/${encodeURIComponent(id)}`,
    { method: "PATCH", body: JSON.stringify({ status }) },
  );
}

export function trimHighlight(
  id: string,
  trim_start: number,
  trim_end: number,
): Promise<HighlightCandidate> {
  return request<HighlightCandidate>(
    `${BASE}/creator/highlights/${encodeURIComponent(id)}/trim`,
    { method: "PATCH", body: JSON.stringify({ trim_start, trim_end }) },
  );
}
