"""Tests for PromptFetchRequest and PromptPayload."""

from __future__ import annotations

import pytest
from pydantic import ValidationError

from dr_langfuse.prompts.prompt_fetch_request import PromptFetchRequest
from dr_langfuse.prompts.prompt_payload import PromptPayload


def test_prompt_fetch_request_roundtrip() -> None:
    fetch_req = PromptFetchRequest.model_validate(
        {"prompt_name": "summarize", "label": "prod", "version": 1}
    )
    fetch_dump = fetch_req.model_dump(mode="json")
    assert (
        PromptFetchRequest.model_validate(fetch_dump).model_dump(mode="json")
        == fetch_dump
    )


def test_prompt_payload_roundtrip() -> None:
    payload = PromptPayload.model_validate(
        {
            "prompt_name": "summarize",
            "system_content": "You are concise.",
            "task_content": "Summarize this text.",
            "label": "prod",
            "version": 1,
        }
    )
    payload_dump = payload.model_dump(mode="json")
    assert (
        PromptPayload.model_validate(payload_dump).model_dump(mode="json")
        == payload_dump
    )


def test_prompt_fetch_request_rejects_empty_name() -> None:
    with pytest.raises(ValidationError):
        PromptFetchRequest.model_validate({})
    with pytest.raises(ValidationError):
        PromptFetchRequest.model_validate({"prompt_name": ""})


def test_prompt_fetch_request_rejects_non_json_variables() -> None:
    with pytest.raises(ValidationError):
        PromptFetchRequest.model_validate(
            {"prompt_name": "x", "variables": {"bad": object()}}
        )


def test_prompt_fetch_request_rejects_nan() -> None:
    with pytest.raises(ValidationError):
        PromptFetchRequest.model_validate(
            {"prompt_name": "x", "variables": {"bad": float("nan")}}
        )


def test_prompt_fetch_request_rejects_inf() -> None:
    with pytest.raises(ValidationError):
        PromptFetchRequest.model_validate(
            {"prompt_name": "x", "variables": {"bad": float("inf")}}
        )


def test_prompt_payload_rejects_empty_name() -> None:
    with pytest.raises(ValidationError):
        PromptPayload.model_validate({"prompt_name": "", "task_content": "x"})


def test_prompt_payload_rejects_null_system_content() -> None:
    with pytest.raises(ValidationError):
        PromptPayload.model_validate({"prompt_name": "x", "system_content": None})


def test_prompt_payload_defaults_system_content_to_empty() -> None:
    payload = PromptPayload.model_validate(
        {"prompt_name": "x", "task_content": "run this"}
    )
    assert payload.system_content == ""
