# 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

"""
Unit tests for various general utility functions that don't have any connection to domain/business logic
"""

import pytest

from czc.util import deep_get


@pytest.mark.unit
def test_deep_get_exact_match():
    input_dict = {'foo': {'123': 'abc', '456': 'def'}, 'bar': {'789': 'ghi', '012': 'jkl'}, 'baz': {'345': 'mno', '678': 'pqr'}}
    result = deep_get(input_dict, 'baz', '345')
    assert 'mno' == result
    result = deep_get(input_dict, 'foo')
    assert {'123': 'abc', '456': 'def'} == result
    result = deep_get(input_dict, 'BaR', '012')
    assert result is None


@pytest.mark.unit
def test_deep_get_case_insensitive_match():
    input_dict = {
        'foo': {'123': 'abc', '456': 'def', 'AAA': {'BBB': '919'}},
        'bar': {'789': 'ghi', '012': 'jkl'},
        'bAz': {'345': 'mno', '678': 'pqr'},
    }
    result = deep_get(input_dict, 'BaR', '012', ignore_case=True)
    assert 'jkl' == result
    result = deep_get(input_dict, 'bar', '012', ignore_case=True)
    assert 'jkl' == result
    result = deep_get(input_dict, 'baz', '345', ignore_case=True)
    assert 'mno' == result
    result = deep_get(input_dict, 'FOO', 'aAa', 'BbB', ignore_case=True)
    assert '919' == result
    result = deep_get(input_dict, 'BaR', '012', ignore_case=False)
    assert result is None
    result = deep_get(input_dict, 'bar', '012', ignore_case=False)
    assert 'jkl' == result


@pytest.mark.unit
def test_deep_get_case_mixed_keys():
    input_dict = {
        frozenset({1, 2, 3}): {123: 'abc', 456.9: 'def'},
        123: {(3, 4): 'ghi', (5, 6): 'jkl'},
        (1, 2): {'345': 'mno', '678': 'pqr'},
    }
    result = deep_get(input_dict, 123, (5, 6), ignore_case=True)
    assert 'jkl' == result
    result = deep_get(input_dict, (1, 2), '678', ignore_case=False)
    assert 'pqr' == result
    result = deep_get(input_dict, frozenset({1, 2, 3}), 456.9)
    assert 'def' == result


@pytest.mark.unit
def test_deep_get_bad_input():
    result = deep_get(None, 'foo', '012')
    assert result is None
    result = deep_get([], 'foo', '012')
    assert result is None


@pytest.mark.unit
def test_deep_get_identity_input():
    result = deep_get({'foo': 'bar'})
    assert result == {'foo': 'bar'}


@pytest.mark.unit
def test_deep_get_no_match():
    input_dict = {'foo': {'123': 'abc', '456': 'def'}, 'bar': {'789': 'ghi', '012': 'jkl'}, 'baz': {'345': 'mno', '678': 'pqr'}}
    result = deep_get(input_dict, 'bar', 'aaa')
    assert result is None
    result = deep_get(input_dict, 'foobar')
    assert result is None
    result = deep_get(input_dict, None)
    assert result is None
