"""Extra coverage tests for calculate_duration edge cases in database.py."""

from local_deep_research.web.models.database import calculate_duration


class TestCalculateDurationFormats:
    def test_iso_format_with_timezone(self):
        result = calculate_duration(
            "2025-01-01T00:00:00+00:00",
            "2025-01-01T01:00:00+00:00",
        )
        assert result == 3600

    def test_space_separated_with_microseconds(self):
        """Space-separated + microseconds → parsed, but naive datetime
        may fail .astimezone(); verify we get a result or None gracefully."""
        result = calculate_duration(
            "2025-01-01 00:00:00.000000",
            "2025-01-01 00:01:00.000000",
        )
        assert result is None or isinstance(result, int)

    def test_space_separated_without_microseconds(self):
        result = calculate_duration(
            "2025-01-01 00:00:00",
            "2025-01-01 00:00:30",
        )
        assert result is None or isinstance(result, int)

    def test_space_separated_fallback_replace(self):
        """Space-separated that fails strptime → replaces space with T."""
        result = calculate_duration(
            "2025-01-01 10:00:00+00:00",
            "2025-01-01 11:00:00+00:00",
        )
        assert result == 3600

    def test_none_created_at_returns_none(self):
        assert calculate_duration(None) is None
        assert calculate_duration("") is None

    def test_no_completed_at_uses_now(self):
        result = calculate_duration("2020-01-01T00:00:00+00:00")
        assert result is not None
        assert result > 0

    def test_dateutil_fallback_for_weird_format(self):
        """If standard parsing fails, dateutil.parser is tried."""
        result = calculate_duration(
            "Jan 1, 2025 00:00:00",
            "Jan 1, 2025 01:00:00",
        )
        # dateutil may parse this, or it may return None on tz issues
        assert result is None or isinstance(result, int)

    def test_completely_unparseable_returns_none(self):
        result = calculate_duration("not-a-date", "also-not-a-date")
        assert result is None

    def test_unparseable_created_at_returns_none(self):
        result = calculate_duration("garbage-date", "2025-01-01T00:00:00+00:00")
        assert result is None


class TestCalculateDurationEdgeCases:
    def test_same_timestamps_returns_zero(self):
        ts = "2025-06-15T12:00:00+00:00"
        assert calculate_duration(ts, ts) == 0

    def test_negative_duration(self):
        """completed_at before created_at → negative seconds."""
        result = calculate_duration(
            "2025-01-01T01:00:00+00:00",
            "2025-01-01T00:00:00+00:00",
        )
        assert result == -3600

    def test_mixed_formats(self):
        """created_at is ISO, completed_at is space-separated."""
        result = calculate_duration(
            "2025-01-01T00:00:00+00:00",
            "2025-01-01 01:00:00",
        )
        # Should still calculate correctly
        assert result is not None
