import json

from lib import authz
from lib.logger import logger
from lib.exclusions import exclusions, state_machine


def get_allowed_actions(user, account_id, requirement, exclusion):
    allowed_actions = {
        'remediate': False,
        'requestExclusion': False,
        'requestExclusionChange': False,
    }
    current_state = exclusions.get_state(exclusion)
    valid_state_transitions = state_machine.USER_STATE_TRANSITIONS.get(current_state, {}).keys()
    logger.debug('Current state: %s', current_state)
    logger.debug('Valid state transitions: %s', str(valid_state_transitions))
    logger.debug('User: %s', json.dumps(user))

    if authz.can_request_exclusion(user, account_id)[0]:
        if set(valid_state_transitions) & set(exclusions.REQUEST_EXCLUSION_STATES):
            allowed_actions['requestExclusion'] = True
        if set(valid_state_transitions) & set(exclusions.REQUEST_EXCLUSION_CHANGE_STATES):
            allowed_actions['requestExclusionChange'] = True

    # Determine If can remediate
    if can_requirement_be_remediated(requirement):
        allowed_actions['remediate'] = authz.can_remediate(user, account_id)[0]
    return allowed_actions


def can_requirement_be_remediated(requirement):
    """
    Mehtod to validate whether a requirement is capable of being remediated.

    :param requirement: The dict representing the requirement to check.

    :returns bool: A boolean representing whether requirement can or cannot be remediated.
    """
    return 'remediation' in requirement

[((14, 20, 14, 51), 'lib.exclusions.exclusions.get_state', 'exclusions.get_state', ({(14, 41, 14, 50): 'exclusion'}, {}), '(exclusion)', False, 'from lib.exclusions import exclusions, state_machine\n'), ((16, 4, 16, 52), 'lib.logger.logger.debug', 'logger.debug', ({(16, 17, 16, 36): '"""Current state: %s"""', (16, 38, 16, 51): 'current_state'}, {}), "('Current state: %s', current_state)", False, 'from lib.logger import logger\n'), ((18, 29, 18, 45), 'json.dumps', 'json.dumps', ({(18, 40, 18, 44): 'user'}, {}), '(user)', False, 'import json\n'), ((20, 7, 20, 52), 'lib.authz.can_request_exclusion', 'authz.can_request_exclusion', ({(20, 35, 20, 39): 'user', (20, 41, 20, 51): 'account_id'}, {}), '(user, account_id)', False, 'from lib import authz\n'), ((15, 30, 15, 89), 'lib.exclusions.state_machine.USER_STATE_TRANSITIONS.get', 'state_machine.USER_STATE_TRANSITIONS.get', ({(15, 71, 15, 84): 'current_state', (15, 86, 15, 88): '{}'}, {}), '(current_state, {})', False, 'from lib.exclusions import exclusions, state_machine\n'), ((28, 39, 28, 76), 'lib.authz.can_remediate', 'authz.can_remediate', ({(28, 59, 28, 63): 'user', (28, 65, 28, 75): 'account_id'}, {}), '(user, account_id)', False, 'from lib import authz\n')]