import React from 'react';
import { Navigate } from 'react-router-dom';
import useAuthStore from '../../../store/useAuthStore';

interface StaffRouteProps {
  children: React.ReactNode;
}

const StaffRoute: React.FC<StaffRouteProps> = ({ 
  children
}) => {
  const { isAuthenticated, userInfo, isLoading } = useAuthStore();
  
  if (isLoading) {
    return (
      <div 
        className="min-h-screen flex items-center 
          justify-center bg-gray-50"
      >
        <div className="text-center">
          <div 
            className="animate-spin rounded-full h-12 w-12 
              border-b-2 border-indigo-600 mx-auto"
          />
          <p className="mt-4 text-gray-600">
            Authenticating...
          </p>
        </div>
      </div>
    );
  }

  
  if (!isAuthenticated) {
    return <Navigate to="/staff/login" replace />;
  }

  
  const isStaff = userInfo?.role === 'staff';
  if (!isStaff) {
    // Redirect to appropriate dashboard based on role
    if (userInfo?.role === 'admin') {
      return <Navigate to="/admin/dashboard" replace />;
    } else if (userInfo?.role === 'accountant') {
      return <Navigate to="/accountant/dashboard" replace />;
    } else if (userInfo?.role === 'housekeeping') {
      return <Navigate to="/housekeeping/dashboard" replace />;
    }
    return <Navigate to="/" replace />;
  }

  return <>{children}</>;
};

export default StaffRoute;

