# tests/unit/core/test_frontend_detector.py
"""
Unit tests for FrontendDetector.

Tests cover:
- Detection for all frontend types (ADMIN, STORE, STOREFRONT, PLATFORM)
- Path-based detection (dev mode)
- Subdomain-based detection (prod mode)
- Custom domain detection
- Priority order of detection methods
"""

import pytest

from app.core.frontend_detector import FrontendDetector
from app.modules.enums import FrontendType


@pytest.mark.unit
class TestFrontendDetectorAdmin:
    """Test suite for admin frontend detection."""

    def test_detect_admin_from_subdomain(self):
        """Test admin detection from admin subdomain."""
        result = FrontendDetector.detect(host="admin.omsflow.lu", path="/dashboard")
        assert result == FrontendType.ADMIN

    def test_detect_admin_from_subdomain_with_port(self):
        """Test admin detection from admin subdomain with port."""
        result = FrontendDetector.detect(host="admin.localhost:8000", path="/dashboard")
        assert result == FrontendType.ADMIN

    def test_detect_admin_from_path(self):
        """Test admin detection from /admin path."""
        result = FrontendDetector.detect(host="localhost", path="/admin/stores")
        assert result == FrontendType.ADMIN

    def test_detect_admin_from_api_path(self):
        """Test admin detection from /api/v1/admin path."""
        result = FrontendDetector.detect(host="localhost", path="/api/v1/admin/users")
        assert result == FrontendType.ADMIN

    def test_detect_admin_nested_path(self):
        """Test admin detection with nested admin path."""
        result = FrontendDetector.detect(host="omsflow.lu", path="/admin/stores/123/products")
        assert result == FrontendType.ADMIN


@pytest.mark.unit
class TestFrontendDetectorStore:
    """Test suite for store dashboard frontend detection."""

    def test_detect_store_from_path(self):
        """Test store detection from /store/ path."""
        result = FrontendDetector.detect(host="localhost", path="/store/settings")
        assert result == FrontendType.STORE

    def test_detect_store_from_api_path(self):
        """Test store detection from /api/v1/store path."""
        result = FrontendDetector.detect(host="localhost", path="/api/v1/store/products")
        assert result == FrontendType.STORE

    def test_detect_store_nested_path(self):
        """Test store detection with nested store path."""
        result = FrontendDetector.detect(host="omsflow.lu", path="/store/dashboard/analytics")
        assert result == FrontendType.STORE

    def test_stores_plural_is_not_storefront(self):
        """Test that /stores/ path is NOT storefront (old pattern removed)."""
        result = FrontendDetector.detect(host="localhost", path="/stores/orion/storefront")
        # /stores/ is no longer a storefront signal; /storefront/ is the canonical prefix
        assert result != FrontendType.STOREFRONT


@pytest.mark.unit
class TestFrontendDetectorStorefront:
    """Test suite for storefront frontend detection."""

    def test_detect_storefront_from_path(self):
        """Test storefront detection from /storefront path."""
        result = FrontendDetector.detect(host="localhost", path="/storefront/products")
        assert result == FrontendType.STOREFRONT

    def test_detect_storefront_from_api_path(self):
        """Test storefront detection from /api/v1/storefront path."""
        result = FrontendDetector.detect(host="localhost", path="/api/v1/storefront/cart")
        assert result == FrontendType.STOREFRONT

    def test_detect_storefront_from_storefront_path(self):
        """Test storefront detection from /storefront/ path (path-based store access)."""
        result = FrontendDetector.detect(host="localhost", path="/storefront/orion/products")
        assert result == FrontendType.STOREFRONT

    def test_detect_storefront_from_store_subdomain(self):
        """Test storefront detection from store subdomain."""
        result = FrontendDetector.detect(host="orion.omsflow.lu", path="/products")
        assert result == FrontendType.STOREFRONT

    def test_detect_storefront_from_store_context(self):
        """Test storefront detection when store context is set."""
        result = FrontendDetector.detect(
            host="mybakery.lu", path="/about", has_store_context=True
        )
        assert result == FrontendType.STOREFRONT



@pytest.mark.unit
class TestFrontendDetectorPlatform:
    """Test suite for platform marketing frontend detection."""

    def test_detect_platform_from_root(self):
        """Test platform detection from root path."""
        result = FrontendDetector.detect(host="localhost", path="/")
        assert result == FrontendType.PLATFORM

    def test_detect_platform_from_marketing_page(self):
        """Test platform detection from marketing page."""
        result = FrontendDetector.detect(host="omsflow.lu", path="/pricing")
        assert result == FrontendType.PLATFORM

    def test_detect_platform_from_about(self):
        """Test platform detection from about page."""
        result = FrontendDetector.detect(host="localhost", path="/about")
        assert result == FrontendType.PLATFORM

    def test_detect_platform_from_api_path(self):
        """Test platform detection from /api/v1/platform path."""
        result = FrontendDetector.detect(host="localhost", path="/api/v1/platform/config")
        assert result == FrontendType.PLATFORM


