# tests/unit/utils/test_page_context.py
"""
Unit tests for page context utilities.

Tests cover:
- base_url calculation for storefront pages
- Dev mode (path-based) with platform prefix
- Prod mode (subdomain/custom domain)
- Ensures no double 'storefront/' in URLs
"""

from unittest.mock import Mock

import pytest
from fastapi import Request

from app.modules.core.utils.page_context import get_storefront_context


def _make_storefront_request(
    path="/storefront/TESTSHOP/products",
    store=None,
    store_context=None,
    platform=None,
    platform_original_path=None,
    theme=None,
    clean_path="/products",
    language="en",
):
    """Create a mock request with storefront state."""
    request = Mock(spec=Request)
    request.url = Mock(path=path)
    request.method = "GET"
    request.headers = {"host": "localhost:8000"}
    request.state = Mock()
    request.state.store = store
    request.state.store_context = store_context
    request.state.clean_path = clean_path
    request.state.platform = platform
    request.state.platform_original_path = platform_original_path
    request.state.theme = theme
    request.state.language = language
    request.state.subscription = None
    request.state.subscription_tier = None
    request.state.frontend_type = None
    return request


def _make_store(store_code="TESTSHOP", subdomain="testshop"):
    store = Mock()
    store.store_code = store_code
    store.subdomain = subdomain
    store.id = 1
    store.name = "Test Shop"
    store.merchant_id = 1
    return store


def _make_platform(code="loyalty", platform_id=3):
    platform = Mock()
    platform.code = code
    platform.id = platform_id
    return platform


@pytest.mark.unit
class TestBaseUrlCalculation:
    """Tests for base_url in storefront context -- prevents double 'storefront/' regression."""

    def test_base_url_dev_mode_with_platform_prefix(self):
        """Dev mode: base_url includes /platforms/{code}/storefront/{subdomain}/."""
        store = _make_store(store_code="FASHIONHUB", subdomain="fashionhub")
        platform = _make_platform(code="loyalty")
        store_context = {"detection_method": "path", "full_prefix": "/storefront/"}

        request = _make_storefront_request(
            store=store,
            store_context=store_context,
            platform=platform,
            platform_original_path="/platforms/loyalty/storefront/fashionhub/products",
        )

        # Mock the module-level context building to avoid DB calls
        # We only need to test the base_url logic, so patch what's needed
        context = get_storefront_context(request)

        assert context["base_url"] == "/platforms/loyalty/storefront/fashionhub/"
        # Verify no double storefront when building links
        assert "/storefront/storefront/" not in context["base_url"]

    def test_base_url_dev_mode_without_platform_prefix(self):
        """Dev mode without /platforms/ prefix: base_url = /storefront/{subdomain}/."""
        store = _make_store(store_code="TESTSHOP", subdomain="testshop")
        store_context = {"detection_method": "path", "full_prefix": "/storefront/"}

        request = _make_storefront_request(
            store=store,
            store_context=store_context,
            platform=None,
            platform_original_path=None,
        )

        context = get_storefront_context(request)

        assert context["base_url"] == "/storefront/testshop/"
        assert "/storefront/storefront/" not in context["base_url"]

    def test_base_url_prod_mode_subdomain(self):
        """Prod mode with subdomain: base_url = /."""
        store = _make_store()
        store_context = {"detection_method": "subdomain", "subdomain": "testshop"}

        request = _make_storefront_request(
            store=store,
            store_context=store_context,
        )

        context = get_storefront_context(request)

        assert context["base_url"] == "/"

    def test_base_url_no_store_context(self):
        """No store context: base_url = /."""
        request = _make_storefront_request(store=None, store_context=None)

        context = get_storefront_context(request)

        assert context["base_url"] == "/"

    def test_base_url_uses_subdomain_not_store_code(self):
        """base_url should use subdomain (lowercase) not store_code (uppercase) — the
        store-context middleware resolves URLs by the lowercase slug."""
        store = _make_store(store_code="FASHIONHUB", subdomain="fashionhub")
        platform = _make_platform(code="loyalty")
        store_context = {"detection_method": "path", "full_prefix": "/storefront/"}

        request = _make_storefront_request(
            store=store,
            store_context=store_context,
            platform=platform,
            platform_original_path="/platforms/loyalty/storefront/fashionhub/products",
        )

        context = get_storefront_context(request)

        assert "fashionhub" in context["base_url"]
        assert "FASHIONHUB" not in context["base_url"]

    def test_base_url_falls_back_to_store_code_when_no_subdomain(self):
        """If subdomain is missing, fall back to store_code."""
        store = _make_store(store_code="FASHIONHUB", subdomain=None)
        platform = _make_platform(code="loyalty")
        store_context = {"detection_method": "path", "full_prefix": "/storefront/"}

        request = _make_storefront_request(
            store=store,
            store_context=store_context,
            platform=platform,
            platform_original_path="/platforms/loyalty/storefront/FASHIONHUB/products",
        )

        context = get_storefront_context(request)

        assert context["base_url"] == "/platforms/loyalty/storefront/FASHIONHUB/"
