{ pkgs, ... }@all: with all;
{
  systemd.timers."nx_cal_lec" = {
    enable = true;
    wantedBy = [ "timers.target" ];
    timerConfig = {
      OnBootSec = "40m";
      OnUnitActiveSec = "24h";
      Unit = "nx_cal_lec.service";
    };
  };

  systemd.services."nx_cal_lec" = {
    script = let 
      nx_cal_lec = (pkgs.writers.writePython3Bin "nx_cal_lec" {
      libraries = with pkgs.python3Packages; [
        ical
        ics
        requests
        dateutils
      ];
      flakeIgnore = [ "E302" "E305" "E226" "E501" ];
    } /*python */ ''
from ics import Calendar
import requests
from datetime import timedelta

def adjust_events(events):
    """
    Adjust overlapping events to ensure they do not conflict.
    """
    sorted_events = sorted(events, key=lambda e: e.begin)
    for i in range(1, len(sorted_events)):
        previous_event = sorted_events[i - 1]
        current_event = sorted_events[i]

        if current_event.begin < previous_event.end:
            # Adjust the start time of the current event to just after the previous event
            current_event.begin = previous_event.end + timedelta(minutes=1)
            print(f"Adjusted event '{current_event.name}' to start at {current_event.begin} and end at {current_event.end}")
    return sorted_events

def fetch_and_save_ical_events(ical_url, save_path):
    """
    Fetch events from an iCal URL and save them as a single combined calendar.
    """
    try:
        # Fetch the iCal data
        response = requests.get(ical_url)
        response.raise_for_status()

        # Parse the iCal data
        calendar = Calendar(response.text)

        # Adjust events
        adjusted_events = adjust_events(list(calendar.events))

        # Create a new combined calendar
        combined_calendar = Calendar()
        for event in adjusted_events:
            combined_calendar.events.add(event)

        # Save the combined calendar to a single .ics file
        with open(save_path, 'w') as file:
            file.writelines(combined_calendar.serialize_iter())

        print(f"Saved combined calendar to {save_path}")

    except requests.exceptions.RequestException as e:
        print(f"Error fetching iCal data: {e}")
    except Exception as e:
        print(f"Error processing iCal data: {e}")

if __name__ == "__main__":
    # Replace with your iCal URL and target file path
    ICAL_URL = "https://zlypher.github.io/lol-events/cal/league-of-legends-lec.ical"
    SAVE_PATH = "${config.services.nginx.virtualHosts."${hyper.domain}".root}/lec.ics"

    fetch_and_save_ical_events(ICAL_URL, SAVE_PATH)
'');
    in ''
      ${nx_cal_lec}/bin/nx_cal_lec
    '';
    serviceConfig = {
      Type = "oneshot";
      User = hyper.user;
    };
  };
}
