import { Outlet, Link, createRootRoute, HeadContent, Scripts } from "@tanstack/react-router";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { LanguageProvider } from "@/hooks/use-language";
import { AutoUpdate } from "@/components/AutoUpdate";
import { InitialLoader } from "@/components/InitialLoader";
import { InstallPrompt } from "@/components/InstallPrompt";



import appCss from "../styles.css?url";

const queryClient = new QueryClient();

function NotFoundComponent() {
  return (
    <div className="flex min-h-screen items-center justify-center bg-background px-4">
      <div className="max-w-md text-center">
        <h1 className="text-7xl font-bold text-foreground">404</h1>
        <h2 className="mt-4 text-xl font-semibold text-foreground">Page not found</h2>
        <p className="mt-2 text-sm text-muted-foreground">
          The page you're looking for doesn't exist or has been moved.
        </p>
        <div className="mt-6">
          <Link
            to="/"
            className="inline-flex items-center justify-center rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground transition-colors hover:bg-primary/90"
          >
            Go home
          </Link>
        </div>
      </div>
    </div>
  );
}

export const Route = createRootRoute({
  head: () => ({
    meta: [
      { charSet: "utf-8" },
      { name: "viewport", content: "width=device-width, initial-scale=1, viewport-fit=cover" },
      { name: "theme-color", content: "#ffffff" },
      { name: "apple-mobile-web-app-capable", content: "yes" },
      { name: "apple-mobile-web-app-status-bar-style", content: "default" },
      { name: "apple-mobile-web-app-title", content: "Niyamah" },
      { name: "mobile-web-app-capable", content: "yes" },
      { title: "Niyamah Electric & Hardware — ERP/POS" },
      { name: "description", content: "Niyamah Electric & Hardware এর জন্য সম্পূর্ণ ERP ও POS সফটওয়্যার" },
      { property: "og:title", content: "Niyamah Electric & Hardware — ERP/POS" },
      { property: "og:description", content: "Niyamah Electric & Hardware এর জন্য সম্পূর্ণ ERP ও POS সফটওয়্যার" },
      { property: "og:type", content: "website" },
      { name: "twitter:title", content: "Niyamah Electric & Hardware — ERP/POS" },
      { name: "twitter:description", content: "Niyamah Electric & Hardware এর জন্য সম্পূর্ণ ERP ও POS সফটওয়্যার" },
      { property: "og:image", content: "https://pub-bb2e103a32db4e198524a2e9ed8f35b4.r2.dev/903cebea-018c-4cbe-8013-1ccd94836ac9/id-preview-699a9f36--7d5437ec-ec25-46e1-9709-df64bfa08d93.lovable.app-1777714085281.png" },
      { name: "twitter:image", content: "https://pub-bb2e103a32db4e198524a2e9ed8f35b4.r2.dev/903cebea-018c-4cbe-8013-1ccd94836ac9/id-preview-699a9f36--7d5437ec-ec25-46e1-9709-df64bfa08d93.lovable.app-1777714085281.png" },
      { name: "twitter:card", content: "summary_large_image" },
    ],
    links: [
      { rel: "stylesheet", href: appCss },
      { rel: "manifest", href: "/manifest.json" },
      { rel: "icon", type: "image/x-icon", href: "/favicon.ico" },
      { rel: "icon", type: "image/png", sizes: "16x16", href: "/favicon-16.png" },
      { rel: "icon", type: "image/png", sizes: "32x32", href: "/favicon-32.png" },
      { rel: "icon", type: "image/png", sizes: "192x192", href: "/icon-192.png" },
      { rel: "icon", type: "image/png", sizes: "512x512", href: "/icon-512.png" },
      { rel: "apple-touch-icon", sizes: "152x152", href: "/apple-touch-icon-152.png" },
      { rel: "apple-touch-icon", sizes: "167x167", href: "/apple-touch-icon-167.png" },
      { rel: "apple-touch-icon", sizes: "180x180", href: "/apple-touch-icon.png" },
    ],
  }),
  shellComponent: RootShell,
  component: RootComponent,
  notFoundComponent: NotFoundComponent,
});

function RootShell({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <HeadContent />
      </head>
      <body>
        {children}
        <Scripts />
      </body>
    </html>
  );
}

function RootComponent() {
  return (
    <QueryClientProvider client={queryClient}>
      <LanguageProvider>
        <InitialLoader />
        <AutoUpdate />
        <InstallPrompt />
        <Outlet />
      </LanguageProvider>
    </QueryClientProvider>
  );
}
