'use server';

import { PrismaClient } from '@prisma/client';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';

const prisma = new PrismaClient();

export async function submitOffer(propertyId: number, formData: FormData) {
  const offeredPrice = parseFloat(formData.get('offeredPrice') as string);
  
  await prisma.offer.create({
    data: {
      propertyId,
      clientId: 1, // Mock client user ID
      offeredPrice,
    }
  });

  revalidatePath('/agent/offers');
  redirect(`/properties/${propertyId}?offer=success`);
}

export async function updateOfferStatus(offerId: number, status: string, counterPrice?: number) {
  await prisma.offer.update({
    where: { id: offerId },
    data: { 
      status,
      ...(counterPrice && { counterPrice })
    }
  });

  revalidatePath('/agent/offers');
}
