################################################################################
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
# limitations under the License.
#################################################################################
from __future__ import annotations

import json
from pathlib import Path

import pytest

from flink_agents.api.tools.tool import ToolMetadata
from flink_agents.api.tools.utils import create_schema_from_function

current_dir = Path(__file__).parent


def foo(bar: int, baz: str) -> str:
    """Function for testing ToolMetadata.

    Parameters
    ----------
    bar : int
        The bar value.
    baz : str
        The baz value.

    Returns:
    -------
    str
        Response string value.
    """
    raise NotImplementedError


@pytest.fixture(scope="module")
def tool_metadata() -> ToolMetadata:
    return ToolMetadata(
        name="foo",
        description="Function for testing ToolMetadata",
        args_schema=create_schema_from_function(name="foo", func=foo),
    )


def test_serialize_tool_metadata(tool_metadata: ToolMetadata) -> None:
    json_value = tool_metadata.model_dump_json(serialize_as_any=True)
    with Path(f"{current_dir}/resources/tool_metadata.json").open() as f:
        expected_json = f.read()
    actual = json.loads(json_value)
    expected = json.loads(expected_json)
    assert actual == expected


def test_deserialize_tool_metadata(tool_metadata: ToolMetadata) -> None:
    with Path(f"{current_dir}/resources/tool_metadata.json").open() as f:
        expected_json = f.read()
    actual_tool_metadata = tool_metadata.model_validate_json(expected_json)
    assert actual_tool_metadata == tool_metadata
