import type { NextApiRequest, NextApiResponse } from 'next';
import prisma from '@/prisma/config';

/**
 * Create a new offer for a property.
 * Expected body: propertyId, clientId, offeredPrice
 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    return res.status(405).json({ error: 'Method not allowed' });
  }
  const { propertyId, clientId, offeredPrice } = req.body as any;
  if (!propertyId || !clientId || !offeredPrice) {
    return res.status(400).json({ error: 'Missing required fields' });
  }
  try {
    const offer = await prisma.offer.create({
      data: {
        propertyId: Number(propertyId),
        clientId: Number(clientId),
        offeredPrice: Number(offeredPrice),
        status: 'pending',
      },
    });
    res.status(201).json(offer);
  } catch (e) {
    console.error(e);
    res.status(500).json({ error: 'Failed to create offer' });
  }
}
