/**
 * Pipeline Executions API — view execution history for a pipeline.
 */

import { NextRequest, NextResponse } from 'next/server';
import { postgresClient } from '@/lib/services/postgres-client';

export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id } = await params;
    const searchParams = request.nextUrl.searchParams;
    const limit = Math.min(parseInt(searchParams.get('limit') || '50', 10), 200);

    const result = await postgresClient.query(
      `SELECT * FROM pipeline_executions WHERE pipeline_id = $1 ORDER BY created_at DESC LIMIT $2`,
      [id, limit]
    );

    return NextResponse.json({ data: result.rows, total: result.rows.length });
  } catch (error) {
    const msg = error instanceof Error ? error.message : String(error);
    return NextResponse.json({ error: msg }, { status: 500 });
  }
}
