import { NextRequest, NextResponse } from 'next/server';
import prisma from '../../../../../lib/prisma';

export async function POST(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const { id } = params;

    if (!id) {
      return NextResponse.json({ error: 'ID requis' }, { status: 400 });
    }

    // Attempt to increment in DB
    // Note: We don't care about mock businesses here as they are static
    const business = await prisma.business.update({
      where: { id },
      data: {
        viewCount: {
          increment: 1,
        },
      },
    });

    return NextResponse.json({ success: true, viewCount: business.viewCount });
  } catch (error) {
    // If business not found in DB (might be mock), just return success but no update
    console.error('Error incrementing view count:', error);
    return NextResponse.json({ success: false, error: 'Business not found or error' }, { status: 404 });
  }
}
