"use client";

import { useQuery } from "@tanstack/react-query";
import { useParams } from "next/navigation";
import { api } from "@/lib/api";
import type { Case, CaseEvent } from "@/lib/types";
import { CaseTimeline } from "@/components/cases/CaseTimeline";
import { Loader2 } from "lucide-react";

interface CaseDetail extends Case {
  recent_events: CaseEvent[];
}

export default function VerlaufPage() {
  const { id } = useParams<{ id: string }>();

  const { data: caseDetail, isLoading } = useQuery({
    queryKey: ["case", id],
    queryFn: () => api.get<CaseDetail>(`/cases/${id}`),
  });

  if (isLoading) {
    return (
      <div className="flex items-center justify-center py-8">
        <Loader2 className="h-5 w-5 animate-spin text-neutral-400" />
      </div>
    );
  }

  const events = Array.isArray(caseDetail?.recent_events)
    ? caseDetail.recent_events
    : [];

  return <CaseTimeline events={events} />;
}
