import { createClient } from "@/lib/supabase/server";
import { notFound } from "next/navigation";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { ArrowLeft } from "lucide-react";
import { ConsultationForm } from "@/components/medical/consultation-form";

export const dynamic = "force-dynamic";

export default async function NewConsultationPage({ params }: { params: Promise<{ patientId: string }> }) {
  const { patientId } = await params;
  const supabase = await createClient();
  const { data: patient, error } = await supabase.from("patients").select("id, first_name, last_name").eq("id", patientId).single();
  if (error || !patient) notFound();

  return (
    <div className="mx-auto max-w-3xl space-y-6">
      <div className="flex items-center gap-4">
        <Link href={`/medical-records/${patientId}`}><Button variant="ghost" size="icon"><ArrowLeft className="h-4 w-4" /></Button></Link>
        <div>
          <h1 className="text-2xl font-bold">Nueva consulta</h1>
          <p className="text-sm text-muted-foreground">Paciente: {patient.first_name} {patient.last_name}</p>
        </div>
      </div>
      <ConsultationForm patientId={patientId} />
    </div>
  );
}
