{ config, containerCfg, pkgs, lib, builder, name,... }:
let 
  serverCfg = config.syscfg.server;
  version = "latest";
in {
  runtime = {
    paths = [{
      path = "${serverCfg.path.config.path}/handbrake";
      mode = "0755";
    }];

    containers = {
      server = builder.mkContainer {
        authentik = true;
        tmpfs = true;
        subdomain = containerCfg.subdomain;
        subpath = containerCfg.subpath;
        image = "ghcr.io/jlesage/handbrake:${version}";
        port = 5800;
        
        extraEnv = {
          USER_ID = "1000";
          GROUP_ID = "1000";
          AUTOMATED_CONVERSION_PRESET = "Custom/AV1 MKV 1080p30";
          AUTOMATED_CONVERSION_FORMAT = "mkv";
          AUTOMATED_CONVERSION_OUTPUT_SUBDIR = "SAME_AS_SRC";
        };
        
        overrides = {
          volumes = [
            "${serverCfg.path.config.path}/handbrake:/config:rw"
            "${serverCfg.path.dlComplete.path}:/watch:rw"
            "${serverCfg.path.dlConverted.path}:/output:rw"
          ];
         };
      };
    };

    
    setup = {
      trigger = "server";
      script = pkgs.writeShellScript "setup" ''
          mkdir -p ${serverCfg.path.data.path}/handbrake/{watch,output}

      '';
    };
  };
}
