{
  lib,
  config,
  namespace,
  ...
}:
with lib;
let
  inherit (lib.${namespace}) mkOpt;
  name = "tdarr";
  cfg = config.${namespace}.services.${name};

  tdarrConfig = lib.${namespace}.mkModule {
    inherit config name;
    description = "tdarr";
    options = {
      serverPort = mkOpt types.str "8266" "node port";
    };
    moduleConfig = {
      virtualisation.oci-containers.containers.${name} = {
        autoStart = true;
        image = "ghcr.io/haveagitgat/tdarr";
        extraOptions = [ "--device=nvidia.com/gpu=0" ];
        volumes = [
          "${cfg.configDir}/tdarr/config:/app/configs"
          "${cfg.configDir}/tdarr/server:/app/server"
          "${cfg.configDir}/tdarr/logs:/app/logs"
          "${cfg.configDir}/tdarr/transcode:/temp"
          "${cfg.dataDir}/movies:/data/movies"
          "${cfg.dataDir}/tv:/data/tv"
        ];
        ports = [
          "${cfg.serverPort}:8266"
          "${cfg.port}:8265"
        ];
        environment = {
          serverPort = "8266";
          webUIPort = "8265";
          internalNode = "true";
          inContainer = "true";
          ffmpegVersion = "6";
          nodeName = "tdarr node";
          NVIDIA_VISIBLE_DEVICES = "all";
          NVIDIA_DRIVER_CAPABILITIES = "all";
          PUID = cfg.puid;
          PGID = cfg.pgid;
          TZ = cfg.timeZone;
        };
      };
    };
  };
in
{
  imports = [ tdarrConfig ];
}
