'use client'

import { SWRConfig } from 'swr'
import { AuthProvider } from '@/lib/auth-context'
import { AppLayout } from '@/components/layout/app-layout'
import { fetcher } from '@/lib/fetcher'

export function AppClientLayout({ children }: { children: React.ReactNode }) {
  return (
    <SWRConfig
      value={{
        fetcher,
        revalidateOnFocus: false,
        revalidateOnMount: true,
        // Não retenta em 401 — a revalidação global no AuthProvider cuida disso
        onErrorRetry: (err, _key, _config, revalidate, { retryCount }) => {
          if (err?.status === 401) return
          if (retryCount >= 2) return
          setTimeout(() => revalidate({ retryCount }), 3000)
        },
        onError: (err) => {
          if (err?.status === 401) return
        },
      }}
    >
      <AuthProvider>
        <AppLayout>{children}</AppLayout>
      </AuthProvider>
    </SWRConfig>
  )
}
