from datetime import (
    datetime,
    time,
    timedelta,
)


class MaintenanceWindow(object):
    def __init__(
        self,
        start_time: time,
        end_time: time,
    ):
        self.start_time = start_time
        self.end_time = end_time

    def is_inside(self) -> bool:
        now = datetime.now().time()
        if self.start_time > self.end_time:
            return self.start_time <= now or now <= self.end_time
        else:
            return self.start_time <= now <= self.end_time

    def seconds_to_next(self):
        now = datetime.now()

        today_start = now.replace(
            hour=self.start_time.hour,
            minute=self.start_time.minute,
            second=self.start_time.second,
            microsecond=0,
        )

        if self.start_time > self.end_time:
            if self.is_inside() and now.time() > self.start_time:
                start_to_use = today_start + timedelta(days=1)
            else:
                start_to_use = today_start
        else:
            if self.is_inside():
                start_to_use = today_start + timedelta(days=1)
            elif now <= today_start:
                start_to_use = today_start
            else:
                start_to_use = today_start + timedelta(days=1)

        return (start_to_use - now).total_seconds()
