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

export async function GET(
  req: NextRequest,
  { params }: { params: Promise<{ productId: string }> }
) {
  try {
    const { productId } = await params;
    const searchParams = req.nextUrl.searchParams;
    const userId = searchParams.get('user');

    // Build the endpoint URL
    const endpoint = `product/co-creation/${productId}${userId ? `?user=${userId}` : ''}`;

    // Call external API through serverApiClient
    const result = await serverApiClient(endpoint, {
      method: 'GET',
    });

    if (!result.success) {
      return NextResponse.json(
        { 
          success: false, 
          error: result.message || 'Failed to fetch co-creation URL',
          data: null 
        },
        { status: 500 }
      );
    }

    return NextResponse.json(result.data, {
      status: 200,
    });
  } catch (error) {
    console.error('Error in co-creation API route:', error);
    return NextResponse.json(
      { 
        success: false, 
        error: 'Internal server error',
        data: null 
      },
      { status: 500 }
    );
  }
}
