# tests/integration/middleware/test_merchant_domain_flow.py
"""
Integration tests for merchant domain resolution end-to-end flow.

Tests verify that merchant domain detection works correctly through real HTTP
requests, including:
- Merchant domain → platform resolved → store resolved
- Store-specific domain overrides merchant domain
- Merchant domain resolves to first active store
- Existing StoreDomain and subdomain routing still work
"""

import uuid
from datetime import UTC, datetime

import pytest

from app.modules.tenancy.models import Store, StoreDomain
from app.modules.tenancy.models.merchant_domain import MerchantDomain


@pytest.mark.integration
@pytest.mark.middleware
class TestMerchantDomainFlow:
    """Test merchant domain resolution through real HTTP requests."""

    def test_merchant_domain_resolves_store(
        self, client, db, middleware_test_merchant, store_with_subdomain
    ):
        """Test merchant domain resolves to merchant's first active store."""
        unique_id = str(uuid.uuid4())[:8]
        md = MerchantDomain(
            merchant_id=middleware_test_merchant.id,
            domain=f"mflow{unique_id}.lu",
            is_primary=True,
            is_active=True,
            is_verified=True,
            verified_at=datetime.now(UTC),
            verification_token=f"mf_{unique_id}",
        )
        db.add(md)
        db.commit()

        response = client.get(
            "/middleware-test/merchant-domain-detection",
            headers={"host": f"mflow{unique_id}.lu"},
        )

        assert response.status_code == 200
        data = response.json()
        assert data["store_detected"] is True
        assert data["merchant_domain"] is True
        assert data["merchant_id"] == middleware_test_merchant.id

    def test_store_domain_overrides_merchant_domain(
        self, client, db, middleware_test_merchant
    ):
        """Test that StoreDomain takes priority over MerchantDomain."""
        unique_id = str(uuid.uuid4())[:8]

        # Create a store with a custom StoreDomain
        store = Store(
            merchant_id=middleware_test_merchant.id,
            name="Override Store",
            store_code=f"OVERRIDE_{unique_id.upper()}",
            subdomain=f"override{unique_id}",
            is_active=True,
            is_verified=True,
        )
        db.add(store)
        db.commit()
        db.refresh(store)

        domain_name = f"storeoverride{unique_id}.lu"
        sd = StoreDomain(
            store_id=store.id,
            domain=domain_name,
            is_primary=True,
            is_active=True,
            is_verified=True,
            verified_at=datetime.now(UTC),
            verification_token=f"so_{unique_id}",
        )
        db.add(sd)
        db.commit()

        response = client.get(
            "/middleware-test/custom-domain",
            headers={"host": domain_name},
        )

        assert response.status_code == 200
        data = response.json()
        if data["store_detected"]:
            assert data["store_code"] == store.store_code

    def test_subdomain_routing_still_works(self, client, store_with_subdomain):
        """Test that subdomain routing still works."""
        response = client.get(
            "/middleware-test/subdomain-detection",
            headers={
                "host": f"{store_with_subdomain.subdomain}.platform.com"
            },
        )

        assert response.status_code == 200
        data = response.json()
        assert data["store_detected"] is True
        assert data["store_code"] == store_with_subdomain.store_code

    def test_inactive_merchant_domain_not_resolved(
        self, client, db, middleware_test_merchant
    ):
        """Test that inactive merchant domain is not resolved."""
        unique_id = str(uuid.uuid4())[:8]
        md = MerchantDomain(
            merchant_id=middleware_test_merchant.id,
            domain=f"inactive{unique_id}.lu",
            is_primary=True,
            is_active=False,
            is_verified=True,
            verified_at=datetime.now(UTC),
            verification_token=f"im_{unique_id}",
        )
        db.add(md)
        db.commit()

        response = client.get(
            "/middleware-test/merchant-domain-detection",
            headers={"host": f"inactive{unique_id}.lu"},
        )

        assert response.status_code == 200
        data = response.json()
        assert data["store_detected"] is False

    def test_unverified_merchant_domain_not_resolved(
        self, client, db, middleware_test_merchant
    ):
        """Test that unverified merchant domain is not resolved."""
        unique_id = str(uuid.uuid4())[:8]
        md = MerchantDomain(
            merchant_id=middleware_test_merchant.id,
            domain=f"unver{unique_id}.lu",
            is_primary=True,
            is_active=True,
            is_verified=False,
            verification_token=f"uv_{unique_id}",
        )
        db.add(md)
        db.commit()

        response = client.get(
            "/middleware-test/merchant-domain-detection",
            headers={"host": f"unver{unique_id}.lu"},
        )

        assert response.status_code == 200
        data = response.json()
        assert data["store_detected"] is False
