import type { TaskItem } from '../types/task'
import type { TaskDto } from '../services/taskService'

function toPriority(raw: string): TaskItem['priority'] {
  const p = raw.toLowerCase()
  if (p === 'high' || p === 'critical' || p === 'urgent') return 'high'
  if (p === 'low' || p === 'minor') return 'low'
  return 'medium'
}

function toStatus(raw: string): TaskItem['status'] {
  const s = raw.toLowerCase()
  if (s === 'in progress' || s === 'active' || s === 'working') return 'active'
  if (s === 'blocked' || s === 'block') return 'blocked'
  return 'pending'
}

function toProgress(raw: string): number {
  const s = raw.toLowerCase()
  if (s === 'in progress' || s === 'active' || s === 'working') return 50
  if (s === 'done') return 100
  if (s === 'blocked') return 30
  return 0
}

export function toTaskItem(dto: TaskDto): TaskItem {
  return {
    id: dto.id,
    title: dto.title,
    agent: dto.assignedTo ?? '—',
    priority: toPriority(dto.priority),
    status: toStatus(dto.state),
    progress: toProgress(dto.state),
  }
}
