import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export async function GET() {
 try {
  const properties = await prisma.property.findMany({
   orderBy: { createdAt: 'desc' }
  });
  return NextResponse.json(properties);
 } catch (error) {
  return NextResponse.json({ error: 'Failed to fetch properties' }, { status: 500 });
 }
}

export async function POST(request: Request) {
 try {
  const data = await request.json();
  const newProperty = await prisma.property.create({
   data: {
    ...data
   }
  });
  return NextResponse.json(newProperty);
 } catch (error: any) {
  return NextResponse.json({ error: 'Failed to create property', details: error.message }, { status: 500 });
 }
}
