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

export function middleware(req: NextRequest) {
  const path = req.nextUrl.pathname;

  // 301 Redirects for /guide -> /learn to avoid duplicate content and consolidate authority
  if (path === '/guide/tracking-analytics') {
    return NextResponse.redirect(new URL('/learn/tracking', req.url), 301);
  }
  if (path === '/guide/bulk-qr-code-generation') {
    return NextResponse.redirect(new URL('/learn/developer', req.url), 301);
  }
  if (path === '/guide/qr-code-best-practices') {
    return NextResponse.redirect(new URL('/learn/basics', req.url), 301);
  }
  if (path === '/create-qr') {
    return NextResponse.redirect(new URL('/dynamic-qr-code-generator', req.url), 301);
  }

  // Public routes that don't require authentication
  const publicPaths = [
    '/',
    '/pricing',
    '/faq',
    '/blog',
    '/login',
    '/signup',
    '/privacy',
    '/newsletter',
    '/tools',
    '/features',
    // '/guide', // Redirected to /learn/*
    '/qr-code-erstellen',
    '/dynamic-qr-code-generator',
    '/dynamic-barcode-generator',
    '/bulk-qr-code-generator',
    '/qr-code-tracking',
    '/qr-code-analytics',
    '/reprint-calculator',
    '/barcode-generator',
    '/custom-qr-code-generator',
    '/manage-qr-codes',
    '/coupon',
    '/feedback',
    '/vcard',
    '/display',
    '/contact',
    '/about',
    '/learn',
    '/use-cases',
    '/authors',
    '/press',
    '/testimonials',
    '/qr-code-for-marketing-campaigns',
    '/qr-code-for',
    '/qr-code-print-size-guide',
    '/use-cases/flyer-qr-codes',
    '/use-cases/packaging-qr-codes',
    '/use-cases/real-estate-sign-qr-codes',
    '/use-cases/feedback-qr-codes',
    '/use-cases/payment-qr-codes',
    '/use-cases/coupon-qr-codes',
    '/alternatives',
    '/vs',
  ];

  // Check if path is public
  const isPublicPath = publicPaths.some(p => path === p || path.startsWith(p + '/'));

  // Allow API routes
  if (path.startsWith('/api/')) {
    return NextResponse.next();
  }

  // Allow redirect routes (QR code redirects)
  if (path.startsWith('/r/')) {
    return NextResponse.next();
  }

  // Allow static files
  if (path.includes('.') || path.startsWith('/_next')) {
    return NextResponse.next();
  }

  // Allow public paths
  if (isPublicPath) {
    return NextResponse.next();
  }

  // For protected routes, check for userId cookie
  const userId = req.cookies.get('userId')?.value;

  if (!userId) {
    // Not authenticated - redirect to signup
    const signupUrl = new URL('/signup', req.url);
    return NextResponse.redirect(signupUrl);
  }

  // Authenticated - allow access
  return NextResponse.next();
}

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico (favicon file)
     * - public folder
     */
    '/((?!_next/static|_next/image|favicon.ico|logo.svg|og-image.png).*)',
  ],
};
