# tests/fixtures/merchant_domain_fixtures.py
"""
Merchant domain test fixtures.

Provides fixtures for:
- Merchants with verified merchant domains
- Stores with merchant domain overrides (StoreDomain)
- MerchantDomain objects for testing
"""

import uuid
from datetime import UTC, datetime

import pytest

from app.modules.tenancy.models.merchant_domain import MerchantDomain
from app.modules.tenancy.models.store_domain import StoreDomain


@pytest.fixture
def test_merchant_domain(db, test_merchant):
    """Create an unverified merchant domain."""
    unique_id = str(uuid.uuid4())[:8]
    domain = MerchantDomain(
        merchant_id=test_merchant.id,
        domain=f"merchant{unique_id}.example.com",
        is_primary=True,
        is_active=False,
        is_verified=False,
        verification_token=f"mtoken_{unique_id}",
        ssl_status="pending",
    )
    db.add(domain)
    db.commit()
    db.refresh(domain)
    return domain


@pytest.fixture
def verified_merchant_domain(db, test_merchant):
    """Create a verified and active merchant domain."""
    unique_id = str(uuid.uuid4())[:8]
    domain = MerchantDomain(
        merchant_id=test_merchant.id,
        domain=f"verified-merchant{unique_id}.example.com",
        is_primary=True,
        is_active=True,
        is_verified=True,
        verified_at=datetime.now(UTC),
        verification_token=f"vmtoken_{unique_id}",
        ssl_status="active",
    )
    db.add(domain)
    db.commit()
    db.refresh(domain)
    return domain


@pytest.fixture
def merchant_with_domain(db, test_merchant, test_platform):
    """Create a merchant with a verified merchant domain linked to a platform."""
    unique_id = str(uuid.uuid4())[:8]
    domain = MerchantDomain(
        merchant_id=test_merchant.id,
        platform_id=test_platform.id,
        domain=f"testmerchant{unique_id}.example.com",
        is_primary=True,
        is_active=True,
        is_verified=True,
        verified_at=datetime.now(UTC),
        verification_token=f"mwd_{unique_id}",
        ssl_status="active",
    )
    db.add(domain)
    db.commit()
    db.refresh(domain)
    return test_merchant, domain


@pytest.fixture
def store_with_merchant_domain_override(db, test_store):
    """Store that overrides the merchant domain with its own StoreDomain."""
    unique_id = str(uuid.uuid4())[:8]
    store_domain = StoreDomain(
        store_id=test_store.id,
        domain=f"storeoverride{unique_id}.example.com",
        is_primary=True,
        is_active=True,
        is_verified=True,
        verified_at=datetime.now(UTC),
        verification_token=f"sod_{unique_id}",
        ssl_status="active",
    )
    db.add(store_domain)
    db.commit()
    db.refresh(store_domain)
    return test_store, store_domain
