"""Tests for lib/model_profiles.py — model profile queries."""

import pytest

from recoil.core.model_profiles import (
    get_profile,
    get_cost,
    get_max_refs,
    get_aspect_ratios,
    get_api_pattern,
    get_modality,
    get_fallback_model,
    supports_inline_refs,
    supports_audio,
    supports_multi_shot,
    supports_start_end_frame,
    list_models,
)


class TestGetProfile:
    def test_known_model(self):
        p = get_profile("gemini-3-pro-image-preview")
        assert p["provider"] == "google"
        assert p["modality"] == "image"

    def test_unknown_model_raises(self):
        with pytest.raises(KeyError, match="Unknown model"):
            get_profile("nonexistent-model")


class TestGetCost:
    def test_image_model_cost(self):
        assert get_cost("gemini-3-pro-image-preview") == 0.134

    def test_flash_model_cost(self):
        assert get_cost("gemini-3.1-flash-image-preview") == 0.039

    def test_video_model_cost_per_second(self):
        assert get_cost("kling-v3") == 0.126

    def test_seeddance_cost(self):
        assert get_cost("seeddance-2.0") == 0.3034


class TestModelCapabilities:
    def test_max_refs_nbp(self):
        assert get_max_refs("gemini-3-pro-image-preview") == 11

    def test_aspect_ratios(self):
        ratios = get_aspect_ratios("gemini-3-pro-image-preview")
        assert "9:16" in ratios
        assert "1:1" in ratios

    def test_inline_refs_google(self):
        assert supports_inline_refs("gemini-3-pro-image-preview") is True

    def test_inline_refs_kling(self):
        # Kling 2.5 uses upload_bundle, no inline refs
        assert supports_inline_refs("kling-2.5") is False


class TestApiPattern:
    def test_genai_inline(self):
        assert get_api_pattern("gemini-3-pro-image-preview") == "genai_inline"

    def test_kling_rest(self):
        assert get_api_pattern("kling-v3-direct") == "kling_rest"

    def test_fal_ai_kling(self):
        assert get_api_pattern("kling-v3") == "fal_ai_kling"

    def test_fal_ai(self):
        assert get_api_pattern("seeddance-2.0") == "fal_ai"

    def test_upload_bundle(self):
        assert get_api_pattern("kling-2.5") == "upload_bundle"


class TestModality:
    def test_image_models(self):
        assert get_modality("gemini-3-pro-image-preview") == "image"
        assert get_modality("gemini-3.1-flash-image-preview") == "image"

    def test_video_models(self):
        assert get_modality("kling-v3") == "video"
        assert get_modality("seeddance-2.0") == "video"
        assert get_modality("veo-3.1") == "video"


class TestVideoCapabilities:
    def test_supports_audio(self):
        assert supports_audio("seeddance-2.0") is True
        assert supports_audio("kling-v3") is True

    def test_supports_multi_shot(self):
        assert supports_multi_shot("seeddance-2.0") is True

    def test_supports_start_end_frame(self):
        assert supports_start_end_frame("kling-v3") is True


class TestFallback:
    def test_seeddance_fallback(self):
        assert get_fallback_model("seeddance-2.0") == "kling-v3"

    def test_no_fallback(self):
        assert get_fallback_model("gemini-3-pro-image-preview") is None


class TestListModels:
    def test_lists_all(self):
        models = list_models()
        assert "gemini-3-pro-image-preview" in models
        assert "kling-v3" in models
        assert "seeddance-2.0" in models
        assert len(models) >= 6
