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

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

export interface KeyMomentSummary {
  id: string;
  title: string;
  summary: string;
  start_time: number;
  end_time: number;
  content_type: string;
  plugins: string[] | null;
  source_video_id: string;
  video_filename: string;
}

export interface CreatorInfo {
  name: string;
  slug: string;
  genres: string[] | null;
}

export interface RelatedLinkItem {
  target_title: string;
  target_slug: string;
  relationship: string;
  creator_name: string;
  topic_category: string;
  reason: string;
}

export interface BodySubSectionV2 {
  heading: string;
  content: string;
}

export interface BodySectionV2 {
  heading: string;
  content: string;
  subsections: BodySubSectionV2[];
}

export interface SourceVideoSummary {
  id: string;
  filename: string;
  content_type: string;
  added_at: string | null;
}

export interface TechniquePageDetail {
  id: string;
  title: string;
  slug: string;
  topic_category: string;
  topic_tags: string[] | null;
  summary: string | null;
  body_sections: BodySectionV2[] | Record<string, unknown> | null;
  body_sections_format: string;
  signal_chains: unknown[] | null;
  plugins: string[] | null;
  creator_id: string;
  source_quality: string | null;
  view_count: number;
  created_at: string;
  updated_at: string;
  key_moments: KeyMomentSummary[];
  creator_info: CreatorInfo | null;
  related_links: RelatedLinkItem[];
  version_count: number;
  source_videos: SourceVideoSummary[];
}

export interface TechniquePageVersionSummary {
  version_number: number;
  created_at: string;
  pipeline_metadata: Record<string, unknown> | null;
}

export interface TechniquePageVersionListResponse {
  items: TechniquePageVersionSummary[];
  total: number;
}

export interface TechniquePageVersionDetail {
  version_number: number;
  content_snapshot: Record<string, unknown>;
  pipeline_metadata: Record<string, unknown> | null;
  created_at: string;
}

export interface TechniqueListItem {
  id: string;
  title: string;
  slug: string;
  topic_category: string;
  topic_tags: string[] | null;
  summary: string | null;
  creator_id: string;
  creator_name: string;
  creator_slug: string;
  source_quality: string | null;
  view_count: number;
  key_moment_count: number;
  created_at: string;
  updated_at: string;
}

export interface TechniqueListResponse {
  items: TechniqueListItem[];
  total: number;
  offset: number;
  limit: number;
}

export interface TechniqueListParams {
  limit?: number;
  offset?: number;
  category?: string;
  creator_slug?: string;
  sort?: string;
}

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

export async function fetchTechniques(
  params: TechniqueListParams = {},
): Promise<TechniqueListResponse> {
  const qs = new URLSearchParams();
  if (params.limit !== undefined) qs.set("limit", String(params.limit));
  if (params.offset !== undefined) qs.set("offset", String(params.offset));
  if (params.category) qs.set("category", params.category);
  if (params.creator_slug) qs.set("creator_slug", params.creator_slug);
  if (params.sort) qs.set("sort", params.sort);
  const query = qs.toString();
  return request<TechniqueListResponse>(
    `${BASE}/techniques${query ? `?${query}` : ""}`,
  );
}

export async function fetchTechnique(
  slug: string,
): Promise<TechniquePageDetail> {
  return request<TechniquePageDetail>(`${BASE}/techniques/${slug}`);
}

export async function fetchRandomTechnique(): Promise<{ slug: string }> {
  return request<{ slug: string }>(`${BASE}/techniques/random`);
}

export async function fetchTechniqueVersions(
  slug: string,
): Promise<TechniquePageVersionListResponse> {
  return request<TechniquePageVersionListResponse>(
    `${BASE}/techniques/${slug}/versions`,
  );
}

export async function fetchTechniqueVersion(
  slug: string,
  versionNumber: number,
): Promise<TechniquePageVersionDetail> {
  return request<TechniquePageVersionDetail>(
    `${BASE}/techniques/${slug}/versions/${versionNumber}`,
  );
}
