/**
 * Review Card Service
 *
 * 审核卡片服务 - 连接后端审核卡片API
 */
import api from './api'

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

export enum ReviewCardType {
  PROCESS_AUTO = 'process_auto',              // 过程中Agent自主识别
  POST_CREATION_AUTO = 'post_creation_auto', // 生成后自动审核
  USER_REQUEST_EPISODE = 'user_episode',     // 用户请求单集审核
  USER_REQUEST_MULTIPLE = 'user_multiple',   // 用户请求多集审核
  USER_REQUEST_ALL = 'user_all'              // 用户请求全集审核
}

export enum ReviewCardStatus {
  PENDING = 'pending',              // 待处理
  AWAITING_USER = 'awaiting_user',  // 待确认
  APPROVED = 'approved',            // 已通过
  REJECTED = 'rejected',            // 已驳回
}

export interface ReviewCard {
  id: string
  project_id: string
  episode_id?: string
  episode_numbers: number[]

  // 卡片类型和来源
  card_type: ReviewCardType
  trigger_source: string

  // 审核内容
  episode_outline?: string
  episode_content?: string
  review_reason: string

  // 审核结果
  issues: Array<{
    id?: string
    type: string
    severity: 'low' | 'medium' | 'high'
    description: string
    location?: string
    suggestion?: string
    ignored?: boolean
  }>
  overall_score?: number
  severity: 'low' | 'medium' | 'high'

  // 审核配置
  review_config_id?: string
  applied_skills: string[]
  applied_rules: string[]

  // 用户操作
  status: ReviewCardStatus
  user_comment?: string
  modified_content?: string

  // 时间戳
  created_at: string
  updated_at: string
  completed_at?: string

  // 附加信息
  metadata: Record<string, any>
}

export interface ReviewCardStats {
  total: number
  pending: number
  awaiting_user: number
  approved: number
  rejected: number
}

export interface ReviewCardCreateRequest {
  project_id: string
  episode_id?: string
  episode_numbers?: number[]
  card_type: ReviewCardType
  trigger_source: string
  review_reason: string
  episode_outline?: string
  episode_content?: string
  review_config_id?: string
  severity?: 'low' | 'medium' | 'high'
  issues?: Array<any>
  overall_score?: number
}

export interface ReviewCardUpdateRequest {
  status?: ReviewCardStatus
  user_comment?: string
  modified_content?: string
  issues?: Array<any>
  overall_score?: number
}

// ========================================
// API 方法
// ========================================

export const reviewCardService = {
  /**
   * 获取项目的审核卡片列表
   */
  listCards: async (projectId: string, status?: ReviewCardStatus): Promise<ReviewCard[]> => {
    const params: any = { project_id: projectId, limit: 100 }
    if (status) params.status = status
    return await api.get<ReviewCard[]>('/review-cards', { params })
  },

  /**
   * 获取审核卡片统计信息
   */
  getStats: async (projectId: string): Promise<ReviewCardStats> => {
    return await api.get<ReviewCardStats>(`/review-cards/stats/${projectId}`)
  },

  /**
   * 获取审核卡片详情
   */
  getCard: async (cardId: string): Promise<ReviewCard> => {
    return await api.get<ReviewCard>(`/review-cards/${cardId}`)
  },

  /**
   * 创建审核卡片
   */
  createCard: async (data: ReviewCardCreateRequest): Promise<{ success: boolean; card: ReviewCard }> => {
    return await api.post('/review-cards', data)
  },

  /**
   * 更新审核卡片
   */
  updateCard: async (cardId: string, data: ReviewCardUpdateRequest): Promise<{ success: boolean; card: ReviewCard }> => {
    return await api.put(`/review-cards/${cardId}`, data)
  },

  /**
   * 通过审核卡片
   */
  approveCard: async (cardId: string, userComment?: string): Promise<{ success: boolean; card: ReviewCard }> => {
    return await api.post(`/review-cards/${cardId}/approve`, null, {
      params: { user_comment: userComment }
    })
  },

  /**
   * 驳回审核卡片
   */
  rejectCard: async (cardId: string, userComment?: string, modifiedContent?: string): Promise<{ success: boolean; card: ReviewCard }> => {
    return await api.post(`/review-cards/${cardId}/reject`, null, {
      params: { user_comment: userComment, modified_content: modifiedContent }
    })
  },

  /**
   * 修改审核卡片内容
   */
  modifyCard: async (cardId: string, userComment?: string, modifiedContent?: string): Promise<{ success: boolean; card: ReviewCard }> => {
    return await api.post(`/review-cards/${cardId}/modify`, null, {
      params: { user_comment: userComment, modified_content: modifiedContent }
    })
  },

  /**
   * 删除审核卡片
   */
  deleteCard: async (cardId: string): Promise<{ success: boolean }> => {
    return await api.delete(`/review-cards/${cardId}`)
  }
}
