# tests/unit/services/test_store_email_settings_service.py
"""Unit tests for StoreEmailSettingsService."""

from datetime import UTC, datetime
from unittest.mock import MagicMock, patch

import pytest

from app.exceptions import (
    AuthorizationException,
    ResourceNotFoundException,
    ValidationException,
)
from app.modules.billing.models import TierCode
from app.modules.messaging.models import StoreEmailSettings
from app.modules.messaging.services.store_email_settings_service import (
    store_email_settings_service,
)

# =============================================================================
# FIXTURES
# =============================================================================


@pytest.fixture
def test_email_settings(db, test_store):
    """Create test email settings for a store."""
    settings = StoreEmailSettings(
        store_id=test_store.id,
        from_email="test@example.com",
        from_name="Test Sender",
        provider="smtp",
        smtp_host="smtp.example.com",
        smtp_port=587,
        smtp_username="testuser",
        smtp_password="testpass",  # noqa: SEC001
        smtp_use_tls=True,
        smtp_use_ssl=False,
        is_configured=True,
        is_verified=False,
    )
    db.add(settings)
    db.commit()
    db.refresh(settings)
    return settings


@pytest.fixture
def test_verified_email_settings(db, test_store):
    """Create verified email settings."""
    settings = StoreEmailSettings(
        store_id=test_store.id,
        from_email="verified@example.com",
        from_name="Verified Sender",
        provider="smtp",
        smtp_host="smtp.example.com",
        smtp_port=587,
        smtp_username="testuser",
        smtp_password="testpass",  # noqa: SEC001
        smtp_use_tls=True,
        is_configured=True,
        is_verified=True,
        last_verified_at=datetime.now(UTC),
    )
    db.add(settings)
    db.commit()
    db.refresh(settings)
    return settings


# =============================================================================
# READ OPERATION TESTS
# =============================================================================


@pytest.mark.unit
@pytest.mark.email
class TestStoreEmailSettingsRead:
    """Test suite for reading email settings."""

    def test_get_settings_exists(self, db, test_email_settings):
        """Test getting settings when they exist."""
        settings = store_email_settings_service.get_settings(db, test_email_settings.store_id)

        assert settings is not None
        assert settings.from_email == "test@example.com"
        assert settings.provider == "smtp"

    def test_get_settings_not_exists(self, db, test_store):
        """Test getting settings when they don't exist."""
        settings = store_email_settings_service.get_settings(db, test_store.id)

        assert settings is None

    def test_get_settings_or_404_exists(self, db, test_email_settings):
        """Test get_settings_or_404 when settings exist."""
        settings = store_email_settings_service.get_settings_or_404(db, test_email_settings.store_id)

        assert settings is not None
        assert settings.id == test_email_settings.id

    def test_get_settings_or_404_not_exists(self, db, test_store):
        """Test get_settings_or_404 raises exception when not found."""
        with pytest.raises(ResourceNotFoundException) as exc:
            store_email_settings_service.get_settings_or_404(db, test_store.id)

        assert "store_email_settings" in str(exc.value)

    def test_is_configured_true(self, db, test_email_settings):
        """Test is_configured returns True for configured settings."""
        result = store_email_settings_service.is_configured(db, test_email_settings.store_id)

        assert result is True

    def test_is_configured_false_not_exists(self, db, test_store):
        """Test is_configured returns False when settings don't exist."""
        result = store_email_settings_service.is_configured(db, test_store.id)

        assert result is False

    def test_get_status_configured(self, db, test_email_settings):
        """Test get_status for configured settings."""
        status = store_email_settings_service.get_status(db, test_email_settings.store_id)

        assert status["is_configured"] is True
        assert status["is_verified"] is False
        assert status["provider"] == "smtp"
        assert status["from_email"] == "test@example.com"

    def test_get_status_not_configured(self, db, test_store):
        """Test get_status when settings don't exist."""
        status = store_email_settings_service.get_status(db, test_store.id)

        assert status["is_configured"] is False
        assert status["is_verified"] is False
        assert status["provider"] is None


# =============================================================================
# WRITE OPERATION TESTS
# =============================================================================


