# Sources for this configuration:
# - https://github.com/librespot-org/librespot
# - https://github.com/librespot-org/librespot/wiki/Options
# - https://github.com/librespot-org/librespot/wiki/Audio-Backends

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

with lib;

let

  cfg = config.ccchh.services.audio;

in

{
  config = mkIf cfg.enable {
    systemd.services.spotify-connect-receiver = {
      enable = true;
      description = "Spotify Connect Receiver Using librespot";
      unitConfig = {
        Requires = [ "network-online.target" "pipewire.service" ];
        After = [ "network-online.target" "pipewire.service" ];
      };
      serviceConfig = {
        ExecStart = "${pkgs.librespot}/bin/librespot --name '${config.ccchh.services.audio.name}' --device-type speaker --bitrate 320 --enable-volume-normalisation --disable-audio-cache --disable-credential-cache";
        User = "librespot";
        Group = "librespot";
      };
      wantedBy = [ "multi-user.target" ];
    };

    users = {
      users.librespot = {
        isSystemUser = true;
        group = "librespot";
        extraGroups = [ "pipewire" "audio" ];
      };
      groups.librespot = { };
    };
  };
}