import Link from "next/link";
import type { DashboardSummary } from "@/types";

function getAlertClasses(kind: string) {
  if (kind === "conflict") {
    return "border-red-300 bg-red-50";
  }

  if (kind === "late") {
    return "border-rose-300 bg-rose-50";
  }

  if (kind === "urgent") {
    return "border-orange-300 bg-orange-50";
  }

  if (kind === "payment") {
    return "border-purple-200 bg-purple-50";
  }

  if (kind === "invoice") {
    return "border-yellow-200 bg-yellow-50";
  }

  if (kind === "assignment") {
    return "border-blue-200 bg-blue-50";
  }

  return "border-gray-200 bg-gray-50";
}

function getAlertLabel(kind: string) {
  if (kind === "conflict") return "Conflict";
  if (kind === "late") return "Late";
  if (kind === "urgent") return "Urgent";
  if (kind === "payment") return "Payment";
  if (kind === "invoice") return "Invoice";
  if (kind === "assignment") return "Assignment";
  return "Alert";
}

export function OperationalAlertsCard({ alerts }: { alerts: DashboardSummary["alerts"] }) {
  return (
    <section className="rounded-2xl border bg-white p-5 shadow-sm">
      <div className="mb-4 flex items-center justify-between">
        <h3 className="text-base font-semibold">Operational Alerts</h3>
        <span className="text-sm text-gray-500">{alerts.length}</span>
      </div>
      {alerts.length ? (
        <div className="space-y-3">
          {alerts.map((alert) => (
            <div key={alert.id} className={`rounded-xl border p-3 ${getAlertClasses(alert.kind)}`}>
              <div className="mb-2 inline-flex rounded-full border border-current/10 bg-white/70 px-2 py-0.5 text-[11px] font-semibold uppercase tracking-wide text-gray-700">
                {getAlertLabel(alert.kind)}
              </div>
              <div className="text-sm font-medium text-gray-900">{alert.headline ?? alert.message}</div>
              {alert.contextLine ? <div className="mt-1 text-xs text-gray-600">{alert.contextLine}</div> : null}
              {alert.referenceLine ? <div className="mt-1 text-xs text-gray-500">{alert.referenceLine}</div> : null}
              {alert.supportLine ? <div className="mt-1 text-xs text-gray-500">{alert.supportLine}</div> : null}
              <div className="mt-2 flex flex-wrap gap-3">
                {alert.loadId ? (
                  <Link href={`/loads/${alert.loadId}`} className="text-sm font-medium text-blue-700 hover:underline">
                    Open load
                  </Link>
                ) : null}
                <Link
                  href={`/cori?source=dispatch&topic=load&question=${encodeURIComponent(`What should I do next with this alert: ${alert.headline ?? alert.message}`)}${alert.headline ? `&title=${encodeURIComponent(alert.headline)}` : ''}${alert.contextLine ? `&context=${encodeURIComponent([alert.contextLine, alert.referenceLine, alert.supportLine].filter(Boolean).join(' · '))}` : alert.referenceLine || alert.supportLine ? `&context=${encodeURIComponent([alert.referenceLine, alert.supportLine].filter(Boolean).join(' · '))}` : ''}`}
                  className="text-sm font-medium text-gray-700 hover:underline"
                >
                  Ask CORI
                </Link>
              </div>
            </div>
          ))}
        </div>
      ) : (
        <div className="text-sm text-gray-500">No operational alerts.</div>
      )}
    </section>
  );
}
