import type { TicketPriority, TicketStatus } from "../api/tickets"

export const TICKET_STATUS_META: Record<TicketStatus, { label: string; color: string }> = {
  open: { label: "未处理", color: "red" },
  pending: { label: "待客服", color: "orange" },
  processing: { label: "处理中", color: "blue" },
  waiting_user: { label: "等待用户", color: "cyan" },
  answered: { label: "已回复", color: "green" },
  resolved: { label: "已解决", color: "purple" },
  closed: { label: "已关闭", color: "default" },
}

export const TICKET_STATUS_TABS: Array<{ key: "all" | TicketStatus; label: string }> = [
  { key: "all", label: "全部" },
  { key: "open", label: TICKET_STATUS_META.open.label },
  { key: "pending", label: TICKET_STATUS_META.pending.label },
  { key: "processing", label: TICKET_STATUS_META.processing.label },
  { key: "waiting_user", label: TICKET_STATUS_META.waiting_user.label },
  { key: "answered", label: TICKET_STATUS_META.answered.label },
  { key: "resolved", label: TICKET_STATUS_META.resolved.label },
  { key: "closed", label: TICKET_STATUS_META.closed.label },
]

export type AdminTicketStatusGroup = "pending_staff" | "waiting_user" | "resolved" | "closed" | "all"

export const ADMIN_TICKET_STATUS_GROUP_TABS: Array<{
  key: AdminTicketStatusGroup
  label: string
  statuses: TicketStatus[]
}> = [
  { key: "pending_staff", label: "待处理", statuses: ["open", "pending", "processing"] },
  { key: "waiting_user", label: "等待用户", statuses: ["waiting_user", "answered"] },
  { key: "resolved", label: "已解决", statuses: ["resolved"] },
  { key: "closed", label: "已关闭", statuses: ["closed"] },
  {
    key: "all",
    label: "全部",
    statuses: ["open", "pending", "processing", "waiting_user", "answered", "resolved", "closed"],
  },
]

export function sumTicketStatuses(
  stats: Partial<Record<TicketStatus, number>>,
  statuses: TicketStatus[],
) {
  return statuses.reduce((total, status) => total + (stats[status] ?? 0), 0)
}

export const TICKET_PRIORITY_META: Record<TicketPriority, { label: string; color: string }> = {
  low: { label: "低", color: "default" },
  normal: { label: "普通", color: "blue" },
  high: { label: "高", color: "orange" },
  urgent: { label: "紧急", color: "red" },
}
