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

/**
 * Get all offers for properties owned by a specific agent.
 * URL: /api/offers/agent/[agentId]
 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'GET') {
    return res.status(405).json({ error: 'Method not allowed' });
  }
  const { agentId } = req.query;
  if (!agentId) {
    return res.status(400).json({ error: 'Missing agentId' });
  }
  try {
    const offers = await prisma.offer.findMany({
      where: { property: { agentId: Number(agentId) } },
      include: { property: true, client: true },
    });
    res.status(200).json(offers);
  } catch (e) {
    console.error(e);
    res.status(500).json({ error: 'Failed to fetch offers' });
  }
}
