"""Tests for HIC calculation."""

import numpy as np
import pytest

from impakt.criteria.hic import hic, hic15, hic36


class TestHIC:
    def test_hic15_computation(self, head_group):
        result = hic(head_group, window_ms=15)
        assert result.criterion == "HIC15"
        assert result.value > 0
        assert result.window is not None
        t1, t2 = result.window
        assert 0 <= (t2 - t1) <= 0.015 + 1e-6  # Window <= 15ms

    def test_hic36_computation(self, head_group):
        result = hic(head_group, window_ms=36)
        assert result.criterion == "HIC36"
        assert result.value > 0

    def test_hic36_gte_hic15(self, head_group):
        """HIC36 should always be >= HIC15 (larger search window)."""
        h15 = hic(head_group, window_ms=15)
        h36 = hic(head_group, window_ms=36)
        assert h36.value >= h15.value - 1e-6  # Small tolerance for numerical issues

    def test_hic_from_single_channel(self, head_accel_x):
        # Just X component as a simple test
        result = hic(head_accel_x, window_ms=15)
        assert result.value > 0

    def test_hic_body_region(self, head_group):
        result = hic(head_group, window_ms=15)
        assert result.body_region == "Head"

    def test_hic_details(self, head_group):
        result = hic(head_group, window_ms=15)
        assert "t1" in result.details
        assert "t2" in result.details
        assert "window_ms" in result.details
        assert result.details["window_ms"] == 15

    def test_hic15_convenience(self, head_group):
        result = hic15(head_group)
        assert result.criterion == "HIC15"

    def test_hic36_convenience(self, head_group):
        result = hic36(head_group)
        assert result.criterion == "HIC36"

    def test_invalid_window(self, head_group):
        with pytest.raises(ValueError, match="15 or 36"):
            hic(head_group, window_ms=20)
