import { NextRequest, NextResponse } from 'next/server';
import {
  pgResolveBankReconciliationDifference,
} from '@/lib/postgres/bank-reconciliation';

export async function PATCH(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const body = await request.json();

    const {
      resolutionType,
      resolvedBy,
      adjustmentEntryId,
    } = body;

    if (!resolutionType || !resolvedBy) {
      return NextResponse.json(
        { error: 'Missing required fields' },
        { status: 400 }
      );
    }

    const success = await pgResolveBankReconciliationDifference(
      id,
      resolutionType,
      resolvedBy,
      adjustmentEntryId
    );

    if (!success) {
      return NextResponse.json(
        { error: 'Difference not found' },
        { status: 404 }
      );
    }

    return NextResponse.json(
      { message: 'Difference resolved successfully' },
      { status: 200 }
    );
  } catch (error) {
    console.error('Error resolving bank reconciliation difference:', error);
    return NextResponse.json(
      { error: error instanceof Error ? error.message : 'Internal Server Error' },
      { status: 500 }
    );
  }
}
