/**
 * Debug endpoint to inspect theme logo data
 * Visit: http://localhost:3000/api/debug-theme
 */

import { shopwareApi } from '@/lib/shopware-api';

export async function GET() {
  try {
    // Get context
    const ctx = await shopwareApi<any>('/context', { method: 'GET' });
    const salesChannelId = ctx?.salesChannel?.id;

    if (!salesChannelId) {
      return Response.json({ error: 'No sales channel ID found' }, { status: 400 });
    }

    // Fetch theme
    const themeRes = await shopwareApi<any>('/theme', {
      body: {
        filter: [
          {
            type: 'equalsAny',
            field: 'salesChannels.id',
            value: [salesChannelId],
          },
        ],
        limit: 1,
        associations: {
          media: {
            associations: {
              media: {},
            },
          },
        },
      },
    });

    const theme = themeRes?.elements?.[0];

    return Response.json({
      success: true,
      salesChannelId,
      theme: {
        id: theme?.id,
        name: theme?.name,
        media: theme?.media,
        customFields: theme?.customFields,
        config: theme?.config,
      },
      fullTheme: theme, // For debugging
    });
  } catch (error: any) {
    return Response.json({
      success: false,
      error: error.message,
      details: error.toString(),
    });
  }
}
