import { useEffect } from 'react'
import Box from '@mui/material/Box'
import CircularProgress from '@mui/material/CircularProgress'
import Typography from '@mui/material/Typography'
import { useNavigate, useSearchParams } from 'react-router-dom'
import { tokenSet } from '@/shared/model/auth'

export function AuthCallbackPage() {
  const [params] = useSearchParams()
  const navigate = useNavigate()

  useEffect(() => {
    const t = params.get('token')
    if (t) {
      tokenSet(t)
      navigate('/', { replace: true })
      return
    }
    navigate('/auth', { replace: true })
  }, [navigate, params])

  return (
    <Box sx={{ py: 6, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 2 }}>
      <CircularProgress />
      <Typography color="text.secondary">Завершение входа…</Typography>
    </Box>
  )
}
