import React from 'react';
import { Navigate } from 'react-router-dom';
import { Result, Spin } from 'antd';
import { usePermission } from '../hooks/usePermission';
import { findRoleAwareLandingPath } from '../auth/menu-policy';
import { useUserStore } from '../store/user/userStore';

const DefaultRoute: React.FC = () => {
  const { permissions, permissionsReady } = usePermission();
  if (!permissionsReady) {
    return <Spin size="large" style={{ display: 'block', margin: '80px auto' }} />;
  }
  const roles = useUserStore((state) => state.user?.roles ?? []);
  const firstPath = findRoleAwareLandingPath(roles, permissions);
  if (firstPath) return <Navigate to={firstPath} replace />;
  return (
    <Result status="403" title="暂无可访问功能" subTitle="请联系管理员为当前账号分配功能权限" />
  );
};

export default DefaultRoute;
