import { useSelector } from 'react-redux';
import { useState, useEffect, useMemo } from 'react';

export default function useAuthorization(entity, action, entityStatus = '') {
  const [isAuthorized, setIsAuthorized] = useState(false);

  const user = useSelector((state) => state.auth.user);

  const permissions = useMemo(
    () => ({
      CLEARING_AGENT: {
        EWAYBILL: {
          CREATE: [],
          CLOSE: ['TRIPFINISHED', 'CLOSED', 'TRIPSTARTED', 'ACTIVE'],
          PRINT: [
            'TRIPFINISHED',
            'CLOSED',
            'TRIPSTARTED',
            'ACTIVE',
            'PENDINGAPPROVAL',
            'UNDERREVIEW',
            'CANCELLED',
            'REJECTED',
            'APPROVED',
          ],
          NEXT: [
            'TRIPFINISHED',
            'CLOSED',
            'TRIPSTARTED',
            'ACTIVE',
            'PENDINGAPPROVAL',
            'UNDERREVIEW',
            'CANCELLED',
            'REJECTED',
            'APPROVED',
          ],
          CR_APPROVE: ['PENDINGAPPROVAL'],
          CANCEL: ['PENDINGAPPROVAL', 'ACTIVE'],
        },
      },
      FREIGHT_FORWARDER: {
        EWAYBILL: {
          CREATE: [],
          APPROVE: ['UNDERREVIEW'],
        },
      },
      TRUCKING_BROKER: {
        EWAYBILL: {
          CREATE: [],
          CLOSE: ['TRIPFINISHED', 'CLOSED', 'TRIPSTARTED', 'ACTIVE'],
          PRINT: [
            'TRIPFINISHED',
            'CLOSED',
            'TRIPSTARTED',
            'ACTIVE',
            'PENDINGAPPROVAL',
            'UNDERREVIEW',
            'CANCELLED',
            'REJECTED',
            'APPROVED',
          ],
          NEXT: [
            'TRIPFINISHED',
            'CLOSED',
            'TRIPSTARTED',
            'ACTIVE',
            'PENDINGAPPROVAL',
            'UNDERREVIEW',
            'CANCELLED',
            'REJECTED',
            'APPROVED',
          ],
          CR_APPROVE: ['PENDINGAPPROVAL'],
          CANCEL: ['PENDINGAPPROVAL', 'ACTIVE'],
        },
      },
      TRUCKING_COMPANY: {
        EWAYBILL: {
          CREATE: [],
        },
      },
      OWNER: {
        EWAYBILL: {
          CREATE: [],
        },
      },
    }),
    [],
  );

  useEffect(() => {
    if (user && Array.isArray(user.authority)) {
      for (let i = 0; i < user.authority.length; i++) {
        const authority = user.authority[i];

        if (
          permissions[authority] && // Check if the role exists in the permission object
          permissions[authority][entity] && // Check if the entity type exists in the permission object
          permissions[authority][entity][action] && // Check if the action exists in the permission object
          (permissions[authority][entity][action].length === 0 ||
            permissions[authority][entity][action].includes(entityStatus)) // Check if the entity status is allowed for the given action
        ) {
          setIsAuthorized(true);
          break; // Exit the loop if authorized
        }
      }
    }
  }, [user, entity, action, entityStatus, permissions]);

  if (
    entity === 'DISPUTE' &&
    action === 'CREATE' &&
    !user.authority.includes('ADMIN') &&
    !user.authority.includes('MOT') &&
    !user.authority.includes('OP_MANAGER')
  ) {
    return true;
  }

  if (action === 'EXPORT' && !user.authority.includes('GATE_OP')) {
    return true;
  }

  return isAuthorized;
}
