import { request, uploadFile } from "@/api/client";
import type { AiTask, AssemblyTask, PageResult, ProjectDetail, ProjectSummary, UploadResult } from "@/types/api";

export interface CreateProjectPayload {
  name: string;
  description?: string;
  type?: string;
  aspectRatio?: string;
  resolution?: string;
  metadata?: Record<string, unknown>;
}

export async function listProjects() {
  const result = await request<PageResult<ProjectSummary> | ProjectSummary[]>("/projects");
  if (Array.isArray(result)) return result;
  return result.records || result.list || result.content || [];
}

export function getProject(projectId: string | number) {
  return request<ProjectDetail>(`/projects/${projectId}`);
}

export function createProject(payload: CreateProjectPayload) {
  return request<ProjectDetail>("/projects", {
    method: "POST",
    data: payload
  });
}

export function uploadScript(projectId: string | number, filePath: string) {
  return uploadFile<UploadResult>(`/projects/${projectId}/assets?assetType=script`, filePath);
}

export function listVideoTasks(projectId: string | number) {
  return request<AiTask[]>(`/projects/${projectId}/video-tasks`);
}

export function getAssemblyTask(projectId: string | number, episodeId: string | number) {
  return request<AssemblyTask | null>(`/projects/${projectId}/episodes/${episodeId}/assembly`);
}
