/**
 * 项目 API 服务
 */
import api from './api'

export interface CharacterProfile {
  name: string
  description?: string
  personality?: string
  speechStyle?: string
  currentState?: string
  relationships?: Record<string, string>
}

export interface SceneSetting {
  name: string
  description?: string
  atmosphere?: string
}

export interface GlobalContext {
  worldSetting?: string
  characterProfiles?: Record<string, CharacterProfile>
  sceneSettings?: Record<string, SceneSetting>
  overallOutline?: string
  styleGuide?: string
  uploadedScript?: string
  inspiration?: string
}

// Skills 配置相关类型
export interface SkillAssignment {
  skill_id: string
  enabled: boolean
  priority: number
  parameter_overrides?: Record<string, any>
}

export interface TaskSkillConfig {
  task_type: 'dialogue' | 'review' | 'outline' | 'structure' | 'scene' | 'consistency'
  skills: SkillAssignment[]
}

export interface EpisodeSkillOverride {
  episode_number: number
  task_configs: TaskSkillConfig[]
  use_project_default: boolean
}

export interface SeriesProject {
  id: string
  name: string
  type: string
  agentId: string
  mode: string
  genre?: string
  globalContext: GlobalContext
  totalEpisodes: number
  // 三级 Skills 配置
  defaultTaskSkills: TaskSkillConfig[]
  episodeSkillOverrides: Record<number, EpisodeSkillOverride>
  // 保留兼容
  skillSettings: Record<string, any>
  // 剧集列表（用于计算完成度）
  episodes?: Episode[]
  createdAt: string
  updatedAt: string
}

export interface Episode {
  id: string
  projectId: string
  number: number
  title: string
  status: string
  content?: string
  outline?: string
  qualityScore?: number
  issues: Array<{
    type: string
    description: string
    severity: string
    suggestion?: string
  }>
  createdAt: string
  completedAt?: string
}

export interface ProjectCreateRequest {
  name: string
  totalEpisodes?: number
  agentId?: string
  mode?: string
  genre?: string
  globalContext?: GlobalContext
  // 三级 Skills 配置
  defaultTaskSkills?: TaskSkillConfig[]
  episodeSkillOverrides?: Record<number, EpisodeSkillOverride>
  // 保留兼容
  skillSettings?: Record<string, any>
}

export const projectService = {
  // 创建项目
  createProject: async (data: ProjectCreateRequest) => {
    return await api.post<SeriesProject>('/projects', data)
  },

  // 列出项目
  listProjects: async () => {
    return await api.get<SeriesProject[]>('/projects')
  },

  // 获取项目详情
  getProject: async (projectId: string) => {
    return await api.get<SeriesProject>(`/projects/${projectId}`)
  },

  // 更新项目
  updateProject: async (projectId: string, data: Partial<SeriesProject>) => {
    return await api.put<SeriesProject>(`/projects/${projectId}`, data)
  },

  // 删除项目
  deleteProject: async (projectId: string) => {
    return await api.delete(`/projects/${projectId}`)
  },

  // 列出剧集
  listEpisodes: async (projectId: string) => {
    return await api.get<Episode[]>(`/projects/${projectId}/episodes`)
  },

  // 获取指定剧集
  getEpisode: async (projectId: string, episodeNumber: number) => {
    return await api.get<Episode>(`/projects/${projectId}/episodes/${episodeNumber}`)
  },

  // 更新剧集内容
  updateEpisode: async (projectId: string, episodeNumber: number, data: Partial<Episode>) => {
    return await api.put<Episode>(`/projects/${projectId}/episodes/${episodeNumber}`, data)
  },

  // 执行单集创作
  executeEpisode: async (
    projectId: string,
    episodeNumber: number,
    title?: string
  ) => {
    return await api.post<{
      episode: Episode
      success: boolean
      message: string
    }>(`/projects/${projectId}/execute`, {
      episodeNumber,
      title: title || `第${episodeNumber}集`,
    })
  },

  // 批量执行
  executeBatch: async (
    projectId: string,
    startEpisode: number,
    endEpisode: number
  ) => {
    return await api.post(`/projects/${projectId}/execute-batch`, null, {
      params: { start_episode: startEpisode, end_episode: endEpisode }
    })
  },

  // 获取记忆系统
  getMemory: async (projectId: string) => {
    return await api.get(`/projects/${projectId}/memory`)
  },
}
