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 [executionResult, stepsResult] = await Promise.all([
      postgresClient.query(`SELECT * FROM workflow_executions WHERE id = $1`, [id]),
      postgresClient.query(`SELECT * FROM workflow_execution_steps WHERE execution_id = $1 ORDER BY step_order`, [id]),
    ]);

    if (executionResult.rows.length === 0) {
      return NextResponse.json({ error: 'Not found' }, { status: 404 });
    }

    return NextResponse.json({
      ...executionResult.rows[0],
      steps: stepsResult.rows,
    });
  } catch (error) {
    console.error('Failed to fetch execution:', error);
    return NextResponse.json({ error: 'Failed to fetch execution' }, { status: 500 });
  }
}
