import pytest
from aura_core.theme.engine import (
    generate_theme,
    hex_to_rgb,
    get_mood_profile,
    compute_colors,
    MOOD_PROFILES,
)
from aura_core.types import ThemeConfig, ThemeColors


class TestHexToRgb:
    def test_basic(self):
        assert hex_to_rgb("#ffffff") == "255,255,255"
        assert hex_to_rgb("#000000") == "0,0,0"

    def test_short_hex(self):
        assert hex_to_rgb("#fff") == "255,255,255"

    def test_no_hash(self):
        assert hex_to_rgb("4F46E5") == "79,70,229"

    def test_invalid_returns_fallback(self):
        assert hex_to_rgb("notacolor") == "128,128,128"


class TestMoodProfiles:
    def test_all_moods_exist(self):
        for mood in ["professional", "creative", "minimal", "bold", "warm"]:
            assert mood in MOOD_PROFILES

    def test_profile_has_required_keys(self):
        required = ["card_style", "border_radius", "shadow_intensity",
                     "font_weight_heading", "letter_spacing", "section_header_style"]
        for mood, profile in MOOD_PROFILES.items():
            for key in required:
                assert key in profile, f"{mood} missing {key}"


class TestGetMoodProfile:
    def test_known_mood(self):
        p = get_mood_profile("professional")
        assert p["card_style"] is not None

    def test_unknown_defaults_professional(self):
        p = get_mood_profile("nonexistent")
        assert p == MOOD_PROFILES["professional"]


class TestComputeColors:
    def test_returns_theme_colors(self):
        colors = compute_colors("#4F46E5", "#7C3AED", "#06B6D4")
        assert isinstance(colors, ThemeColors)
        assert colors.primary == "#4F46E5"
        assert colors.primary_rgb == "79,70,229"

    def test_auto_generates_variants(self):
        colors = compute_colors("#4F46E5", "#7C3AED", "#06B6D4")
        assert colors.primary_light != ""
        assert colors.primary_dark != ""
        assert colors.primary_text != ""


class TestGenerateTheme:
    def test_basic_generation(self):
        theme = generate_theme(
            mood="professional",
            primary="#4F46E5",
            secondary="#7C3AED",
            accent="#06B6D4",
            niche="software",
        )
        assert isinstance(theme, ThemeConfig)
        assert theme.mood == "professional"
        assert theme.colors.primary == "#4F46E5"

    def test_all_moods(self):
        for mood in ["professional", "creative", "minimal", "bold", "warm"]:
            theme = generate_theme(mood=mood, primary="#4F46E5", secondary="#7C3AED", accent="#06B6D4")
            assert theme.mood == mood
            assert theme.card_style != ""

    def test_default_mood(self):
        theme = generate_theme(primary="#4F46E5", secondary="#7C3AED", accent="#06B6D4")
        assert theme.mood == "professional"

    def test_has_section_backgrounds(self):
        theme = generate_theme(mood="bold", primary="#FF0000", secondary="#00FF00", accent="#0000FF")
        assert isinstance(theme.section_backgrounds, dict)

    def test_has_dividers(self):
        theme = generate_theme(mood="creative", primary="#4F46E5", secondary="#7C3AED", accent="#06B6D4")
        assert isinstance(theme.dividers, dict)

    def test_has_animated_bg(self):
        theme = generate_theme(mood="creative", primary="#4F46E5", secondary="#7C3AED", accent="#06B6D4")
        assert theme.animated_bg_keyframes != ""
