import { NextRequest, NextResponse } from 'next/server';
import { runOcrForensicComparison } from '@/lib/ocrAnalysis';

export async function POST(req: NextRequest) {
  try {
    const body = await req.json();
    const {
      puCode,
      puName,
      agentPhotoUrl,
      irevPhotoUrl,
      agentVotes,
      irevVotes,
      bvasAccredited,
    } = body;

    if (!puCode || !agentVotes || !irevVotes) {
      return NextResponse.json(
        { success: false, error: 'Missing required parameters (puCode, agentVotes, irevVotes)' },
        { status: 400 }
      );
    }

    const report = await runOcrForensicComparison({
      puCode,
      puName: puName || 'Polling Unit ' + puCode,
      agentPhotoUrl: agentPhotoUrl || '',
      irevPhotoUrl: irevPhotoUrl || '',
      agentVotes,
      irevVotes,
      bvasAccredited: Number(bvasAccredited) || 400,
    });

    return NextResponse.json({
      success: true,
      report,
    });
  } catch (error: any) {
    return NextResponse.json(
      { success: false, error: error.message || 'OCR forensic analysis failed' },
      { status: 500 }
    );
  }
}
