"use client";

import { useState } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { FileText, Download, Trash2, Loader2 } from "lucide-react";
import { format } from "date-fns";
import { de } from "date-fns/locale";
import { toast } from "sonner";
import { api } from "@/lib/api";
import type { Document } from "@/lib/types";

const DOC_TYPE_BADGE: Record<string, string> = {
  schriftsatz: "bg-blue-50 text-blue-700",
  beschluss: "bg-violet-50 text-violet-700",
  urteil: "bg-emerald-50 text-emerald-700",
  gutachten: "bg-amber-50 text-amber-700",
  vertrag: "bg-cyan-50 text-cyan-700",
  korrespondenz: "bg-neutral-100 text-neutral-600",
};

interface DocumentListProps {
  documents: Document[];
  caseId: string;
}

export function DocumentList({ documents, caseId }: DocumentListProps) {
  const [deleteId, setDeleteId] = useState<string | null>(null);
  const queryClient = useQueryClient();

  const deleteMutation = useMutation({
    mutationFn: (docId: string) => api.delete(`/documents/${docId}`),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["case-documents", caseId] });
      queryClient.invalidateQueries({ queryKey: ["case", caseId] });
      toast.success("Dokument geloescht");
      setDeleteId(null);
    },
    onError: (err) => {
      const msg =
        err && typeof err === "object" && "error" in err
          ? (err as { error: string }).error
          : "Unbekannter Fehler";
      toast.error(`Fehler beim Loeschen: ${msg}`);
      setDeleteId(null);
    },
  });

  if (documents.length === 0) {
    return (
      <p className="py-8 text-center text-sm text-neutral-400">
        Keine Dokumente vorhanden.
      </p>
    );
  }

  return (
    <div className="space-y-2">
      {documents.map((doc) => (
        <div
          key={doc.id}
          className="flex items-center justify-between rounded-md border border-neutral-200 bg-white px-4 py-3"
        >
          <div className="flex items-center gap-3 min-w-0">
            <FileText className="h-4 w-4 shrink-0 text-neutral-400" />
            <div className="min-w-0">
              <p className="truncate text-sm font-medium text-neutral-900">
                {doc.title}
              </p>
              <div className="mt-0.5 flex flex-wrap items-center gap-2 text-xs text-neutral-400">
                {doc.doc_type && (
                  <span
                    className={`rounded-full px-2 py-0.5 text-xs font-medium ${
                      DOC_TYPE_BADGE[doc.doc_type.toLowerCase()] ??
                      "bg-neutral-100 text-neutral-600"
                    }`}
                  >
                    {doc.doc_type}
                  </span>
                )}
                {doc.file_size != null && (
                  <span>{formatFileSize(doc.file_size)}</span>
                )}
                <span>
                  {format(new Date(doc.created_at), "d. MMM yyyy", {
                    locale: de,
                  })}
                </span>
              </div>
            </div>
          </div>

          <div className="flex items-center gap-1 shrink-0 ml-3">
            <a
              href={`/api/documents/${doc.id}`}
              className="rounded p-1.5 text-neutral-400 hover:bg-neutral-100 hover:text-neutral-600"
              title="Herunterladen"
            >
              <Download className="h-4 w-4" />
            </a>

            {deleteId === doc.id ? (
              <div className="flex items-center gap-1">
                <button
                  type="button"
                  onClick={() => deleteMutation.mutate(doc.id)}
                  disabled={deleteMutation.isPending}
                  className="rounded px-2 py-1 text-xs font-medium text-red-600 hover:bg-red-50"
                >
                  {deleteMutation.isPending ? (
                    <Loader2 className="h-3.5 w-3.5 animate-spin" />
                  ) : (
                    "Loeschen"
                  )}
                </button>
                <button
                  type="button"
                  onClick={() => setDeleteId(null)}
                  className="rounded px-2 py-1 text-xs text-neutral-500 hover:bg-neutral-100"
                >
                  Abbrechen
                </button>
              </div>
            ) : (
              <button
                type="button"
                onClick={() => setDeleteId(doc.id)}
                className="rounded p-1.5 text-neutral-400 hover:bg-neutral-100 hover:text-red-500"
                title="Loeschen"
              >
                <Trash2 className="h-4 w-4" />
              </button>
            )}
          </div>
        </div>
      ))}
    </div>
  );
}

function formatFileSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`;
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
