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

export async function GET(request: NextRequest) {
  const cookie = request.headers.get('cookie') || '';

  const isTest = process.env.NEXT_PUBLIC_ENV === 'test';
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
    Cookie: cookie,
  };

  if (isTest) {
    const user = process.env.BASIC_AUTH_USER || '';
    const pass = process.env.BASIC_AUTH_PASS || '';
    headers.Authorization = 'Basic ' + Buffer.from(`${user}:${pass}`).toString('base64');
  }

  const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000';
  const url = apiUrl.replace(/\/$/, '') + '/accessManagement/getAccessConfig';

  const res = await fetch(url, {
    headers,
    method: 'GET',
  });
  const data = await res.json();
  return NextResponse.json(data);
}
