"""Tests for export formatting helpers.

These tests verify the formatting functions produce correct output
for user-facing timestamp and datetime displays in exported documents.
"""

from __future__ import annotations

from datetime import UTC, datetime

import pytest

from noteflow.infrastructure.export._formatting import format_datetime, format_timestamp


class TestFormatTimestamp:
    """Tests for format_timestamp display formatting.

    Verifies MM:SS format under an hour, HH:MM:SS format for longer durations.
    """

    @pytest.mark.parametrize(
        "seconds,expected",
        [
            pytest.param(0, "0:00", id="zero_seconds"),
            pytest.param(1, "0:01", id="one_second"),
            pytest.param(9, "0:09", id="single_digit_seconds"),
            pytest.param(59, "0:59", id="max_seconds_before_minute"),
            pytest.param(60, "1:00", id="exactly_one_minute"),
            pytest.param(61, "1:01", id="one_minute_one_second"),
            pytest.param(125, "2:05", id="two_minutes_five_seconds"),
            pytest.param(599, "9:59", id="max_under_ten_minutes"),
            pytest.param(600, "10:00", id="exactly_ten_minutes"),
            pytest.param(3599, "59:59", id="max_under_one_hour"),
        ],
    )
    def test_format_timestamp_under_hour(self, seconds: int, expected: str) -> None:
        """Format timestamp under an hour as MM:SS."""
        assert format_timestamp(seconds) == expected, f"Expected {seconds}s to format as {expected}"

    @pytest.mark.parametrize(
        "seconds,expected",
        [
            pytest.param(3600, "1:00:00", id="exactly_one_hour"),
            pytest.param(3601, "1:00:01", id="one_hour_one_second"),
            pytest.param(3661, "1:01:01", id="one_hour_one_minute_one_second"),
            pytest.param(7200, "2:00:00", id="exactly_two_hours"),
            pytest.param(36000, "10:00:00", id="ten_hours"),
            pytest.param(86399, "23:59:59", id="max_under_24_hours"),
        ],
    )
    def test_format_timestamp_over_hour(self, seconds: int, expected: str) -> None:
        """Format timestamp over an hour as HH:MM:SS."""
        assert format_timestamp(seconds) == expected, f"Expected {seconds}s to format as {expected}"

    def test_format_timestamp_handles_float_truncation(self) -> None:
        """Format truncates fractional seconds (floor behavior)."""
        fractional_seconds = 61.9  # 1 minute + 1.9 seconds -> truncates to 1:01
        assert format_timestamp(fractional_seconds) == "1:01", "Fractional seconds should truncate to floor"


class TestFormatDatetime:
    """Tests for format_datetime display formatting.

    Verifies ISO-like human-readable format for datetime values.
    """

    def test_format_datetime_none_returns_empty_string(self) -> None:
        """None datetime returns empty string for optional fields."""
        assert format_datetime(None) == "", "None should return empty string"

    def test_format_datetime_naive_datetime(self) -> None:
        """Format naive datetime without timezone."""
        dt = datetime(2024, 1, 1, 12, 30, 15)
        assert format_datetime(dt) == "2024-01-01 12:30:15", "Naive datetime should format as YYYY-MM-DD HH:MM:SS"

    def test_format_datetime_utc_datetime(self) -> None:
        """Format UTC-aware datetime (ignores timezone in display)."""
        dt = datetime(2024, 6, 15, 9, 45, 30, tzinfo=UTC)
        assert format_datetime(dt) == "2024-06-15 09:45:30", "UTC datetime should format without timezone suffix"

    def test_format_datetime_midnight(self) -> None:
        """Format midnight timestamp correctly."""
        dt = datetime(2024, 12, 31, 0, 0, 0)
        assert format_datetime(dt) == "2024-12-31 00:00:00", "Midnight should format with 00:00:00 time"

    def test_format_datetime_end_of_day(self) -> None:
        """Format end-of-day timestamp correctly."""
        dt = datetime(2024, 12, 31, 23, 59, 59)
        assert format_datetime(dt) == "2024-12-31 23:59:59", "End of day should format with 23:59:59 time"
