"""
Unit tests for ChatUI — no real model or Gradio launch required.
"""

import pytest
from unittest.mock import MagicMock, patch

from arielai import ChatUI, ACCENT_COLORS
from arielai.backends import BaseBackend
from arielai.chatbot import Chatbot


class MockBackend(BaseBackend):
    def generate(self, message, history, **kwargs):
        return f"Echo: {message}"


@pytest.fixture
def bot():
    return Chatbot(backend_instance=MockBackend())


class TestChatUIInit:
    def test_default_construction(self, bot):
        ui = ChatUI(bot)
        assert ui.brand_name == "ArielAI"
        assert ui.dark_mode is False
        assert ui.show_footer is True

    def test_custom_construction(self, bot):
        ui = ChatUI(
            bot,
            brand_name="MyBot",
            tagline="Hello world",
            accent="rose",
            dark_mode=True,
            show_footer=False,
        )
        assert ui.brand_name == "MyBot"
        assert ui.tagline == "Hello world"
        assert ui.accent == ACCENT_COLORS["rose"]
        assert ui.dark_mode is True
        assert ui.show_footer is False

    def test_custom_hex_accent(self, bot):
        ui = ChatUI(bot, accent="#ff6b6b")
        assert ui.accent == "#ff6b6b"

    def test_unknown_accent_falls_back_to_value(self, bot):
        ui = ChatUI(bot, accent="not-a-preset")
        assert ui.accent == "not-a-preset"


class TestAccentColors:
    def test_all_presets_present(self):
        expected = {"indigo", "emerald", "rose", "amber", "blue", "violet", "cyan", "slate"}
        assert expected.issubset(set(ACCENT_COLORS.keys()))

    def test_all_values_are_hex(self):
        for name, hex_val in ACCENT_COLORS.items():
            assert hex_val.startswith("#"), f"Accent '{name}' is not a hex color"

    def test_list_accents(self, bot):
        ui = ChatUI(bot)
        accents = ui.list_accents()
        assert isinstance(accents, dict)
        assert "indigo" in accents


class TestChatUIExtraCSS:
    def test_extra_css_stored(self, bot):
        ui = ChatUI(bot, extra_css=".custom { color: red; }")
        assert ".custom" in ui.extra_css


class TestChatUIImport:
    def test_importable_from_top_level(self):
        from arielai import ChatUI as C
        assert C is ChatUI
