import {
  isRouteErrorResponse,
  Links,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
} from "react-router";
import { Toaster } from "react-hot-toast";

import type { Route } from "./+types/root";
import { AuthProvider } from "./contexts/auth-context";
import { GlobalRouteGuard } from "./components/auth/global-route-guard";
import "./app.css";

export const links: Route.LinksFunction = () => [
  { rel: "preconnect", href: "https://fonts.googleapis.com" },
  {
    rel: "preconnect",
    href: "https://fonts.gstatic.com",
    crossOrigin: "anonymous",
  },
  {
    rel: "stylesheet",
    href: "https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap",
  },
  {
    rel: "stylesheet",
    href: "https://fonts.googleapis.com/css2?family=Vazirmatn:wght@100..900&display=swap",
  },
];

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="fa" dir="rtl" className="dark">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
      </head>
      <body className="font-persian bg-gray-900 text-white">
        <AuthProvider>
          <GlobalRouteGuard>{children}</GlobalRouteGuard>
          <Toaster
            position="top-center"
            reverseOrder={false}
            gutter={8}
            containerClassName=""
            containerStyle={{}}
            toastOptions={{
              // Define default options
              className: "",
              duration: 4000,
              style: {
                background: "rgba(31, 41, 55, 0.95)",
                color: "#fff",
                fontFamily:
                  "Vazirmatn, Inter, ui-sans-serif, system-ui, sans-serif",
                direction: "rtl",
                textAlign: "right",
                border: "1px solid rgba(16, 185, 129, 0.3)",
              },
              // Default options for specific types
              success: {
                duration: 3000,
                style: {
                  background: "rgba(16, 185, 129, 0.9)",
                  color: "#fff",
                },
                iconTheme: {
                  primary: "#fff",
                  secondary: "#10b981",
                },
              },
              error: {
                duration: 4000,
                style: {
                  background: "rgba(239, 68, 68, 0.9)",
                  color: "#fff",
                },
                iconTheme: {
                  primary: "#fff",
                  secondary: "#ef4444",
                },
              },
            }}
          />
        </AuthProvider>
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

export default function App() {
  return <Outlet />;
}

export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
  let message = "خطا!";
  let details = "خطای غیرمنتظره‌ای رخ داده است.";
  let stack: string | undefined;

  if (isRouteErrorResponse(error)) {
    message = error.status === 404 ? "404" : "خطا";
    details =
      error.status === 404
        ? "صفحه مورد نظر یافت نشد."
        : error.statusText || details;
  } else if (import.meta.env.DEV && error && error instanceof Error) {
    details = error.message;
    stack = error.stack;
  }

  return (
    <main className="pt-16 p-4 container mx-auto" dir="rtl">
      <h1 className="text-2xl font-bold mb-4 font-persian">{message}</h1>
      <p className="mb-4 font-persian">{details}</p>
      {stack && (
        <pre className="w-full p-4 overflow-x-auto bg-gray-100 dark:bg-gray-800 rounded">
          <code>{stack}</code>
        </pre>
      )}
    </main>
  );
}
