# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

import functools

from czc.api.environment import TestKeyEnvironment
from czc.api.v2.constants import CLOUDZERO_TEST_KEY
from czc.constants import CLOUDZERO_TEST_KEY_EVENT_PARAMETER
from czc.logging import get_logger

logger = get_logger()


def iapi_test_key_aware():
    f"""Make this iAPI Lambda Handler Test Key Aware

    If {CLOUDZERO_TEST_KEY_EVENT_PARAMETER} key is present in lambda event, set {CLOUDZERO_TEST_KEY} environment
    variable so that downstream processing (usually DynamoDB) honors it.

    Parameters
    ----------
    ignore_case: bool, optional defaults to True
        Whether or not to ignore the case of the

    Returns
    -------
    Callable
        Decorated Lambda Handler
    """

    def _iapi_handler(handler):
        @functools.wraps(handler)
        def handler_wrapper(event, context, **kwargs):
            with TestKeyEnvironment(event.get(CLOUDZERO_TEST_KEY_EVENT_PARAMETER)):
                return handler(event, context, **kwargs)

        return handler_wrapper

    return _iapi_handler
