# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/dt/sun.ipynb.

# %% auto 0
__all__ = ['SunTimes', 'get_suntimes_for_day']

# %% ../../nbs/dt/sun.ipynb 3
import datetime
from dataclasses import dataclass
from typing import Optional

import pandas as pd
import suntime
from loguru import logger

import enilm.etypes
from ..datasets.coords import get_lat_lon
from .nilmtktf import get_tzinfo_from_ds

# %% ../../nbs/dt/sun.ipynb 21
@dataclass
class SunTimes:
    sunrise: datetime.datetime
    sunset: datetime.datetime


def get_suntimes_for_day(
    day_date: datetime.datetime | pd.Timestamp,
    ds: enilm.etypes.Datasets,
) -> Optional[SunTimes]:
    if isinstance(day_date, pd.Timestamp):
        day_date = day_date.to_pydatetime()

    # to tz
    tz = get_tzinfo_from_ds(ds)
    day_date = day_date.astimezone(tz)

    # sun at lat/lon
    lat_lon = get_lat_lon(ds)
    if lat_lon is None:
        logger.warning(f"Can't get lat_lon for {ds}")
        return None
    lat, lon = lat_lon
    sun = suntime.Sun(lat, lon)

    return SunTimes(
        sunrise=sun.get_sunrise_time(day_date).astimezone(tz),
        sunset=sun.get_sunset_time(day_date + datetime.timedelta(days=1)).astimezone(tz),
    )
