{ config, lib, pkgs, ... }:

# based on https://github.com/foo-dogsquared/nixos-config/blob/4dade2fb8e0258bab02e4a2bc9cadaa4001c88b1/modules/nixos/services/yt-dlp.nix#L127

let
  jobs = {
    "/mnt/meconium/yt/pbsds/amvs"                = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoFmeytJBvphrxtqn8rNIx4v" ];
    "/mnt/meconium/yt/pbsds/bangers"             = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoHQXxj8tHMTWtnlfpLaZnog" ];
    #"/mnt/meconium/yt/pbsds/bill-wurtz"          = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoHwkPuKqHR_WS8BhG6V8KoH" ];
    #"/mnt/meconium/yt/pbsds/cg-showcases"        = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoHQ7Qjw_oOVFrGcqeWer7xs" ];
    #"/mnt/meconium/yt/pbsds/concerts"            = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoFSWbkwRPQRKnRqy6oM05iS" ];
    #"/mnt/meconium/yt/pbsds/demo-music-videos"   = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoFvMNwtazgcU29Hu93qpw3G" ];
    #"/mnt/meconium/yt/pbsds/demos"               = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoEnOTjY_BKwe6nh7a1iVNTh" ];
    #"/mnt/meconium/yt/pbsds/favorites"           = [ "https://www.youtube.com/playlist?list=FLih25FQ7Ws1y6kQM1W6OJ_A" ];
    #"/mnt/meconium/yt/pbsds/groovy"              = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoHLvqj7mDT0YFn00aFXWUh9" ];
    #"/mnt/meconium/yt/pbsds/hymnos"              = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoE-nakahu9zVcai5Z-5aggK" ];
    #"/mnt/meconium/yt/pbsds/interesting-music"   = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoEy7xwDuZhMiR0baLOcfxmS" ];
    #"/mnt/meconium/yt/pbsds/interesting-video"   = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoFlzOVX7-oCjmuZI8OTiQx4" ];
    #"/mnt/meconium/yt/pbsds/its-not-a-phase-mom" = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoHr9YrqqlLgMSCKUdqfKnvm" ];
    #"/mnt/meconium/yt/pbsds/jazz-arrange"        = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoGgtZItKNB2_COfdVxZnSbU" ];
    #"/mnt/meconium/yt/pbsds/jazz-sessions"       = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoHlqk9s00QC4YtSzvRn4mZL" ];
    #"/mnt/meconium/yt/pbsds/k-jam"               = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoHGWjqnEWurFy-P6Zyb6yYT" ];
    #"/mnt/meconium/yt/pbsds/line-rider"          = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoG39Sl23lVe-XK02_pq4ffx" ];
    #"/mnt/meconium/yt/pbsds/mashups"             = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoFop0qEkXOFJmyv9I0DtkAA" ];
    #"/mnt/meconium/yt/pbsds/music-videos"        = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoF_LLhQLgzesteRHCkMRvZb" ];
    #"/mnt/meconium/yt/pbsds/piano"               = [ "https://www.youtube.com/playlist?list=PL3702F75BB73D3261" ];
    #"/mnt/meconium/yt/pbsds/sakuga-mad"          = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoGO6m68cIPJuy-dRWSOsw0b" ];
    #"/mnt/meconium/yt/pbsds/talks"               = [ "https://www.youtube.com/playlist?list=PLRtYFti2fRoGzsQrJMhg3aG2XqrV20WR5" ];
    #"/mnt/meconium/yt/pbsds/vocaloid1"           = [ "https://www.youtube.com/playlist?list=PL788896D79B90CFFA" ];
    #"/mnt/meconium/yt/pbsds/vocaloid2"           = [ "https://www.youtube.com/playlist?list=PL591C53ADE1FEC8FA" ];
  };
  args = [
    "--verbose"
    #"--concurrent-fragments" "2"
    #"--retries" "20"
  ];

in
{
  systemd.services = lib.flip lib.mapAttrs' jobs (dest: urls:
    lib.nameValuePair "yt-dlp-archive-service-${dest}" {
      enable = true;
      wantedBy = [ "multi-user.target" ];
      description = "yt-dlp archive job for group '${dest}'";
      documentation = [ "man:yt-dlp(1)" ];
      script = ''
        mkdir -vp ${lib.escapeShellArg dest}
        ${lib.getExe pkgs.yt-dlp} \
          --download-archive ${lib.escapeShellArg dest}/yt-dlp-download-db.txt \
          ${lib.escapeShellArgs args} \
          ${lib.escapeShellArgs urls} \
          --paths ${lib.escapeShellArg dest}
      '';
      startAt = "weekly";
      # TODO: better choise of user
      serviceConfig.User = "pbsds";
      serviceConfig.Group = "users";
      serviceConfig.LockPersonality = true;
      serviceConfig.NoNewPrivileges = true;
      serviceConfig.PrivateTmp = true;
      serviceConfig.PrivateUsers = true;
      serviceConfig.PrivateDevices = true;
      serviceConfig.ProtectControlGroups = true;
      serviceConfig.ProtectClock = true;
      serviceConfig.ProtectKernelLogs = true;
      serviceConfig.ProtectKernelModules = true;
      serviceConfig.ProtectKernelTunables = true;
      serviceConfig.StandardOutput = "journal";
      serviceConfig.StandardError = "journal";
      serviceConfig.SystemCallFilter = "@system-service";
      serviceConfig.SystemCallErrorNumber = "EPERM";
    }
  );

  systemd.timers = lib.flip lib.mapAttrs' jobs (dest: urls:
    lib.nameValuePair "yt-dlp-archive-service-${dest}" {
      timerConfig.Persistent = true;
      timerConfig.RandomizedDelaySec = "8min";
    }
  );
}
