{ lib, pkgs, mediaStackPaths, ... }:
let
  dataDir = mediaStackPaths.dispatcharrDataDir;
in
{
  systemd.tmpfiles.settings."r5500-dispatcharr-data" = {
    "${dataDir}"."d" = {
      mode = "0777";
      user = "root";
      group = "root";
    };
  };

  systemd.services.docker-dispatcharr.preStart = lib.mkBefore ''
    ${pkgs.coreutils}/bin/mkdir -p ${dataDir}
  '';

  virtualisation.oci-containers.containers.dispatcharr = {
    image = "ghcr.io/dispatcharr/dispatcharr:latest";
    ports = [ "9191:9191" ];
    volumes = [
      "${dataDir}:/data"
    ];
    environment = {
      DISPATCHARR_ENV = "aio";
      REDIS_HOST = "localhost";
      CELERY_BROKER_URL = "redis://localhost:6379/0";
      DISPATCHARR_LOG_LEVEL = "info";
      TZ = "America/Moncton";
    };
  };

  networking.firewall.allowedTCPPorts = [ 9191 ];
}
