export const API_BASE = process.env.NEXT_PUBLIC_KLAUSUR_SERVICE_URL || 'http://localhost:8086'

export interface Task {
  id: string
  title: string
  description: string
  status: 'pending' | 'in_progress' | 'completed'
  priority: 'urgent' | 'high' | 'medium' | 'low'
  deadline: string | null
  emailId: string | null
  sourceSubject: string | null
  sourceSender: string | null
  senderType: string | null
  aiExtracted: boolean
  confidenceScore: number | null
  createdAt: string
  updatedAt: string
}

export interface DashboardStats {
  totalTasks: number
  pendingTasks: number
  inProgressTasks: number
  completedTasks: number
  overdueTasks: number
  dueToday: number
  dueThisWeek: number
  byPriority: Record<string, number>
  bySenderType: Record<string, number>
}

export type FilterStatus = 'all' | 'pending' | 'in_progress' | 'completed'
export type FilterPriority = 'all' | 'urgent' | 'high' | 'medium' | 'low'

export const priorityColors: Record<string, { bg: string; text: string; border: string }> = {
  urgent: { bg: 'bg-red-50', text: 'text-red-700', border: 'border-red-200' },
  high: { bg: 'bg-orange-50', text: 'text-orange-700', border: 'border-orange-200' },
  medium: { bg: 'bg-yellow-50', text: 'text-yellow-700', border: 'border-yellow-200' },
  low: { bg: 'bg-green-50', text: 'text-green-700', border: 'border-green-200' },
}

export const priorityLabels: Record<string, string> = {
  urgent: 'Dringend',
  high: 'Hoch',
  medium: 'Mittel',
  low: 'Niedrig',
}

export const statusLabels: Record<string, string> = {
  pending: 'Offen',
  in_progress: 'In Bearbeitung',
  completed: 'Erledigt',
}

export const getOverdueIndicator = (deadline: string | null) => {
  if (!deadline) return null
  const deadlineDate = new Date(deadline)
  const now = new Date()
  const diffDays = Math.ceil((deadlineDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24))

  if (diffDays < 0) {
    return { label: 'Ueberfaellig', color: 'bg-red-100 text-red-800', urgent: true }
  } else if (diffDays === 0) {
    return { label: 'Heute', color: 'bg-orange-100 text-orange-800', urgent: true }
  } else if (diffDays <= 3) {
    return { label: `In ${diffDays} Tag${diffDays > 1 ? 'en' : ''}`, color: 'bg-yellow-100 text-yellow-800', urgent: false }
  } else if (diffDays <= 7) {
    return { label: `In ${diffDays} Tagen`, color: 'bg-blue-100 text-blue-800', urgent: false }
  }
  return null
}
