/**
 * 任务管理API
 */
import { http } from '@/utils/http'
import type { ResponseModel } from '@/types/practice'

/**
 * 任务接口
 */
export interface Task {
  id: number
  title: string
  description?: string
  priority: string
  status: string
  creator_id: number
  deadline?: string
  requirements?: any
  progress: number
  created_at: string
  updated_at: string
  courses: string[]
  assigned_count: number
  completed_count: number
}

/**
 * 任务创建请求
 */
export interface TaskCreate {
  title: string
  description?: string
  priority: string
  deadline?: string
  course_ids: number[]
  user_ids: number[]
  requirements?: any
}

/**
 * 任务更新请求
 */
export interface TaskUpdate {
  title?: string
  description?: string
  priority?: string
  status?: string
  deadline?: string
  progress?: number
}

/**
 * 任务统计响应
 */
export interface TaskStats {
  total: number
  ongoing: number
  completed: number
  expired: number
  avg_completion_rate: number
}

/**
 * 获取任务列表
 */
export function getTasks(params?: {
  status?: string
  page?: number
  page_size?: number
}): Promise<ResponseModel<{ items: Task[]; total: number; page: number; page_size: number; total_pages: number }>> {
  return http.get('/api/v1/manager/tasks', { params })
}

/**
 * 获取任务统计
 */
export function getTaskStats(): Promise<ResponseModel<TaskStats>> {
  return http.get('/api/v1/manager/tasks/stats')
}

/**
 * 获取任务详情
 */
export function getTaskDetail(id: number): Promise<ResponseModel<Task>> {
  return http.get(`/api/v1/manager/tasks/${id}`)
}

/**
 * 创建任务
 */
export function createTask(data: TaskCreate): Promise<ResponseModel<Task>> {
  return http.post('/api/v1/manager/tasks', data)
}

/**
 * 更新任务
 */
export function updateTask(id: number, data: TaskUpdate): Promise<ResponseModel<Task>> {
  return http.put(`/api/v1/manager/tasks/${id}`, data)
}

/**
 * 删除任务
 */
export function deleteTask(id: number): Promise<ResponseModel<void>> {
  return http.delete(`/api/v1/manager/tasks/${id}`)
}

