{
  config,
  lib,
  pkgs,
  ...
}:
with lib;
let
  cfg = config.services.bandcamp-collection-downloader;
in
{
  options.services.bandcamp-collection-downloader = {
    enable = mkEnableOption "Bandcamp Collection Downloader";

    bandcampUser = mkOption {
      type = types.str;
    };

    cookiesFile = mkOption {
      type = types.path;
    };

    downloadFolder = mkOption {
      type = types.str;
      default = "/data/bandcamp";
    };

    audioFormat = mkOption {
      type = types.str;
      default = "flac";
    };
  };

  config = mkIf cfg.enable {
    users.users.bandcampcd = {
      enable = true;
      isSystemUser = true;
      group = "bandcampcd";
    };
    users.groups.bandcampcd = { };

    systemd.services."bandcamp-collection-downloader" = {
      description = "Synchronize Bandcamp library";
      requires = [ "network-online.target" ];
      serviceConfig = {
        Type = "oneshot";
        ExecStart = pkgs.writers.writeBash "bandcamp-sync" ''
          ${pkgs.bandcamp-collection-downloader}/bin/bandcamp-collection-downloader ${cfg.bandcampUser} \
            --cookies-file ${cfg.cookiesFile} \
            --download-folder ${cfg.downloadFolder} \
            --audio-format ${cfg.audioFormat}
        '';
      };
    };
  };
}
