// [TSM.ID].[11031972] -- All Rights Reserved. Proprietary & Confidential.
import createMiddleware from 'next-intl/middleware';
import { locales } from './i18n';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

const intlMiddleware = createMiddleware({
  locales,
  defaultLocale: 'id',
  localePrefix: 'as-needed'
});

export function proxy(request: NextRequest) {
  // Check auth first
  const isGuestOrApi = request.nextUrl.pathname.includes('/api/') || request.nextUrl.pathname.includes('/guest/');
  if (!isGuestOrApi) {
    const token = request.cookies.get('jumpa_token')?.value;
    if (!token && request.nextUrl.pathname !== '/') {
      const loginUrl = new URL('/', request.url);
      return NextResponse.redirect(loginUrl);
    }
  }

  // Run next-intl
  return intlMiddleware(request);
}

export const config = {
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)']
};
