import { NextResponse } from 'next/server';

const SHOPWARE_URL = process.env.NEXT_PUBLIC_SHOPWARE_URL || '';
const ACCESS_KEY = process.env.NEXT_PUBLIC_SHOPWARE_ACCESS_KEY || '';

async function rawNav(type: string, depth: number) {
  const url = `${SHOPWARE_URL}/store-api/navigation/${type}/${type}`;
  const body = { depth };

  const res = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      'sw-access-key': ACCESS_KEY,
    },
    body: JSON.stringify(body),
    cache: 'no-store',
  });

  const status = res.status;
  let data: any;
  try {
    data = await res.json();
  } catch {
    data = await res.text();
  }

  return { status, data };
}

export async function GET() {
  const [footer, service] = await Promise.all([
    rawNav('footer-navigation', 3),
    rawNav('service-navigation', 1),
  ]);

  return NextResponse.json({
    footer_navigation: footer,
    service_navigation: service,
  });
}
