import { useEffect } from 'react'
import { RouterProvider } from 'react-router-dom'
import { router } from '@/router'
import { useAuthStore } from '@/store/authStore'

function App() {
  const { isAuthenticated, fetchUser, setLoading } = useAuthStore()

  useEffect(() => {
    // On app load, check if we have a token and fetch user data
    const token = localStorage.getItem('access_token')
    if (token && isAuthenticated) {
      fetchUser().catch(() => {
        // Token is invalid, will be handled by interceptor
      })
    } else {
      setLoading(false)
    }
  }, [])

  return <RouterProvider router={router} />
}

export default App
