import { NextRequest, NextResponse } from 'next/server'
import { Finding } from '@/lib/sdk/vendor-compliance'

// In-memory storage for demo purposes
const findings: Map<string, Finding> = new Map()

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams
    const vendorId = searchParams.get('vendorId')
    const contractId = searchParams.get('contractId')
    const status = searchParams.get('status')

    let findingsList = Array.from(findings.values())

    // Filter by vendor
    if (vendorId) {
      findingsList = findingsList.filter((f) => f.vendorId === vendorId)
    }

    // Filter by contract
    if (contractId) {
      findingsList = findingsList.filter((f) => f.contractId === contractId)
    }

    // Filter by status
    if (status) {
      findingsList = findingsList.filter((f) => f.status === status)
    }

    return NextResponse.json({
      success: true,
      data: findingsList,
      timestamp: new Date().toISOString(),
    })
  } catch (error) {
    console.error('Error fetching findings:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to fetch findings' },
      { status: 500 }
    )
  }
}