@pytest.mark.unit
@pytest.mark.email
class TestStoreEmailSettingsWrite:
    """Test suite for writing email settings."""

    def test_create_settings(self, db, test_store):
        """Test creating new email settings."""
        data = {
            "from_email": "new@example.com",
            "from_name": "New Sender",
            "provider": "smtp",
            "smtp_host": "smtp.example.com",
            "smtp_port": 587,
            "smtp_username": "user",
            "smtp_password": "pass",  # noqa: SEC001
        }

        settings = store_email_settings_service.create_or_update(
            db=db,
            store_id=test_store.id,
            data=data,
            current_tier=TierCode.ESSENTIAL,
        )

        assert settings.from_email == "new@example.com"
        assert settings.provider == "smtp"
        assert settings.smtp_host == "smtp.example.com"

    def test_update_existing_settings(self, db, test_email_settings):
        """Test updating existing settings."""
        data = {
            "from_email": "updated@example.com",
            "from_name": "Updated Sender",
        }

        settings = store_email_settings_service.create_or_update(
            db=db,
            store_id=test_email_settings.store_id,
            data=data,
            current_tier=TierCode.ESSENTIAL,
        )

        assert settings.from_email == "updated@example.com"
        assert settings.from_name == "Updated Sender"
        # Other fields should remain unchanged
        assert settings.smtp_host == "smtp.example.com"

    def test_premium_provider_requires_business_tier(self, db, test_store):
        """Test that premium providers require Business tier."""
        data = {
            "from_email": "test@example.com",
            "from_name": "Test",
            "provider": "sendgrid",
            "sendgrid_api_key": "test-key",
        }

        with pytest.raises(AuthorizationException) as exc:
            store_email_settings_service.create_or_update(
                db=db,
                store_id=test_store.id,
                data=data,
                current_tier=TierCode.ESSENTIAL,
            )

        assert "Business or Enterprise" in str(exc.value)

    def test_premium_provider_allowed_for_business(self, db, test_store):
        """Test that premium providers work with Business tier."""
        data = {
            "from_email": "test@example.com",
            "from_name": "Test",
            "provider": "sendgrid",
            "sendgrid_api_key": "test-key",
        }

        settings = store_email_settings_service.create_or_update(
            db=db,
            store_id=test_store.id,
            data=data,
            current_tier=TierCode.BUSINESS,
        )

        assert settings.provider == "sendgrid"

    def test_provider_change_resets_verification(self, db, test_verified_email_settings):
        """Test that changing provider resets verification status."""
        assert test_verified_email_settings.is_verified is True

        data = {"smtp_host": "new-smtp.example.com"}

        settings = store_email_settings_service.create_or_update(
            db=db,
            store_id=test_verified_email_settings.store_id,
            data=data,
            current_tier=TierCode.ESSENTIAL,
        )

        assert settings.is_verified is False

    def test_delete_settings(self, db, test_email_settings):
        """Test deleting email settings."""
        store_id = test_email_settings.store_id

        store_email_settings_service.delete(db, store_id)
        db.commit()

        # Verify deletion
        settings = store_email_settings_service.get_settings(db, store_id)
        assert settings is None

    def test_delete_settings_not_found(self, db, test_store):
        """Test deleting non-existent settings raises exception."""
        with pytest.raises(ResourceNotFoundException):
            store_email_settings_service.delete(db, test_store.id)


# =============================================================================
# VERIFICATION TESTS
# =============================================================================


@pytest.mark.unit
@pytest.mark.email
class TestStoreEmailSettingsVerification:
    """Test suite for email verification."""

    def test_verify_settings_not_configured(self, db, test_store):
        """Test verification fails for non-existent settings."""
        with pytest.raises(ResourceNotFoundException):
            store_email_settings_service.verify_settings(db, test_store.id, "test@example.com")

    def test_verify_settings_incomplete(self, db, test_store):
        """Test verification fails for incomplete settings."""
        # Create incomplete settings
        settings = StoreEmailSettings(
            store_id=test_store.id,
            from_email="test@example.com",
            from_name="Test",
            provider="smtp",
            # Missing SMTP config
            is_configured=False,
        )
        db.add(settings)
        db.commit()

        with pytest.raises(ValidationException) as exc:
            store_email_settings_service.verify_settings(db, test_store.id, "test@example.com")

        assert "incomplete" in str(exc.value).lower()

    @patch("smtplib.SMTP")
    def test_verify_smtp_success(self, mock_smtp, db, test_email_settings):
        """Test successful SMTP verification."""
        # Mock SMTP connection
        mock_server = MagicMock()
        mock_smtp.return_value = mock_server

        result = store_email_settings_service.verify_settings(
            db,
            test_email_settings.store_id,
            "recipient@example.com",
        )

        assert result["success"] is True
        assert "successfully" in result["message"].lower()

    @patch("smtplib.SMTP")
    def test_verify_smtp_failure(self, mock_smtp, db, test_email_settings):
        """Test SMTP verification failure."""
        # Mock SMTP error
        mock_smtp.side_effect = Exception("Connection refused")

        result = store_email_settings_service.verify_settings(
            db,
            test_email_settings.store_id,
            "recipient@example.com",
        )

        assert result["success"] is False
        assert "failed" in result["message"].lower()


# =============================================================================
# PROVIDER AVAILABILITY TESTS
# =============================================================================


@pytest.mark.unit
@pytest.mark.email
class TestStoreEmailProvidersAvailability:
    """Test suite for provider availability checking."""

    def test_get_providers_essential_tier(self, db):
        """Test available providers for Essential tier."""
        providers = store_email_settings_service.get_available_providers(TierCode.ESSENTIAL)

        # Find SMTP provider
        smtp = next((p for p in providers if p["code"] == "smtp"), None)
        assert smtp is not None
        assert smtp["available"] is True

        # Find SendGrid provider
        sendgrid = next((p for p in providers if p["code"] == "sendgrid"), None)
        assert sendgrid is not None
        assert sendgrid["available"] is False

    def test_get_providers_business_tier(self, db):
        """Test available providers for Business tier."""
        providers = store_email_settings_service.get_available_providers(TierCode.BUSINESS)

        # All providers should be available
        for provider in providers:
            assert provider["available"] is True

    def test_get_providers_no_tier(self, db):
        """Test available providers with no subscription."""
        providers = store_email_settings_service.get_available_providers(None)

        # Only SMTP should be available
        smtp = next((p for p in providers if p["code"] == "smtp"), None)
        assert smtp["available"] is True

        sendgrid = next((p for p in providers if p["code"] == "sendgrid"), None)
        assert sendgrid["available"] is False
