import { NextRequest } from 'next/server';
import { getSession } from '@/lib/auth';
import { createResponse, handleApiError } from '@/src/lib/api';

export async function GET(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  try {
    const session = await getSession();
    const { id } = await params;

    if (!id) {
      return new Response('Deployment ID is required', { status: 400 });
    }

    const vercelResponse = await fetch(`https://api.vercel.com/v3/deployments/${id}/events?follow=0`, {
      headers: {
        Authorization: `Bearer ${session?.user.vercelAccessToken}`,
      },
    });
    const data = await vercelResponse.json();
    return createResponse(200, 'Deployment events retrieved successfully', {
      data,
    });
  } catch (error) {
    return handleApiError(error);
  }
}
