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

const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8001';

/**
 * Proxy route for fetching spans related to an issue.
 * GET /api/pipelines/reviewiq/issues/[issueId]/spans
 */
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ issueId: string }> }
) {
  try {
    const { issueId } = await params;
    const url = `${API_BASE_URL}/api/pipelines/reviewiq/issues/${issueId}/spans`;

    const response = await fetch(url, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
      cache: 'no-store',
    });

    if (!response.ok) {
      const errorData = await response.json().catch(() => ({}));
      return NextResponse.json(
        { detail: errorData.detail || `Backend error: ${response.status}` },
        { status: response.status }
      );
    }

    const data = await response.json();
    return NextResponse.json(data);
  } catch (error) {
    console.error('Issue spans proxy error:', error);
    return NextResponse.json(
      { detail: 'Failed to fetch issue spans' },
      { status: 500 }
    );
  }
}
