/**
 * Webhook Logs API
 * View and manage webhook event logs
 */

import { NextRequest, NextResponse } from 'next/server';
import { webhookService } from '@/lib/services/webhook-service';

/**
 * GET /api/webhooks/logs
 * Get recent webhook logs
 */
export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams;
    const limit = parseInt(searchParams.get('limit') || '100');
    const entityType = searchParams.get('entityType') || undefined;
    
    const logs = await webhookService.getWebhookLogs(limit, entityType);
    
    return NextResponse.json({
      success: true,
      logs,
      count: logs.length,
    });
    
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    console.error('[WEBHOOK LOGS API] Error fetching logs:', errorMessage);
    
    return NextResponse.json(
      { error: 'Failed to fetch webhook logs', details: errorMessage },
      { status: 500 }
    );
  }
}
