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

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

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url);
    const limit = searchParams.get('limit') || '100';

    const response = await fetch(`${API_BASE_URL}/jobs?limit=${limit}`);

    if (!response.ok) {
      return NextResponse.json(
        { error: 'Failed to get jobs' },
        { status: response.status }
      );
    }

    const data = await response.json();
    // Backend returns array directly, not { jobs: [...] }
    const jobs = Array.isArray(data) ? data : (data.jobs || []);
    return NextResponse.json({ jobs });
  } catch (error) {
    console.error('Jobs API error:', error);
    return NextResponse.json(
      { error: 'Failed to get jobs' },
      { status: 500 }
    );
  }
}
