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

// Cache co-creation sessions for 5 minutes (sessions are temporary)
export const revalidate = 300;

/**
 * Create fabric co-creation session
 * Cached server-side to reduce costs
 */
export async function GET(
  req: NextRequest,
  { params }: { params: Promise<{ fabric_id: string }> }
) {
  try {
    const { fabric_id } = await params;

    const result = await serverApiClient(`product/fabric-creation/${fabric_id}`, {
      method: 'GET',
      revalidate: 300, // Cache for 5 minutes
    });

    if (!result.success) {
      return NextResponse.json(
        {
          success: false,
          message: result.message || 'Failed to create co-creation session',
        },
        { status: 500 }
      );
    }

    return NextResponse.json(
      {
        success: true,
        data: result.data,
      },
      {
        headers: {
          'Cache-Control': 'private, s-maxage=300, stale-while-revalidate=600',
        },
      }
    );
  } catch (error: any) {
    console.error('Error creating fabric co-creation session:', error);
    return NextResponse.json(
      {
        success: false,
        message: error.message || 'Failed to create co-creation session',
      },
      { status: 500 }
    );
  }
}
