import { StrictMode, lazy, Suspense } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter, useLocation, Navigate } from 'react-router-dom'
import { configureBoneyard } from 'boneyard-js/react'
import './index.css'
import Landing from './pages/Landing'

const LuarmorAlternativePage = lazy(() => import('./pages/LuarmorAlternative'))
const JunkieAlternativePage = lazy(() => import('./pages/JunkieAlternative'))
const PandauthAlternativePage = lazy(() => import('./pages/PandauthAlternative'))
const ObfuscatorLandingPage = lazy(() => import('./pages/ObfuscatorLanding'))
const BlogIndexPage = lazy(() => import('./pages/Blog/BlogIndex.jsx'))
const BlogPostPage = lazy(() => import('./pages/Blog/BlogPost.jsx'))
const NotFound = lazy(() => import('./components/NotFound.jsx'))

const SUPPORTED_LOCALES = ['pt', 'ru', 'de'];

configureBoneyard({
  color: 'rgba(255, 255, 255, 0.08)',
  darkColor: 'rgba(255, 255, 255, 0.05)',
  shimmerColor: 'rgba(255, 255, 255, 0.14)',
  darkShimmerColor: 'rgba(255, 255, 255, 0.10)',
  animate: 'shimmer',
  speed: '1.8s',
  transition: 250,
})

function LandingEntry() {
  const location = useLocation();
  const path = location.pathname.toLowerCase().replace(/\/+$/, '') || '/';
  if (path === '/docs' || path.startsWith('/docs/') || path === '/documentation' || path === '/getting-started') {
    const subpath = location.pathname.replace(/^\/(?:docs|documentation)/i, '');
    window.location.replace(`https://docs.luaprotect.dev${subpath}`);
    return null;
  }
  if (path === '/' || path === '') {
    // Auto-detect the visitor's language from the browser and bounce them to
    // their localized homepage once per browser - no manual switcher needed.
    // Only ever runs on the bare "/" so a /pt or /de visitor who navigates
    // back home isn't redirected in a loop, and only once per browser so
    // someone who prefers English after landing on a localized page isn't
    // forced back there on their next visit.
    try {
      if (!localStorage.getItem('lp_locale_checked')) {
        localStorage.setItem('lp_locale_checked', '1');
        const browserLang = (navigator.language || '').slice(0, 2).toLowerCase();
        if (SUPPORTED_LOCALES.includes(browserLang)) {
          window.location.replace(`/${browserLang}`);
          return null;
        }
      }
    } catch (e) {
      // localStorage/navigator unavailable (privacy mode, etc.) - fall back to English
    }
    return <Landing />;
  }
  // Localized homepage - marketing only, same Landing.jsx component tree
  // as "/" with a locale prop threaded down to every section (1:1 layout,
  // only the text differs - see Landing.jsx's own comment). Deliberately
  // does NOT extend to /docs (redirects to the English-only docs subdomain
  // above regardless of locale) or to dashboard.luaprotect.dev (a separate
  // build entirely, App.jsx, never localized).
  if (path === '/pt' || path === '/ru' || path === '/de') {
    return <Landing locale={path.slice(1)} />;
  }
  // /obfuscator used to redirect straight to "/" here despite sitting in
  // sitemap.xml at priority 0.95 (second only to the homepage) - this
  // landing.jsx router, not App.jsx, is what actually serves luaprotect.dev
  // in production, so that redirect (and server-new.js's matching 301,
  // fixed separately) was the real cause of the page being unreachable.
  if (path === '/obfuscator') {
    return (
      <Suspense fallback={<div style={{ minHeight: '100vh', background: '#0a0b10' }} />}>
        <ObfuscatorLandingPage />
      </Suspense>
    );
  }
  if (path === '/obfuscate') {
    return <Navigate to="/obfuscator" replace />;
  }
  if (path === '/blog') {
    return (
      <Suspense fallback={<div style={{ minHeight: '100vh', background: '#0c0c0e' }} />}>
        <BlogIndexPage />
      </Suspense>
    );
  }
  if (path.startsWith('/blog/')) {
    const slug = location.pathname.replace(/^\/blog\//, '').replace(/\/+$/, '');
    return (
      <Suspense key={slug} fallback={<div style={{ minHeight: '100vh', background: '#0c0c0e' }} />}>
        <BlogPostPage slug={slug} />
      </Suspense>
    );
  }
  if (
    path === '/luarmor-alternative' ||
    path === '/luarmor' ||
    path === '/luraph-alternative' ||
    path === '/luraph'
  ) {
    return (
      <Suspense fallback={<div style={{ minHeight: '100vh', background: '#0a0b10' }} />}>
        <LuarmorAlternativePage />
      </Suspense>
    );
  }
  if (
    path === '/junkie-alternative' ||
    path === '/junkie-development-alternative' ||
    path === '/jnkie-alternative'
  ) {
    return (
      <Suspense fallback={<div style={{ minHeight: '100vh', background: '#0a0b10' }} />}>
        <JunkieAlternativePage />
      </Suspense>
    );
  }
  if (path === '/pandauth-alternative' || path === '/panda-auth-alternative') {
    return (
      <Suspense fallback={<div style={{ minHeight: '100vh', background: '#0a0b10' }} />}>
        <PandauthAlternativePage />
      </Suspense>
    );
  }
  if (path === '/alternative' || path === '/alternatives') {
    return <Navigate to="/" replace />;
  }
  return (
    <Suspense fallback={<div style={{ minHeight: '100vh', background: '#0a0b10' }} />}>
      <NotFound />
    </Suspense>
  );
}

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <BrowserRouter>
      <LandingEntry />
    </BrowserRouter>
  </StrictMode>,
)
