import { request } from './request'

// 类型定义
export interface InitInterviewResponse {
  sessionId: string
  name: string
  debugUrl?: string
  workflowResponse?: unknown
}

export interface SubmitCandidateResponse {
  sessionId: string
  fileId: string
}

export interface CreateRoomRequest {
  sessionId?: string
  fileId?: string
}

export interface CreateRoomResponse {
  roomId: string
  token: string
  appId: string
  userId: string
  sessionId?: string  // 后端生成的会话ID
}

export interface ChatRequest {
  sessionId: string
  message: string
  conversationId?: string
}

export interface ChatResponse {
  reply: string
  conversationId: string
  debugInfo?: {
    status_history?: Array<{
      iteration: number
      status: string
      required_action?: any
    }>
    messages?: Array<{
      role: string
      type: string
      content: string
    }>
    raw_responses?: any[]
  }
}

export interface Candidate {
  sessionId: string
  name: string
  status: 'pending' | 'ongoing' | 'completed'
  score?: number
  createdAt: string
}

export interface CandidateDetail extends Candidate {
  resume: string
  currentStage: number
  scores?: {
    salesSkill: number
    salesMindset: number
    quality: number
    motivation: number
    total: number
  }
  analysis?: string
  interviewLog?: string
  completedAt?: string
}

export interface CandidateListResponse {
  list: Candidate[]
  total: number
  page: number
  pageSize: number
}

export interface CandidateListParams {
  page?: number
  pageSize?: number
  keyword?: string
  status?: string
  startDate?: string
  endDate?: string
}

// API 方法
export const candidateApi = {
  /**
   * 初始化面试（工作流 A）
   * 上传简历 + 执行初始化工作流 → 返回 sessionId
   */
  initInterview(name: string, resumeFile: File) {
    const formData = new FormData()
    formData.append('name', name)
    formData.append('file', resumeFile)
    return request.upload<InitInterviewResponse>('/init-interview', formData)
  },

  /**
   * 上传简历到 Coze
   */
  uploadResume(file: File) {
    const formData = new FormData()
    formData.append('file', file)
    return request.upload<{ fileId: string }>('/upload', formData)
  },

  /**
   * 提交候选人信息（上传简历）- 旧方法
   */
  submit(name: string, resumeFile: File) {
    const formData = new FormData()
    formData.append('name', name)
    formData.append('resume', resumeFile)
    return request.upload<SubmitCandidateResponse>('/candidates', formData)
  },

  /**
   * 创建语音房间
   */
  createRoom(data: CreateRoomRequest) {
    return request.post<CreateRoomResponse>('/rooms', data)
  },

  /**
   * 结束面试
   */
  endInterview(sessionId: string) {
    return request.post<{ success: boolean }>(`/interviews/${sessionId}/end`)
  },

  /**
   * 获取候选人列表
   */
  getList(params: CandidateListParams) {
    return request.get<CandidateListResponse>('/candidates', { params })
  },

  /**
   * 获取候选人详情
   */
  getDetail(sessionId: string) {
    return request.get<CandidateDetail>(`/candidates/${sessionId}`)
  },

  /**
   * 导出 PDF 报告
   */
  exportPdf(sessionId: string) {
    return `/api/candidates/${sessionId}/export`
  },

  /**
   * 文本对话（模拟语音）
   */
  chat(data: ChatRequest) {
    return request.post<ChatResponse>('/chat', data)
  },

  /**
   * 获取 Coze Realtime SDK 配置
   * 用于直接连接 Coze 语音服务
   */
  getCozeConfig() {
    return request.get<{
      accessToken: string
      botId: string
      voiceId: string
      connectorId: string
    }>('/coze-config')
  },
}
