/**
 * Confirm Card API Service
 *
 * 确认卡片API服务 - 提供确认卡片的CRUD操作
 */
import api from './api'

// ============================================
// Types
// ============================================

export type ConfirmCardType =
  | 'direction_decision'       // 方向决策
  | 'plot_branch'              // 剧情分支
  | 'character_action'         // 角色行动
  | 'content_approval'         // 内容批准
  | 'resource_allocation'      // 资源分配

export type ConfirmCardStatus =
  | 'pending'                  // 待确认
  | 'approved'                 // 已确认
  | 'rejected'                 // 已拒绝
  | 'expired'                  // 已过期

export interface ConfirmCardOption {
  id: string
  label: string
  description: string
  implications?: string
}

export interface ConfirmCard {
  id: string
  project_id: string
  episode_number?: number
  card_type: ConfirmCardType
  title: string
  description: string
  context_data?: string
  options: ConfirmCardOption[]
  allow_custom_response: boolean
  status: ConfirmCardStatus
  selected_option_id?: string
  custom_response?: string
  user_notes?: string
  created_at: string
  expires_at?: string
  confirmed_at?: string
  metadata: Record<string, any>
}

export interface ConfirmCardCreate {
  project_id: string
  episode_number?: number
  card_type: ConfirmCardType
  title: string
  description: string
  context_data?: string
  options: ConfirmCardOption[]
  allow_custom_response?: boolean
  expires_at?: string
  metadata?: Record<string, any>
}

export interface ConfirmCardResponse {
  card_id: string
  status: ConfirmCardStatus
  selected_option?: string
  custom_response?: string
  confirmed_at?: string
}

// ============================================
// API Service
// ============================================

export const confirmCardService = {
  /**
   * 创建确认卡片
   */
  create: async (data: ConfirmCardCreate): Promise<{ success: boolean; card: ConfirmCard }> => {
    return await api.post('/confirm-cards', data)
  },

  /**
   * 简化创建确认卡片 (用于Agent快速推送)
   */
  createSimple: async (
    projectId: string,
    cardType: string,
    title: string,
    description: string,
    options?: Array<{ id?: string; label: string; description?: string; implications?: string } | string>,
    episodeNumber?: number,
    allowCustomResponse: boolean = false,
    expiresInHours?: number
  ): Promise<{ success: boolean; card_id: string; card: ConfirmCard }> => {
    return await api.post('/confirm-cards/simple', null, {
      params: {
        project_id: projectId,
        card_type: cardType,
        title,
        description,
        episode_number: episodeNumber,
        allow_custom_response: allowCustomResponse,
        expires_in_hours: expiresInHours
      },
      data: { options }
    })
  },

  /**
   * 获取确认卡片列表
   */
  list: async (
    projectId: string,
    status?: ConfirmCardStatus,
    includeExpired: boolean = false
  ): Promise<ConfirmCard[]> => {
    const params: Record<string, any> = {
      project_id: projectId,
      include_expired: includeExpired
    }
    if (status) params.status = status
    return await api.get('/confirm-cards', { params })
  },

  /**
   * 获取确认卡片详情
   */
  get: async (cardId: string): Promise<ConfirmCard> => {
    return await api.get(`/confirm-cards/${cardId}`)
  },

  /**
   * 删除确认卡片
   */
  delete: async (cardId: string): Promise<{ success: boolean }> => {
    return await api.delete(`/confirm-cards/${cardId}`)
  },

  /**
   * 确认卡片
   */
  confirm: async (
    cardId: string,
    selectedOptionId?: string,
    customResponse?: string,
    userNotes?: string
  ): Promise<{ success: boolean; card: ConfirmCard }> => {
    return await api.post(`/confirm-cards/${cardId}/confirm`, null, {
      params: {
        selected_option_id: selectedOptionId,
        custom_response: customResponse,
        user_notes: userNotes
      }
    })
  },

  /**
   * 拒绝确认卡片
   */
  reject: async (
    cardId: string,
    userNotes?: string
  ): Promise<{ success: boolean; card: ConfirmCard }> => {
    return await api.post(`/confirm-cards/${cardId}/reject`, null, {
      params: { user_notes: userNotes }
    })
  },

  /**
   * 清理过期卡片
   */
  cleanupExpired: async (): Promise<{ success: boolean; cleaned_count: number }> => {
    return await api.post('/confirm-cards/cleanup-expired')
  }
}

export default confirmCardService
