import React from 'react';
import { Navigate } from 'react-router-dom';
import { Result } from 'antd';
import { usePermission } from '../hooks/usePermission';
import { findRoleAwareLandingPath } from '../auth/menu-policy';

const DefaultRoute: React.FC = () => {
  const { permissions } = usePermission();
  const roles = (() => {
    try {
      return JSON.parse(localStorage.getItem('user') || '{}').roles || [];
    } catch {
      return [];
    }
  })();
  const firstPath = findRoleAwareLandingPath(roles, permissions);
  if (firstPath) return <Navigate to={firstPath} replace />;
  return (
    <Result status="403" title="暂无可访问功能" subTitle="请联系管理员为当前账号分配功能权限" />
  );
};

export default DefaultRoute;
