"""
Unit tests for SiteConfig model and related functionality.

Tests the theme tokens, header/footer chrome, and versioning.
"""
from unittest.mock import Mock, patch
import pytest


class TestSiteConfigModel:
    """Test SiteConfig model fields and methods."""

    def test_model_exists(self):
        """Should have SiteConfig model."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        assert SiteConfig is not None

    def test_str_method(self):
        """Should return 'Config for {site}'."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        config = SiteConfig()
        mock_site = Mock()
        mock_site.__str__ = Mock(return_value='Site for Test Business')
        config._state.fields_cache['site'] = mock_site

        result = str(config)

        assert 'Config' in result

    def test_model_has_required_fields(self):
        """Should have site, theme, header, footer, version fields."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        field_names = [f.name for f in SiteConfig._meta.get_fields()]
        assert 'site' in field_names
        assert 'theme' in field_names
        assert 'header' in field_names
        assert 'footer' in field_names
        assert 'version' in field_names
        assert 'created_at' in field_names
        assert 'updated_at' in field_names

    def test_site_one_to_one_relationship(self):
        """Should have OneToOne relationship with Site."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        site_field = SiteConfig._meta.get_field('site')
        assert site_field.one_to_one is True

    def test_theme_default_value(self):
        """Should default theme to empty dict."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        theme_field = SiteConfig._meta.get_field('theme')
        assert theme_field.default == dict

    def test_header_default_value(self):
        """Should default header to empty dict."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        header_field = SiteConfig._meta.get_field('header')
        assert header_field.default == dict

    def test_footer_default_value(self):
        """Should default footer to empty dict."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        footer_field = SiteConfig._meta.get_field('footer')
        assert footer_field.default == dict

    def test_version_default_value(self):
        """Should default version to 1."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        version_field = SiteConfig._meta.get_field('version')
        assert version_field.default == 1


class TestSiteConfigThemeStructure:
    """Test theme token structure validation."""

    def test_get_default_theme_returns_complete_structure(self):
        """Should return theme with colors, typography, buttons, sections."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        config = SiteConfig()
        default_theme = config.get_default_theme()

        assert 'colors' in default_theme
        assert 'typography' in default_theme
        assert 'buttons' in default_theme
        assert 'sections' in default_theme

    def test_get_default_theme_colors(self):
        """Should have primary, secondary, accent colors."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        config = SiteConfig()
        default_theme = config.get_default_theme()
        colors = default_theme['colors']

        assert 'primary' in colors
        assert 'secondary' in colors
        assert 'accent' in colors
        assert 'background' in colors
        assert 'text' in colors

    def test_get_merged_theme_uses_defaults_for_missing(self):
        """Should merge custom theme with defaults."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        config = SiteConfig()
        config.theme = {'colors': {'primary': '#ff0000'}}

        merged = config.get_merged_theme()

        # Custom value preserved
        assert merged['colors']['primary'] == '#ff0000'
        # Default values filled in
        assert 'secondary' in merged['colors']
        assert 'typography' in merged


class TestSiteConfigHeaderFooter:
    """Test header/footer chrome configuration."""

    def test_get_default_header_structure(self):
        """Should return header with enabled, logo, navigation."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        config = SiteConfig()
        default_header = config.get_default_header()

        assert 'enabled' in default_header
        assert 'logo' in default_header
        assert 'navigation' in default_header
        assert 'sticky' in default_header

    def test_get_default_footer_structure(self):
        """Should return footer with enabled, columns, copyright."""
        from smoothschedule.platform.tenant_sites.models import SiteConfig

        config = SiteConfig()
        default_footer = config.get_default_footer()

        assert 'enabled' in default_footer
        assert 'columns' in default_footer
        assert 'copyright' in default_footer
        assert 'socialLinks' in default_footer


class TestSiteConfigSignal:
    """Test SiteConfig auto-creation signal."""

    def test_creates_config_on_new_site(self):
        """Should auto-create SiteConfig when Site is created."""
        from smoothschedule.platform.tenant_sites.models import (
            create_config_for_site, Site, SiteConfig
        )

        mock_site = Mock(spec=Site)
        mock_site.id = 1

        with patch.object(SiteConfig.objects, 'get_or_create') as mock_create:
            mock_create.return_value = (Mock(), True)

            create_config_for_site(
                sender=Site,
                instance=mock_site,
                created=True
            )

            mock_create.assert_called_once_with(site=mock_site)

    def test_does_not_create_config_on_site_update(self):
        """Should not create SiteConfig when Site is updated."""
        from smoothschedule.platform.tenant_sites.models import (
            create_config_for_site, Site, SiteConfig
        )

        mock_site = Mock(spec=Site)

        with patch.object(SiteConfig.objects, 'get_or_create') as mock_create:
            create_config_for_site(
                sender=Site,
                instance=mock_site,
                created=False
            )

            mock_create.assert_not_called()
