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

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

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

    let url = `${API_BASE_URL}/jobs?limit=${limit}`;
    if (status) {
      url += `&status=${status}`;
    }

    const response = await fetch(url);

    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 }
    );
  }
}
