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

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

export interface CreatorBrowseItem {
  id: string;
  name: string;
  slug: string;
  genres: string[] | null;
  folder_name: string;
  view_count: number;
  created_at: string;
  updated_at: string;
  technique_count: number;
  video_count: number;
  last_technique_at: string | null;
}

export interface CreatorBrowseResponse {
  items: CreatorBrowseItem[];
  total: number;
  offset: number;
  limit: number;
}

export interface CreatorTechniqueItem {
  title: string;
  slug: string;
  topic_category: string;
  summary: string | null;
  topic_tags: string[] | null;
  key_moment_count: number;
  created_at: string;
}

export interface CreatorDetailResponse {
  id: string;
  name: string;
  slug: string;
  genres: string[] | null;
  folder_name: string;
  view_count: number;
  created_at: string;
  updated_at: string;
  video_count: number;
  bio: string | null;
  social_links: Record<string, string> | null;
  featured: boolean;
  avatar_url: string | null;
  technique_count: number;
  moment_count: number;
  techniques: CreatorTechniqueItem[];
  genre_breakdown: Record<string, number>;
}

export interface CreatorListParams {
  sort?: string;
  genre?: string;
  limit?: number;
  offset?: number;
}

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

export async function fetchCreators(
  params: CreatorListParams = {},
): Promise<CreatorBrowseResponse> {
  const qs = new URLSearchParams();
  if (params.sort) qs.set("sort", params.sort);
  if (params.genre) qs.set("genre", params.genre);
  if (params.limit !== undefined) qs.set("limit", String(params.limit));
  if (params.offset !== undefined) qs.set("offset", String(params.offset));
  const query = qs.toString();
  return request<CreatorBrowseResponse>(
    `${BASE}/creators${query ? `?${query}` : ""}`,
  );
}

export async function fetchCreator(
  slug: string,
): Promise<CreatorDetailResponse> {
  return request<CreatorDetailResponse>(`${BASE}/creators/${slug}`);
}
