import { notFound } from "next/navigation";
import { FlightForm } from "@/components/admin/flight/FlightForm";
import { prisma } from "@/lib/prisma";
import { updateFlight } from "../../actions";

export default async function EditFlightPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const flightId = Number(id);

  if (Number.isNaN(flightId)) {
    notFound();
  }

  const flight = await prisma.flight.findUnique({ where: { id: flightId } });

  if (!flight) {
    notFound();
  }

  return (
    <div>
      <h1 className="text-2xl font-semibold text-heading">
        Edit Tiket Pesawat
      </h1>
      <p className="mt-1 text-sm text-body">
        {flight.airline} · {flight.flightNumber}
      </p>

      <div className="mt-6 rounded-2xl bg-white p-8 shadow-sm">
        <FlightForm
          action={updateFlight.bind(null, flightId)}
          flight={flight}
          submitLabel="Simpan Perubahan"
        />
      </div>
    </div>
  );
}
