import "@fontsource/inter/400.css";
import "@fontsource/inter/600.css";
import "@fontsource/inter/700.css";
import "@fontsource/inter/800.css";
import "@fontsource/jetbrains-mono/400.css";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import {
  Outlet,
  Link,
  createRootRouteWithContext,
  useRouter,
  HeadContent,
  Scripts,
} from "@tanstack/react-router";
import { useEffect, type ReactNode } from "react";

import appCss from "../styles.css?url";
import { reportLovableError } from "../lib/lovable-error-reporting";

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-extrabold tracking-tighter text-foreground">404</h1>
        <h2 className="mt-4 text-xl font-semibold text-foreground">Página não encontrada</h2>
        <p className="mt-2 text-sm text-muted-foreground">
          A página que você procura não existe ou foi movida.
        </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-bold text-primary-foreground transition-colors hover:opacity-90"
          >
            Voltar ao início
          </Link>
        </div>
      </div>
    </div>
  );
}

function ErrorComponent({ error, reset }: { error: Error; reset: () => void }) {
  console.error(error);
  const router = useRouter();
  useEffect(() => {
    reportLovableError(error, { boundary: "tanstack_root_error_component" });
  }, [error]);

  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-xl font-semibold tracking-tight text-foreground">
          Esta página não carregou
        </h1>
        <p className="mt-2 text-sm text-muted-foreground">
          Algo deu errado. Tente novamente ou volte ao início.
        </p>
        <div className="mt-6 flex flex-wrap justify-center gap-2">
          <button
            onClick={() => {
              router.invalidate();
              reset();
            }}
            className="inline-flex items-center justify-center rounded-md bg-primary px-4 py-2 text-sm font-bold text-primary-foreground transition-colors hover:opacity-90"
          >
            Tentar novamente
          </button>
          <a
            href="/"
            className="inline-flex items-center justify-center rounded-md border border-border bg-background px-4 py-2 text-sm font-medium text-foreground transition-colors hover:bg-accent"
          >
            Início
          </a>
        </div>
      </div>
    </div>
  );
}

export const Route = createRootRouteWithContext<{ queryClient: QueryClient }>()({
  head: () => ({
    meta: [
      { charSet: "utf-8" },
      { name: "viewport", content: "width=device-width, initial-scale=1" },
      { title: "Consulting Venture Builder — Desenvolvemos startups que resolvem problemas reais | Venture Builder" },
      {
        name: "description",
        content:
          "A Consulting Venture Builder ajuda a desenvolver startups escaláveis com metodologia própria, tecnologia, inteligência artificial e um ecossistema de consultores e especialistas.",
      },
      { name: "author", content: "Consulting Venture Builder" },
      {
        name: "keywords",
        content:
          "Consulting Venture Builder, Venture Builder, Venture Building, Desenvolvimento de Startups, Inovação, Inteligência Artificial, Consultoria, Transformação Digital, Ecossistema de Startups",
      },
      { property: "og:title", content: "Consulting Venture Builder — Desenvolvemos startups que resolvem problemas reais | Venture Builder" },
      {
        property: "og:description",
        content:
          "A Consulting Venture Builder ajuda a desenvolver startups escaláveis com metodologia própria, tecnologia, inteligência artificial e um ecossistema de consultores e especialistas.",
      },
      { property: "og:type", content: "website" },
      { property: "og:site_name", content: "Consulting Venture Builder" },
      { name: "twitter:card", content: "summary_large_image" },
      { name: "twitter:title", content: "Consulting Venture Builder — Desenvolvemos startups que resolvem problemas reais | Venture Builder" },
      {
        name: "twitter:description",
        content:
          "A Consulting Venture Builder ajuda a desenvolver startups escaláveis com metodologia própria, tecnologia, inteligência artificial e um ecossistema de consultores e especialistas.",
      },
      { property: "og:image", content: "https://pub-bb2e103a32db4e198524a2e9ed8f35b4.r2.dev/400f68df-4e39-4089-b19f-338c19eed44a/id-preview-6b13baa2--02adad34-07f9-4223-aaf2-5a268d44e9e4.lovable.app-1783970618016.png" },
      { name: "twitter:image", content: "https://pub-bb2e103a32db4e198524a2e9ed8f35b4.r2.dev/400f68df-4e39-4089-b19f-338c19eed44a/id-preview-6b13baa2--02adad34-07f9-4223-aaf2-5a268d44e9e4.lovable.app-1783970618016.png" },
    ],
    links: [
      { rel: "stylesheet", href: appCss },
      { rel: "icon", href: "/favicon.png", type: "image/png" },
      { rel: "apple-touch-icon", href: "/favicon.png" },
    ],
  }),
  shellComponent: RootShell,
  component: RootComponent,
  notFoundComponent: NotFoundComponent,
  errorComponent: ErrorComponent,
});

function RootShell({ children }: { children: ReactNode }) {
  return (
    <html lang="pt-BR">
      <head>
        <HeadContent />
      </head>
      <body>
        {children}
        <Scripts />
      </body>
    </html>
  );
}

function RootComponent() {
  const { queryClient } = Route.useRouteContext();

  return (
    <QueryClientProvider client={queryClient}>
      <Outlet />
    </QueryClientProvider>
  );
}