@pytest.mark.unit
class TestFrontendDetectorPriority:
    """Test suite for detection priority order."""

    def test_admin_subdomain_priority_over_path(self):
        """Test that admin subdomain takes priority."""
        result = FrontendDetector.detect(host="admin.omsflow.lu", path="/storefront/products")
        assert result == FrontendType.ADMIN

    def test_admin_path_priority_over_store_context(self):
        """Test that admin path takes priority over store context."""
        result = FrontendDetector.detect(
            host="localhost", path="/admin/dashboard", has_store_context=True
        )
        assert result == FrontendType.ADMIN

    def test_path_priority_over_subdomain(self):
        """Test that explicit path takes priority for store/storefront."""
        # /store/ path on a store subdomain -> STORE (path wins)
        result = FrontendDetector.detect(host="orion.omsflow.lu", path="/store/settings")
        assert result == FrontendType.STORE


@pytest.mark.unit
class TestFrontendDetectorHelpers:
    """Test suite for helper methods."""

    def test_strip_port(self):
        """Test port stripping from host."""
        assert FrontendDetector._strip_port("localhost:8000") == "localhost"
        assert FrontendDetector._strip_port("omsflow.lu") == "omsflow.lu"
        assert FrontendDetector._strip_port("admin.localhost:9999") == "admin.localhost"

    def test_get_subdomain(self):
        """Test subdomain extraction."""
        assert FrontendDetector._get_subdomain("orion.omsflow.lu") == "orion"
        assert FrontendDetector._get_subdomain("admin.omsflow.lu") == "admin"
        assert FrontendDetector._get_subdomain("omsflow.lu") is None
        assert FrontendDetector._get_subdomain("localhost") is None
        assert FrontendDetector._get_subdomain("127.0.0.1") is None

    def test_is_admin(self):
        """Test is_admin convenience method."""
        assert FrontendDetector.is_admin("admin.omsflow.lu", "/dashboard") is True
        assert FrontendDetector.is_admin("localhost", "/admin/stores") is True
        assert FrontendDetector.is_admin("localhost", "/store/settings") is False

    def test_is_store(self):
        """Test is_store convenience method."""
        assert FrontendDetector.is_store("localhost", "/store/settings") is True
        assert FrontendDetector.is_store("localhost", "/api/v1/store/products") is True
        assert FrontendDetector.is_store("localhost", "/admin/dashboard") is False

    def test_is_storefront(self):
        """Test is_storefront convenience method."""
        assert FrontendDetector.is_storefront("localhost", "/storefront/products") is True
        assert FrontendDetector.is_storefront("orion.omsflow.lu", "/products") is True
        assert FrontendDetector.is_storefront("localhost", "/admin/dashboard") is False

    def test_is_platform(self):
        """Test is_platform convenience method."""
        assert FrontendDetector.is_platform("localhost", "/") is True
        assert FrontendDetector.is_platform("omsflow.lu", "/pricing") is True
        assert FrontendDetector.is_platform("localhost", "/admin/dashboard") is False

    def test_is_api_request(self):
        """Test is_api_request convenience method."""
        assert FrontendDetector.is_api_request("/api/v1/stores") is True
        assert FrontendDetector.is_api_request("/api/v1/admin/users") is True
        assert FrontendDetector.is_api_request("/admin/dashboard") is False


@pytest.mark.unit
class TestReservedSubdomains:
    """Test suite for reserved subdomain handling."""

    def test_www_subdomain_not_storefront(self):
        """Test that www subdomain is not treated as store storefront."""
        result = FrontendDetector.detect(host="www.omsflow.lu", path="/")
        assert result == FrontendType.PLATFORM

    def test_api_subdomain_not_storefront(self):
        """Test that api subdomain is not treated as store storefront."""
        result = FrontendDetector.detect(host="api.omsflow.lu", path="/v1/products")
        assert result == FrontendType.PLATFORM

    def test_portal_subdomain_not_storefront(self):
        """Test that portal subdomain is not treated as store storefront."""
        result = FrontendDetector.detect(host="portal.omsflow.lu", path="/")
        assert result == FrontendType.PLATFORM
