from __future__ import annotations

import uuid

import pytest
from pydantic import ValidationError

from codeflash_api.logging.schemas import (
    LogFeaturesErrorResponse,
    LogFeaturesRequest,
)


class TestLogFeaturesRequest:
    """Tests for LogFeaturesRequest schema."""

    def test_minimal_valid(self) -> None:
        """
        Only trace_id is required; all other fields default to None.
        """
        req = LogFeaturesRequest(trace_id="abc")
        assert "abc" == req.trace_id
        assert req.speedup_ratio is None
        assert req.original_runtime is None
        assert req.optimized_runtime is None
        assert req.is_correct is None
        assert req.optimized_line_profiler_results is None
        assert req.metadata is None
        assert req.optimizations_post is None

    def test_all_fields(self) -> None:
        """
        All optional fields are accepted when provided.
        """
        req = LogFeaturesRequest(
            trace_id=str(uuid.uuid4()),
            speedup_ratio={"opt1": 2.5, "opt2": None},
            original_runtime=100.5,
            optimized_runtime={"opt1": 40.2, "opt2": None},
            is_correct={"opt1": True, "opt2": False},
            optimized_line_profiler_results={"opt1": "data"},
            metadata={"key": "value", "nested": {"a": 1}},
            optimizations_post={"opt1": "code"},
        )
        assert {"opt1": 2.5, "opt2": None} == req.speedup_ratio
        assert 100.5 == req.original_runtime
        assert {"opt1": True, "opt2": False} == req.is_correct

    def test_integer_runtime(self) -> None:
        """
        Integer values are accepted for runtime fields.
        """
        req = LogFeaturesRequest(
            trace_id="t",
            original_runtime=100,
            optimized_runtime={"a": 50},
            speedup_ratio={"a": 2},
        )
        assert 100 == req.original_runtime
        assert {"a": 50} == req.optimized_runtime

    def test_missing_trace_id_rejected(self) -> None:
        """
        Omitting trace_id raises a validation error.
        """
        with pytest.raises(ValidationError):
            LogFeaturesRequest()  # type: ignore[call-arg]


class TestLogFeaturesErrorResponse:
    """Tests for LogFeaturesErrorResponse schema."""

    def test_error_message(self) -> None:
        """
        Error response carries the error string.
        """
        resp = LogFeaturesErrorResponse(error="something broke")
        assert "something broke" == resp.error

    def test_missing_error_rejected(self) -> None:
        """
        Omitting error raises a validation error.
        """
        with pytest.raises(ValidationError):
            LogFeaturesErrorResponse()  # type: ignore[call-arg]
