from pathlib import Path
from hyprsunset_qt.config import Profile, parse_config, serialize, day_index, night_index, read_config, write_config, CONFIG_PATH


def test_parse_two_profiles():
    text = """
profile {
    time = 5:00
    identity = true
}

profile {
    time = 19:40
    temperature = 5500
    gamma = 0.8
}
"""
    profiles = parse_config(text)
    assert profiles == [
        Profile(time="5:00", identity=True, temperature=None, gamma=None),
        Profile(time="19:40", identity=False, temperature=5500, gamma=0.8),
    ]


def test_parse_empty():
    assert parse_config("") == []


def test_serialize_roundtrip():
    profiles = [
        Profile(time="5:00", identity=True),
        Profile(time="19:40", temperature=5500, gamma=0.8),
    ]
    text = serialize(profiles)
    assert parse_config(text) == profiles
    assert "identity = true" in text
    assert "temperature = 5500" in text
    assert "gamma = 0.8" in text
    assert text.startswith("# Managed by hyprsunset-qt")


def test_day_night_index():
    profiles = [
        Profile(time="5:00", identity=True),
        Profile(time="19:40", temperature=5500, gamma=0.8),
    ]
    assert day_index(profiles) == 0
    assert night_index(profiles) == 1


def test_day_night_index_missing():
    assert day_index([Profile(time="1:00", temperature=4000)]) is None
    assert night_index([Profile(time="1:00", identity=True)]) is None


def test_read_missing_returns_empty(tmp_path):
    assert read_config(tmp_path / "nope.conf") == []


def test_write_then_read(tmp_path):
    path = tmp_path / "hyprsunset.conf"
    profiles = [Profile(time="19:40", temperature=5500, gamma=0.8)]
    write_config(profiles, path)
    assert read_config(path) == profiles


def test_default_path_is_hypr_conf():
    assert str(CONFIG_PATH).endswith(".config/hypr/hyprsunset.conf")


from hyprsunset_qt.config import valid_time, valid_temperature, valid_gamma


def test_valid_time():
    assert valid_time("19:40")
    assert valid_time("5:00")
    assert not valid_time("25:00")
    assert not valid_time("19:60")
    assert not valid_time("abc")


def test_valid_temperature():
    assert valid_temperature(5500)
    assert not valid_temperature(500)
    assert not valid_temperature(30000)


def test_valid_gamma():
    assert valid_gamma(0.8)
    assert valid_gamma(0.0)
    assert not valid_gamma(-1.0)
    assert not valid_gamma(2.5)
