"""Tests for Issue #77: Duplicate row detection and deduplication.

These tests verify the count_duplicates() and deduplicate_bars() methods
for the ReplacingMergeTree deduplication behavior.
"""

from __future__ import annotations

import pytest


def _is_clickhouse_available() -> bool:
    """Check if ClickHouse server is running and accessible."""
    try:
        from rangebar.clickhouse import RangeBarCache

        with RangeBarCache() as cache:
            cache.count_bars("BTCUSDT", 250)
    except (ImportError, OSError, ConnectionError, RuntimeError):
        return False
    else:
        return True


class TestDuplicateDetection:
    """Test duplicate detection methods."""

    @pytest.fixture(autouse=True)
    def _require_clickhouse(self) -> None:
        if not _is_clickhouse_available():
            pytest.skip("ClickHouse not available")

    def test_count_duplicates_returns_list(self) -> None:
        """count_duplicates returns a list (possibly empty)."""
        from rangebar import RangeBarCache

        with RangeBarCache() as cache:
            result = cache.count_duplicates("BTCUSDT", 1000)
            assert isinstance(result, list)

    def test_count_duplicates_all_symbols(self) -> None:
        """count_duplicates works without symbol filter."""
        from rangebar import RangeBarCache

        with RangeBarCache() as cache:
            result = cache.count_duplicates()
            assert isinstance(result, list)

    def test_deduplicate_bars_runs_without_error(self) -> None:
        """deduplicate_bars completes without error."""
        from rangebar import RangeBarCache

        with RangeBarCache() as cache:
            # Should not raise even if no duplicates exist
            cache.deduplicate_bars("BTCUSDT", 1000)


class TestDuplicateResultFormat:
    """Test the format of duplicate detection results."""

    def test_duplicate_result_keys(self) -> None:
        """Verify expected keys in duplicate result dict."""
        # This tests the structure without requiring ClickHouse
        expected_keys = {
            "symbol",
            "threshold_decimal_bps",
            "timestamp_ms",
            "duplicate_count",
            "opens",
            "closes",
        }

        # Simulate a result dict
        sample_result = {
            "symbol": "BTCUSDT",
            "threshold_decimal_bps": 1000,
            "timestamp_ms": 1672801643907,
            "duplicate_count": 2,
            "opens": [16675.65, 16675.65],
            "closes": [16842.42, 16842.42],
        }

        assert set(sample_result.keys()) == expected_keys
