"""Tests for `cv_compare` package."""

from sklearn.datasets import make_classification, make_regression
from sklearn.tree import DecisionTreeClassifier

from cv_compare import cv_compare


def test_import():
    """Verify the package can be imported."""
    assert cv_compare


def test_classification_default():
    """Test classification task with default arguments."""
    X, y = make_classification(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y, model_task="classification")
    assert result is not None


def test_regression_default():
    """Test regression task with default arguments."""
    X, y = make_regression(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y, model_task="regression")
    assert result is not None


def test_scaler_false():
    """Test that scaler=False runs without error."""
    X, y = make_classification(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y, scaler=False)
    assert result is not None


def test_custom_scoring():
    """Test that a custom scoring metric is accepted."""
    X, y = make_classification(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y, cv_scoring="f1")
    assert result is not None


def test_custom_random_state():
    """Test that a custom random state runs without error."""
    X, y = make_classification(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y, random_state=42)
    assert result is not None


def test_custom_models():
    """Test that a custom models list is accepted."""
    X, y = make_classification(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y, models=[DecisionTreeClassifier()])
    assert result is not None


def test_add_models():
    """Test that add_models appends to the default list."""
    X, y = make_classification(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y, add_models=[DecisionTreeClassifier()])
    assert result is not None


def test_output_attributes():
    """Test that the output object has the expected attributes."""
    X, y = make_classification(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y)
    assert result.names is not None
    assert result.scores is not None
    assert result.summary_table is not None
    assert result.raw_table is not None


def test_summary_table_sorted():
    """Test that summary table is sorted descending by mean score."""
    X, y = make_classification(n_samples=100, n_features=5, random_state=1)
    result = cv_compare(X, y)
    scores = result.summary_table.iloc[:, 1].values
    assert all(scores[i] >= scores[i + 1] for i in range(len(scores) - 1))
