import type { TicketStatus } from "@/lib/schemas/ticket"

export type TicketStatusSummaryTone = "default" | "info" | "warning" | "success" | "muted" | "danger"

export type TicketStatusStyle = {
  label: string
  summaryTone: TicketStatusSummaryTone
  chipClass: string
  badgeClass: string
  tableTextClass: string
  dotClass: string
}

const FALLBACK_STATUS: TicketStatus = "PENDING"

const META: Record<TicketStatus, TicketStatusStyle> = {
  PENDING: {
    label: "Pendente",
    summaryTone: "muted",
    chipClass: "bg-slate-100 text-neutral-700 ring-1 ring-slate-200",
    badgeClass: "border border-slate-200 bg-slate-100 text-neutral-700",
    tableTextClass: "text-slate-700",
    dotClass: "bg-slate-400",
  },
  AWAITING_ATTENDANCE: {
    label: "Em andamento",
    summaryTone: "info",
    chipClass: "bg-sky-100 text-sky-700 ring-1 ring-sky-200",
    badgeClass: "border border-sky-200 bg-sky-100 text-sky-700",
    tableTextClass: "text-[#0a4760]",
    dotClass: "bg-sky-500",
  },
  PAUSED: {
    label: "Pausado",
    summaryTone: "warning",
    chipClass: "bg-amber-100 text-amber-800 ring-1 ring-amber-200",
    badgeClass: "border border-amber-200 bg-amber-50 text-amber-700",
    tableTextClass: "text-[#7a5901]",
    dotClass: "bg-amber-400",
  },
  RESOLVED: {
    label: "Resolvido",
    summaryTone: "success",
    chipClass: "bg-emerald-100 text-emerald-700 ring-1 ring-emerald-200",
    badgeClass: "border border-emerald-200 bg-emerald-50 text-emerald-700",
    tableTextClass: "text-[#1f6a45]",
    dotClass: "bg-emerald-500",
  },
}

function resolveMeta(status: TicketStatus | string | null | undefined): TicketStatusStyle {
  if (!status) return META[FALLBACK_STATUS]
  const normalized = status.toUpperCase() as TicketStatus
  return META[normalized] ?? META[FALLBACK_STATUS]
}

export function getTicketStatusMeta(status: TicketStatus | string | null | undefined): TicketStatusStyle {
  return resolveMeta(status)
}

export function getTicketStatusLabel(status: TicketStatus | string | null | undefined): string {
  return resolveMeta(status).label
}

export function getTicketStatusChipClass(status: TicketStatus | string | null | undefined): string {
  return resolveMeta(status).chipClass
}

export function getTicketStatusBadgeClass(status: TicketStatus | string | null | undefined): string {
  return resolveMeta(status).badgeClass
}

export function getTicketStatusTextClass(status: TicketStatus | string | null | undefined): string {
  return resolveMeta(status).tableTextClass
}

export function getTicketStatusDotClass(status: TicketStatus | string | null | undefined): string {
  return resolveMeta(status).dotClass
}

export function getTicketStatusSummaryTone(status: TicketStatus | string | null | undefined): TicketStatusSummaryTone {
  return resolveMeta(status).summaryTone
}

export const TICKET_STATUS_META = META
