import { NextRequest, NextResponse } from 'next/server';
import { getAuthenticatedStore, unauthorizedResponse } from '@/lib/auth/storeContext';
import { ShopifyProductService } from '@/lib/shopify/product-service';

export const dynamic = 'force-dynamic';

export async function GET(req: NextRequest) {
  try {
    const shop = await getAuthenticatedStore(req);
    if (!shop) {
      return unauthorizedResponse();
    }
    const collections = await ShopifyProductService.fetchCollections({
      shopDomain: shop.shopify_domain,
    });

    return NextResponse.json({
      success: true,
      collections,
    });
  } catch (err: any) {
    console.error('[API /api/shopify/collections GET] Error:', err);
    return NextResponse.json(
      { success: false, error: err.message || 'Failed to fetch Shopify collections' },
      { status: 500 }
    );
  }
}
