import { notFound } from 'next/navigation';
import { prisma } from '@/lib/prisma';
import ContractSigningForm from '@/components/ContractSigningForm';

export default async function ContractSignPage({ params }: { params: { token: string } }) {
  // Decode token to get booking ID
  const decoded = Buffer.from(params.token, 'base64url').toString();
  const bookingId = decoded.split('-')[0];

  const booking = await prisma.booking.findUnique({
    where: { id: bookingId },
    include: {
      location: true,
      photobox: true,
    },
  });

  if (!booking) {
    notFound();
  }

  // Check if already signed
  if (booking.contractSigned) {
    return (
      <div className="min-h-screen bg-gray-50 flex items-center justify-center p-4">
        <div className="max-w-2xl w-full bg-white rounded-xl shadow-lg p-8 text-center">
          <div className="text-6xl mb-4">✅</div>
          <h1 className="text-2xl font-bold text-gray-900 mb-2">
            Vertrag bereits unterschrieben
          </h1>
          <p className="text-gray-600">
            Dieser Vertrag wurde bereits am {booking.contractSignedAt?.toLocaleDateString('de-DE')} unterschrieben.
          </p>
        </div>
      </div>
    );
  }

  return (
    <div className="min-h-screen bg-gray-50 py-8 px-4">
      <ContractSigningForm booking={booking} location={booking.location} photobox={booking.photobox} token={params.token} />
    </div>
  );
}
