# The MIT License (MIT)
#
# Copyright (c) 2025 FourCIPP Authors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Test inline dat utils."""

from functools import partial

import pytest

from fourcipp.legacy_io.inline_dat import (
    _entry_casting_factory,
    _extract_entry,
    _extract_enum,
    casting_factory,
    inline_dat_read,
)


def test_extract_enum_invalid_choice():
    """Test enum extract for invalid choice."""
    with pytest.raises(ValueError, match="Unknown entry"):
        _extract_enum(["a"], choices=["b", "c"])


def test_entry_casting_factory_invalid_type():
    """Test reading invalid types."""
    with pytest.raises(NotImplementedError, match="Entry type"):
        _entry_casting_factory({"type": "invalid"})


@pytest.mark.parametrize(
    "value,expected_value",
    [
        ("true", True),
        ("yes", True),
        ("1", True),
        ("false", False),
        ("no", False),
        ("0", False),
    ],
)
def test_entry_casting_factory_bool_cast(value, expected_value):
    """Test bool casting."""
    spec = {"type": "bool"}
    cast_func = _entry_casting_factory(spec)

    assert cast_func([value]) is expected_value


@pytest.mark.parametrize(
    "invalid_input", ["True", "False", "some text", "TRUE", "FALSE"]
)
def test_entry_casting_factory_invalid_bool_cast(invalid_input):
    """Test invalid bool casting."""
    spec = {"type": "bool"}
    cast_func = _entry_casting_factory(spec)

    with pytest.raises(KeyError):
        cast_func([invalid_input])


def test_casting_factory_invalid_type():
    """Test reading invalid types."""
    with pytest.raises(NotImplementedError, match="First entry has to be an all_of"):
        casting_factory({"type": "invalid"})


def test_doubled_inline_keyword():
    """Test if parameter is set twice."""
    with pytest.raises(KeyError, match="The entry a was provided already"):
        inline_dat_read(
            ["a", "1", "b", "1", "a", "2"],
            keyword_casting={
                "a": partial(_extract_entry, extractor=int),
                "b": partial(_extract_entry, extractor=int),
            },
        )
