"""Operator timezone (Asia/Shanghai) helpers."""

from datetime import datetime, timezone

import pytest

from server.utils.display_time import (
    beijing_day_range_utc,
    operator_wall,
    parse_operator_calendar_date,
    to_naive_utc,
)


def test_operator_wall_beijing_0200_is_utc_previous_evening():
    # Beijing 2026-06-08 02:00 = UTC 2026-06-07 18:00
    at = datetime(2026, 6, 7, 18, 0, tzinfo=timezone.utc)
    minute, hour, day, month, dow = operator_wall(at)
    assert (minute, hour, day, month) == (0, 2, 8, 6)


def test_beijing_day_range_utc_covers_local_midnight():
    start, end = beijing_day_range_utc("2026-06-08")
    # Beijing 2026-06-08 00:00 = UTC 2026-06-07 16:00
    assert start == datetime(2026, 6, 7, 16, 0, 0)
    # Beijing 2026-06-08 23:59:59.999999 ≈ UTC 2026-06-08 15:59:59
    assert end.year == 2026 and end.month == 6 and end.day == 8
    assert end.hour == 15 and end.minute == 59


def test_to_naive_utc_strips_tz():
    aware = datetime(2026, 6, 7, 18, 0, tzinfo=timezone.utc)
    assert to_naive_utc(aware) == datetime(2026, 6, 7, 18, 0, 0)


def test_parse_operator_calendar_date_accepts_iso_prefix():
    assert parse_operator_calendar_date("2026-06-08T12:00:00") == "2026-06-08"


@pytest.mark.parametrize("bad", ["", "abc", "2026/06/08", "2026-02-30"])
def test_parse_operator_calendar_date_rejects_invalid(bad: str):
    with pytest.raises(ValueError, match="YYYY-MM-DD"):
        parse_operator_calendar_date(bad)
