'use server';

export async function getCoCreationUrl(organizationId: string) {
  try {
    const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || "http://localhost:3000";
    const response = await fetch(
      `${siteUrl}/api/design-houses/co-creation/${organizationId}`,
      {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
        },
        cache: 'no-store', // Don't cache this request
      },
    );

    if (!response.ok) {
      throw new Error(`Error: ${response.status} - ${response.statusText}`);
    }

    const data = await response.json();
    
    if (!data.success) {
      throw new Error(data.error || 'Failed to get co-creation URL');
    }
    
    return {
      success: true,
      sessionId: data.data.sessionId,
      url: data.data.url,
    };
  } catch (error) {
    console.error("Error fetching co-creation URL:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : 'Unknown error occurred',
    };
  }
}
