'use client';

import NextLink from 'next/link';
import {MotionConfig} from 'framer-motion';
import {Theme} from '@astryxdesign/core/theme';
import {LinkProvider} from '@astryxdesign/core/Link';
import {ToastViewport} from '@astryxdesign/core/Toast';
import {loyalyTheme} from '@/theme';
import {WorkspaceProvider} from '@/components/shell/WorkspaceProvider';
import {CopilotProvider} from '@/features/copilot/CopilotProvider';

/**
 * @/theme wraps the GENERATED module (__built: true), so <Theme> skips runtime
 * token injection entirely — tokens already ship in loyaly.css. Importing
 * src/theme/loyalyTheme.ts here instead would move theming to a client-only
 * useInsertionEffect and reintroduce the first-paint flash.
 *
 * @/theme also re-attaches the Lucide icon registry that `theme build` drops;
 * <Theme> registers it during render, on both the SSR and hydration passes.
 *
 * WorkspaceProvider and CopilotProvider sit here, ABOVE the route tree, so
 * query scope and Copilot state outlive both navigation and the responsive
 * swap between the inline panel and the slide-over.
 */
export function Providers({children}: {children: React.ReactNode}) {
  return (
    <Theme theme={loyalyTheme} mode="dark">
      <LinkProvider component={NextLink}>
        {/* One line, and every motion.* in the app honours prefers-reduced-motion. */}
        <MotionConfig reducedMotion="user">
          <WorkspaceProvider>
            <CopilotProvider>{children}</CopilotProvider>
          </WorkspaceProvider>
          {/* Mounted once at the root: useToast() positions, stacks and
              expires notifications into this viewport from anywhere. */}
          <ToastViewport />
        </MotionConfig>
      </LinkProvider>
    </Theme>
  );
}
