# Copyright (c) 2025 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 pytest

from czc.api.v2.exceptions import ServiceError
from czc.environ import environ_aware, namespace_aware


@pytest.mark.unit
@pytest.mark.parametrize(
    'required, optional',
    [
        ({'NAMESPACE': 'test-ns'}, {}),
        ({'NAMESPACE': 'test-ns'}, {'FOO': 'test-foo'}),
    ],
)
def test_environ_aware_named_kwargs(environ, required, optional):
    for k, v in required.items():
        environ[k] = v
    for k, v in optional.items():
        environ[k] = v

    @environ_aware(required=list(required.keys()), optional=list(optional.keys()))
    def handler(e, c, NAMESPACE=None, FOO=None):
        assert NAMESPACE == required['NAMESPACE']
        assert FOO == optional.get('FOO')

    handler({}, None)


@pytest.mark.unit
def test_environ_aware_required_missing():
    @environ_aware(required=['REQUIRED_VAR'])
    def handler(e, c, NAMESPACE=None, FOO=None):
        pass

    with pytest.raises(ServiceError, match='REQUIRED_VAR environment variable missing.'):
        handler({}, None)


@pytest.mark.unit
def test_namespace_aware(environ):
    @namespace_aware
    def handler(e, c, NAMESPACE=None):
        assert NAMESPACE == environ['NAMESPACE']

    handler({}, None)


@pytest.mark.unit
def test_namespace_aware_not_set(environ):
    environ.pop('NAMESPACE')

    @namespace_aware
    def handler(e, c, NAMESPACE=None):
        pass

    with pytest.raises(ServiceError, match='NAMESPACE environment variable missing.'):
        handler({}, None)
