/**
 * 视频生成模块 - 前端 API 调用封装
 */

import { request, getFullUrl } from './request';
import type { VideoConfig, VideoProjectStatus } from './types/video-generator';

// ============ 类型定义 ============

/** 创建视频项目请求 */
export interface CreateVideoProjectRequest {
  title: string;
  description?: string;
  coverUrl?: string;
  config?: VideoConfig;
  bookId?: number;
  audioId?: number;
}

/** 更新视频项目请求 */
export interface UpdateVideoProjectRequest {
  title?: string;
  description?: string;
  coverUrl?: string;
  config?: VideoConfig;
}

/** 视频项目响应 */
export interface VideoProjectResponse {
  id: number;
  userId: number | null;
  title: string;
  description: string | null;
  coverUrl: string | null;
  config?: VideoConfig;
  outputUrl: string | null;
  duration: number | null;
  fileSize: number | null;
  bookId: number | null;
  audioId: number | null;
  status: VideoProjectStatus;
  progress: number;
  errorMsg: string | null;
  createdAt: string;
  updatedAt: string;
}

/** 素材响应 */
export interface VideoMaterialResponse {
  id: number;
  userId: number | null;
  type: 'image' | 'audio' | 'template';
  name: string;
  url: string;
  thumbnail: string | null;
  tags: string[];
  category: string | null;
  duration: number | null;
  size: number | null;
  width: number | null;
  height: number | null;
  createdAt: string;
  updatedAt: string;
}

/** 生成进度响应 */
export interface GenerateProgressResponse {
  status: VideoProjectStatus;
  progress: number;
  outputUrl?: string;
  duration?: number;
  fileSize?: number;
  errorMsg?: string;
}

// ============ API 函数 ============

/**
 * 获取视频项目列表
 */
export function getVideoProjects(params?: {
  userId?: number;
  status?: VideoProjectStatus;
  page?: number;
  pageSize?: number;
}) {
  return request<{
    items: VideoProjectResponse[];
    total: number;
    page: number;
    pageSize: number;
  }>('/video/projects', { method: 'GET', data: params });
}

/**
 * 创建视频项目
 */
export function createVideoProject(data: CreateVideoProjectRequest) {
  return request<VideoProjectResponse>('/video/projects', { method: 'POST', data });
}

/**
 * 获取视频项目详情
 */
export function getVideoProject(id: number) {
  return request<VideoProjectResponse>(`/video/projects/${id}`, { method: 'GET' });
}

/**
 * 更新视频项目
 */
export function updateVideoProject(id: number, data: UpdateVideoProjectRequest) {
  return request<VideoProjectResponse>(`/video/projects/${id}`, { method: 'PUT', data });
}

/**
 * 删除视频项目
 */
export function deleteVideoProject(id: number) {
  return request<void>(`/video/projects/${id}`, { method: 'DELETE' });
}

/**
 * 开始生成视频
 */
export function generateVideo(id: number) {
  return request<{
    outputUrl: string;
    duration: number;
    fileSize: number;
  }>(`/video/projects/${id}/generate`, { method: 'POST' });
}

/**
 * 获取生成进度
 */
export function getGenerateProgress(id: number) {
  return request<GenerateProgressResponse>(`/video/projects/${id}/status`, { method: 'GET' });
}

/**
 * 获取素材列表
 */
export function getMaterials(params?: {
  userId?: number;
  type?: 'image' | 'audio' | 'template';
  category?: string;
  page?: number;
  pageSize?: number;
}) {
  return request<{
    items: VideoMaterialResponse[];
    total: number;
  }>('/video/materials', { method: 'GET', data: params });
}

/**
 * 上传素材
 */
export function uploadMaterial(data: {
  type: 'image' | 'audio' | 'template';
  name: string;
  url: string;
  thumbnail?: string;
  tags?: string[];
  category?: string;
  duration?: number;
  size?: number;
  width?: number;
  height?: number;
}) {
  return request<VideoMaterialResponse>('/video/materials/upload', { method: 'POST', data });
}

/**
 * 删除素材
 */
export function deleteMaterial(id: number) {
  return request<void>(`/video/materials/${id}`, { method: 'DELETE' });
}

/**
 * 从书籍生成视频项目
 */
export function createVideoProjectFromBook(bookId: number) {
  return request<VideoProjectResponse>(`/video/books/${bookId}/generate`, { method: 'POST' });
}
