from datetime import datetime, timezone

from conftest import load_bin_module

lumen_control = load_bin_module("lumen_control")

MILOS_LAT, MILOS_LON = 36.7, 24.42


def test_midday_is_not_dark():
    now = datetime(2026, 10, 15, 12, 0, tzinfo=timezone.utc)
    assert lumen_control.is_dark(now, MILOS_LAT, MILOS_LON, "civil") is False


def test_evening_after_civil_dusk_is_dark():
    now = datetime(2026, 10, 15, 17, 0, tzinfo=timezone.utc)
    assert lumen_control.is_dark(now, MILOS_LAT, MILOS_LON, "civil") is True


def test_small_hours_before_dawn_are_still_dark():
    # Confirms the "yesterday's dusk -> today's dawn" window is checked,
    # not just "today's dusk -> tomorrow's dawn".
    now = datetime(2026, 10, 16, 3, 0, tzinfo=timezone.utc)
    assert lumen_control.is_dark(now, MILOS_LAT, MILOS_LON, "civil") is True


def test_after_dawn_is_not_dark():
    now = datetime(2026, 10, 16, 5, 0, tzinfo=timezone.utc)
    assert lumen_control.is_dark(now, MILOS_LAT, MILOS_LON, "civil") is False


def test_actual_sunset_definition_triggers_earlier_than_civil_dusk():
    # 16:00 UTC on 2026-10-15 at Milos is after actual sunset (~15:46) but
    # before civil dusk (~16:12).
    now = datetime(2026, 10, 15, 16, 0, tzinfo=timezone.utc)
    assert lumen_control.is_dark(now, MILOS_LAT, MILOS_LON, "actual") is True
    assert lumen_control.is_dark(now, MILOS_LAT, MILOS_LON, "civil") is False


def test_lumen_pwm_sim_mode_is_idempotent(sim_env, capsys):
    from bubblecam.hardware.pwm_sysfs import LumenPWM

    pwm = LumenPWM()
    pwm.set_brightness_pct(30)
    pwm.set_brightness_pct(30)  # should be a no-op, no duplicate print
    pwm.off()

    output_lines = [line for line in capsys.readouterr().out.splitlines() if line.strip()]
    assert len(output_lines) == 2
