"""Daylight colour-temperature schedule for tunable-white fixtures.

Prints the colour temperature and dimming level a tunable-white luminaire
would follow over a day so that indoor light tracks the daylight outside.
"""
import math
from dataclasses import dataclass

WARM_K = 2700
COOL_K = 6500


@dataclass
class Setpoint:
    hour: float
    cct_k: int
    level: float  # 0..1


def sun_elevation(hour: float, day_length: float = 14.0) -> float:
    """Rough solar elevation factor, 0 at sunrise and sunset, 1 at solar noon."""
    sunrise = 12.0 - day_length / 2
    t = (hour - sunrise) / day_length
    return max(0.0, math.sin(math.pi * t)) if 0.0 <= t <= 1.0 else 0.0


def setpoint(hour: float) -> Setpoint:
    e = sun_elevation(hour)
    cct = round(WARM_K + (COOL_K - WARM_K) * e ** 0.6, -1)
    level = 0.15 + 0.85 * e
    return Setpoint(hour, int(cct), round(level, 2))


def schedule(step_minutes: int = 30) -> list[Setpoint]:
    return [setpoint(m / 60) for m in range(0, 24 * 60, step_minutes)]


if __name__ == "__main__":
    print(f"{'time':>5}  {'CCT':>6}  level")
    for sp in schedule(60):
        h = int(sp.hour)
        bar = "#" * int(sp.level * 30)
        print(f"{h:02d}:00  {sp.cct_k:>5}K  {sp.level:4.2f} {bar}")
