/**
 * 剧集内容服务
 */
import api from './api'

export type ContentStatus = 'draft' | 'generating' | 'pending_review' | 'approved' | 'rejected'

export type ExportFormat = 'markdown' | 'txt' | 'pdf' | 'docx'

export interface EpisodeContent {
  id: string
  project_id: string
  episode_number: number
  title?: string
  content: string
  status: ContentStatus
  quality_score?: number
  review_issues: any[]
  created_at: string
  updated_at: string
  created_by: string
  generation_params?: Record<string, any>
  version: number
  parent_version?: number
}

export interface EpisodeContentCreate {
  project_id: string
  episode_number: number
  title?: string
  content: string
  generation_params?: Record<string, any>
}

export interface EpisodeContentUpdate {
  title?: string
  content?: string
  status?: ContentStatus
}

export interface EpisodeContentReview {
  approved: boolean
  comment?: string
}

export interface ExportRequest {
  format: ExportFormat
  episode_numbers?: number[]
  include_metadata?: boolean
}

export interface ExportResult {
  success: boolean
  format: ExportFormat
  content?: string
  filename?: string
  message?: string
  markdown_content?: string
}

export const episodeContentService = {
  /**
   * 列出项目的所有剧集内容
   * @param projectId 项目 ID
   * @param status 筛选状态（可选）
   * @param episodeNumber 筛选集数（可选）
   * @returns 剧集内容列表
   */
  listContents: async (
    projectId: string,
    status?: ContentStatus,
    episodeNumber?: number
  ): Promise<EpisodeContent[]> => {
    const params: any = {}
    if (status) params.status = status
    if (episodeNumber !== undefined) params.episode_number = episodeNumber

    return await api.get<EpisodeContent[]>(`/episodes/${projectId}`, { params })
  },

  /**
   * 获取指定集数的内容
   * @param projectId 项目 ID
   * @param episodeNumber 集数
   * @returns 剧集内容
   */
  getContent: async (projectId: string, episodeNumber: number): Promise<EpisodeContent> => {
    return await api.get<EpisodeContent>(`/episodes/${projectId}/${episodeNumber}`)
  },

  /**
   * 创建新的剧集内容
   * @param data 创建数据
   * @returns 创建的剧集内容
   */
  createContent: async (data: EpisodeContentCreate): Promise<EpisodeContent> => {
    return await api.post<EpisodeContent>('/episodes', data)
  },

  /**
   * 更新剧集内容
   * @param contentId 内容 ID
   * @param data 更新数据
   * @returns 更新后的剧集内容
   */
  updateContent: async (contentId: string, data: EpisodeContentUpdate): Promise<EpisodeContent> => {
    return await api.put<EpisodeContent>(`/episodes/${contentId}`, data)
  },

  /**
   * 审核剧集内容
   * @param contentId 内容 ID
   * @param review 审核数据
   * @returns 审核后的剧集内容
   */
  reviewContent: async (contentId: string, review: EpisodeContentReview): Promise<EpisodeContent> => {
    return await api.post<EpisodeContent>(`/episodes/${contentId}/review`, review)
  },

  /**
   * 导出剧集内容
   * @param projectId 项目 ID
   * @param request 导出请求
   * @returns 导出结果
   */
  exportContents: async (projectId: string, request: ExportRequest): Promise<ExportResult> => {
    return await api.post<ExportResult>(`/episodes/${projectId}/export`, request)
  },

  /**
   * 删除剧集内容
   * @param contentId 内容 ID
   * @returns 删除结果
   */
  deleteContent: async (contentId: string): Promise<{ success: boolean; message: string }> => {
    return await api.delete(`/episodes/${contentId}`)
  }
}
