"""
Unit tests for Page model SEO fields.

Tests meta_title, meta_description, og_image, canonical_url, noindex,
include_in_nav, and hide_chrome fields.
"""
from unittest.mock import Mock, patch
import pytest


class TestPageSeoFields:
    """Test Page model SEO field existence and defaults."""

    def test_meta_title_field_exists(self):
        """Should have meta_title field."""
        from smoothschedule.platform.tenant_sites.models import Page

        field_names = [f.name for f in Page._meta.get_fields()]
        assert 'meta_title' in field_names

    def test_meta_title_max_length(self):
        """Should have max_length of 255."""
        from smoothschedule.platform.tenant_sites.models import Page

        field = Page._meta.get_field('meta_title')
        assert field.max_length == 255

    def test_meta_title_allows_blank(self):
        """Should allow blank meta_title."""
        from smoothschedule.platform.tenant_sites.models import Page

        field = Page._meta.get_field('meta_title')
        assert field.blank is True

    def test_meta_description_field_exists(self):
        """Should have meta_description field."""
        from smoothschedule.platform.tenant_sites.models import Page

        field_names = [f.name for f in Page._meta.get_fields()]
        assert 'meta_description' in field_names

    def test_meta_description_allows_blank(self):
        """Should allow blank meta_description."""
        from smoothschedule.platform.tenant_sites.models import Page

        field = Page._meta.get_field('meta_description')
        assert field.blank is True

    def test_og_image_field_exists(self):
        """Should have og_image field."""
        from smoothschedule.platform.tenant_sites.models import Page

        field_names = [f.name for f in Page._meta.get_fields()]
        assert 'og_image' in field_names

    def test_og_image_allows_blank(self):
        """Should allow blank og_image."""
        from smoothschedule.platform.tenant_sites.models import Page

        field = Page._meta.get_field('og_image')
        assert field.blank is True

    def test_canonical_url_field_exists(self):
        """Should have canonical_url field."""
        from smoothschedule.platform.tenant_sites.models import Page

        field_names = [f.name for f in Page._meta.get_fields()]
        assert 'canonical_url' in field_names

    def test_canonical_url_allows_blank(self):
        """Should allow blank canonical_url."""
        from smoothschedule.platform.tenant_sites.models import Page

        field = Page._meta.get_field('canonical_url')
        assert field.blank is True

    def test_noindex_field_exists(self):
        """Should have noindex field."""
        from smoothschedule.platform.tenant_sites.models import Page

        field_names = [f.name for f in Page._meta.get_fields()]
        assert 'noindex' in field_names

    def test_noindex_default_false(self):
        """Should default noindex to False."""
        from smoothschedule.platform.tenant_sites.models import Page

        field = Page._meta.get_field('noindex')
        assert field.default is False

    def test_include_in_nav_field_exists(self):
        """Should have include_in_nav field."""
        from smoothschedule.platform.tenant_sites.models import Page

        field_names = [f.name for f in Page._meta.get_fields()]
        assert 'include_in_nav' in field_names

    def test_include_in_nav_default_true(self):
        """Should default include_in_nav to True."""
        from smoothschedule.platform.tenant_sites.models import Page

        field = Page._meta.get_field('include_in_nav')
        assert field.default is True

    def test_hide_chrome_field_exists(self):
        """Should have hide_chrome field for landing page mode."""
        from smoothschedule.platform.tenant_sites.models import Page

        field_names = [f.name for f in Page._meta.get_fields()]
        assert 'hide_chrome' in field_names

    def test_hide_chrome_default_false(self):
        """Should default hide_chrome to False."""
        from smoothschedule.platform.tenant_sites.models import Page

        field = Page._meta.get_field('hide_chrome')
        assert field.default is False


class TestPageSeoHelpers:
    """Test Page model SEO helper methods."""

    def test_get_effective_title_returns_meta_title_when_set(self):
        """Should return meta_title when set."""
        from smoothschedule.platform.tenant_sites.models import Page

        page = Page()
        page.title = 'About Us'
        page.meta_title = 'About Our Company - My Business'

        result = page.get_effective_title()

        assert result == 'About Our Company - My Business'

    def test_get_effective_title_falls_back_to_title(self):
        """Should fall back to title when meta_title is empty."""
        from smoothschedule.platform.tenant_sites.models import Page

        page = Page()
        page.title = 'About Us'
        page.meta_title = ''

        result = page.get_effective_title()

        assert result == 'About Us'

    def test_get_robots_meta_returns_noindex_when_set(self):
        """Should return noindex when noindex is True."""
        from smoothschedule.platform.tenant_sites.models import Page

        page = Page()
        page.noindex = True

        result = page.get_robots_meta()

        assert 'noindex' in result

    def test_get_robots_meta_returns_index_when_not_set(self):
        """Should return index when noindex is False."""
        from smoothschedule.platform.tenant_sites.models import Page

        page = Page()
        page.noindex = False

        result = page.get_robots_meta()

        assert 'index' in result
        assert 'noindex' not in result


class TestPageSerializerSeoFields:
    """Test PageSerializer includes SEO fields."""

    def test_serializer_includes_seo_fields(self):
        """Should include all SEO fields in serialization."""
        from smoothschedule.platform.tenant_sites.serializers import PageSerializer

        serializer = PageSerializer()
        field_names = serializer.fields.keys()

        assert 'meta_title' in field_names
        assert 'meta_description' in field_names
        assert 'og_image' in field_names
        assert 'canonical_url' in field_names
        assert 'noindex' in field_names
        assert 'include_in_nav' in field_names
        assert 'hide_chrome' in field_names

    def test_public_serializer_includes_seo_fields(self):
        """PublicPageSerializer should include SEO fields for rendering."""
        from smoothschedule.platform.tenant_sites.serializers import PublicPageSerializer

        serializer = PublicPageSerializer()
        field_names = serializer.fields.keys()

        assert 'meta_title' in field_names
        assert 'meta_description' in field_names
        assert 'og_image' in field_names
        assert 'canonical_url' in field_names
        assert 'noindex' in field_names
        assert 'hide_chrome' in field_names
