"""Tests for classifier modules."""
import pytest
from src.classification.ml_classifier import MLClassifier
from src.classification.adaptive_classifier import AdaptiveClassifier
from src.classification.feature_extractor import FeatureExtractor
from src.classification.llm_classifier import LLMClassifier
from src.llm.ollama import OllamaProvider
from src.utils.config import load_config, load_categories
import numpy as np


def test_ml_classifier_init():
    """Test ML classifier initialization."""
    classifier = MLClassifier()
    assert classifier is not None
    assert classifier.is_mock is True  # Should be mock for testing
    assert len(classifier.categories) > 0


def test_ml_classifier_info():
    """Test ML classifier info."""
    classifier = MLClassifier()
    info = classifier.get_info()

    assert 'is_loaded' in info
    assert 'is_mock' in info
    assert 'categories' in info
    assert len(info['categories']) == 12


def test_ml_classifier_predict():
    """Test ML classifier prediction."""
    classifier = MLClassifier()

    # Create dummy feature vector
    features = np.random.rand(50)

    result = classifier.predict(features)

    assert 'category' in result
    assert 'confidence' in result
    assert 'probabilities' in result
    assert 0 <= result['confidence'] <= 1


def test_adaptive_classifier_init(config, categories):
    """Test adaptive classifier initialization."""
    feature_extractor = FeatureExtractor()
    ml_classifier = MLClassifier()
    llm_classifier = None

    classifier = AdaptiveClassifier(
        feature_extractor,
        ml_classifier,
        llm_classifier,
        categories,
        config.dict()
    )

    assert classifier is not None
    assert classifier.feature_extractor is not None
    assert classifier.ml_classifier is not None


def test_adaptive_classifier_hard_rules(sample_email, config, categories):
    """Test hard rule matching in adaptive classifier."""
    from src.email_providers.base import Email

    # Create auth email
    auth_email = Email(
        id='auth-test',
        subject='Verify your account',
        sender='noreply@bank.com',
        body='Your verification code is 123456'
    )

    feature_extractor = FeatureExtractor()
    ml_classifier = MLClassifier()

    classifier = AdaptiveClassifier(
        feature_extractor,
        ml_classifier,
        None,
        categories,
        config.dict()
    )

    result = classifier._try_hard_rules(auth_email)

    assert result is not None
    assert result.category == 'auth'
    assert result.method == 'rule'
    assert result.confidence == 0.99


def test_adaptive_classifier_stats(config, categories):
    """Test adaptive classifier statistics."""
    feature_extractor = FeatureExtractor()
    ml_classifier = MLClassifier()

    classifier = AdaptiveClassifier(
        feature_extractor,
        ml_classifier,
        None,
        categories,
        config.dict()
    )

    stats = classifier.get_stats()

    assert stats.total_emails == 0
    assert stats.rule_matched == 0
    assert stats.ml_classified == 0


def test_llm_classifier_init(config, categories):
    """Test LLM classifier initialization."""
    # Create mock LLM provider
    llm = OllamaProvider()

    classifier = LLMClassifier(llm, categories, config.dict())

    assert classifier is not None
    assert classifier.provider is not None
    assert len(classifier.categories) > 0


def test_llm_classifier_status(config, categories):
    """Test LLM classifier status."""
    llm = OllamaProvider()
    classifier = LLMClassifier(llm, categories, config.dict())

    status = classifier.get_status()

    assert 'llm_available' in status
    assert 'provider' in status
    assert 'categories' in status
    assert status['provider'] == 'ollama'
