"""
Tests for feed statistics tracking

Tests feed statistics aggregation per v1.1.2 Phase 3.
"""

import pytest
from starpunk.monitoring.business import get_feed_statistics, track_feed_generated


def test_get_feed_statistics_returns_structure():
    """Test get_feed_statistics returns expected structure"""
    stats = get_feed_statistics()

    # Check top-level keys
    assert "by_format" in stats
    assert "cache" in stats
    assert "total_requests" in stats
    assert "format_percentages" in stats

    # Check by_format structure
    assert "rss" in stats["by_format"]
    assert "atom" in stats["by_format"]
    assert "json" in stats["by_format"]

    # Check format stats structure
    for format_name in ["rss", "atom", "json"]:
        fmt_stats = stats["by_format"][format_name]
        assert "generated" in fmt_stats
        assert "cached" in fmt_stats
        assert "total" in fmt_stats
        assert "avg_duration_ms" in fmt_stats

    # Check cache structure
    assert "hits" in stats["cache"]
    assert "misses" in stats["cache"]
    assert "hit_rate" in stats["cache"]


def test_get_feed_statistics_empty_metrics():
    """Test get_feed_statistics with no metrics returns zeros"""
    stats = get_feed_statistics()

    # All values should be zero or empty
    assert stats["total_requests"] >= 0
    assert stats["cache"]["hit_rate"] >= 0.0
    assert stats["cache"]["hit_rate"] <= 1.0


def test_feed_statistics_cache_hit_rate_calculation():
    """Test cache hit rate is calculated correctly"""
    stats = get_feed_statistics()

    # Hit rate should be between 0 and 1
    assert 0.0 <= stats["cache"]["hit_rate"] <= 1.0

    # If there are hits and misses, hit rate should be hits / (hits + misses)
    if stats["cache"]["hits"] + stats["cache"]["misses"] > 0:
        expected_rate = stats["cache"]["hits"] / (
            stats["cache"]["hits"] + stats["cache"]["misses"]
        )
        assert abs(stats["cache"]["hit_rate"] - expected_rate) < 0.001


def test_feed_statistics_format_percentages():
    """Test format percentages sum to 1.0 when there are requests"""
    stats = get_feed_statistics()

    if stats["total_requests"] > 0:
        total_percentage = sum(stats["format_percentages"].values())
        # Should sum to approximately 1.0 (allowing for floating point errors)
        assert abs(total_percentage - 1.0) < 0.001


def test_feed_statistics_total_requests_sum():
    """Test total_requests equals sum of all format totals"""
    stats = get_feed_statistics()

    format_total = sum(
        fmt["total"] for fmt in stats["by_format"].values()
    )

    assert stats["total_requests"] == format_total


def test_track_feed_generated_records_metrics():
    """Test track_feed_generated creates metrics entries"""
    # Note: This test just verifies the function runs without error.
    # Actual metrics tracking is tested in integration tests.
    track_feed_generated(
        format="rss",
        item_count=10,
        duration_ms=50.5,
        cached=False
    )

    # Get statistics - may be empty if metrics buffer hasn't persisted yet
    stats = get_feed_statistics()

    # Verify structure is correct
    assert "total_requests" in stats
    assert "by_format" in stats
    assert "cache" in stats
