import { PrismaClient } from '@prisma/client';
import { submitOffer } from './actions';
import { notFound } from 'next/navigation';

export const dynamic = 'force-dynamic';

const prisma = new PrismaClient();

export default async function MakeOfferPage({ params }: { params: { id: string } }) {
  const propertyId = parseInt(params.id);
  const property = await prisma.property.findUnique({ where: { id: propertyId } });
  
  if (!property) return notFound();

  // Bind the propertyId to the server action
  const submitWithId = submitOffer.bind(null, propertyId);

  return (
    <div className="max-w-2xl mx-auto p-6 mt-10 bg-white shadow rounded-lg text-foreground">
      <h1 className="text-2xl font-bold mb-4">Make an Offer</h1>
      <div className="mb-6 bg-gray-50 p-4 rounded border">
        <h2 className="text-xl font-semibold">{property.title}</h2>
        <p className="text-gray-600">Listed Price: <span className="font-bold text-primary">{property.price} BHD</span></p>
      </div>

      <form action={submitWithId} className="space-y-4">
        <div>
          <label className="block text-sm font-medium mb-1">Your Offer (BHD)</label>
          <input type="number" name="offeredPrice" required className="w-full border rounded p-3 text-lg" placeholder="e.g. 350" />
        </div>
        
        <button type="submit" className="w-full bg-accent text-white py-3 rounded text-lg font-bold hover:opacity-90 transition">
          Submit Offer
        </button>
      </form>
    </div>
  );
}
