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

    containers = {
      server = builder.mkContainer {
        authentik = true;
        subdomain = containerCfg.subdomain;
        subpath = containerCfg.subpath;
        image = "ghcr.io/calibrain/shelfmark:${version}";
        port = 8080;
        
        extraEnv = {
          # HARDCOVER_API_KEY = ""; #FROM SOPS
          # AA_DONATOR_KEY = ""; #FROM SOPS
          # PROWLARR_API_KEY = ""; #FROM SOPS

          FLASK_PORT = "8080";
          PUID = "1000";
          PGID = "1000";
          USING_TOR = "false";
          ONBOARDING = "false";
          SUPPORTED_FORMATS = "epub,mobi,azw3,fb2,djvu,cbz,cbr,pdf";
          SUPPORTED_AUDIOBOOK_FORMATS = "mp3, m4b";
          BOOK_LANGUAGE = "en,fr"; # ,de,jp";
          SEARCH_MODE = "universal";
          AA_DEFAULT_SORT = "relevance";
          METADATA_PROVIDER = "openlibrary";
          INGEST_DIR = "/books";
          BOOKS_OUTPUT_MODE = "/output";
          FILE_ORGANIZATION = "organize";
          TEMPLATE_RENAME = "{Author} - {Title} ({Year})";
          TEMPLATE_ORGANIZE = "{Author}/{Title} ({Year})";
          HARDLINK_TORRENTS = "false";
          FILE_ORGANIZATION_AUDIOBOOK = "organize";
          TEMPLATE_RENAME_AUDIOBOOK = "{Author} - {Title}";
          TEMPLATE_ORGANIZE_AUDIOBOOK = "{Author}/{Title} ({Year})";

          HARDCOVER_ENABLED = "true";
          HARDCOVER_DEFAULT_SORT = "relevance";
          OPENLIBRARY_ENABLED = "true";
          OPENLIBRARY_DEFAULT_SORT = "relevance";
          DIRECT_DOWNLOAD_ENABLED = "true";

          USE_CF_BYPASS = "true";
          AA_BASE_URL = "auto";
          AA_MIRROR_URLS = "https://annas-archive.gl,https://annas-archive.pk,https://annas-archive.gd,";
          LIBGEN_MIRROR_URLS = "https://libgen.li,https://libgen.vg,https://libgen.la,https://libgen.bz,https://libgen.gl";
          ZLIB_MIRROR_URLS = "https://z-lib.sk,https://z-library.gs,https://z-lib.fm,https://z-lib.gd,https://z-lib.gl";
          # WELIB_MIRROR_URLS = "https://welib.org"; #avoid
        } // lib.optionalAttrs(containerCfg.subpath != null) {
          BASE_PATH = "/${containerCfg.subpath}";
          URL_BASE = "/${containerCfg.subpath}";
        } // lib.optionalAttrs(serverCfg.containers?calibre) {
          CALIBRE_WEB_URL = "https://${serverCfg.containers.calibre.subdomain}.${serverCfg.domain}";
        } // lib.optionalAttrs(serverCfg.containers?authentik) {
          AUTH_METHOD = "proxy";
          PROXY_AUTH_USER_HEADER = "X-authentik-username";
          PROXY_AUTH_ADMIN_GROUP_HEADER = "X-authentik-groups";
          PROXY_AUTH_ADMIN_GROUP_NAME = "admin";
        } // lib.optionalAttrs(serverCfg.containers?servarr && builtins.elem "prowlarr" serverCfg.containers.servarr.extra.modules) ({
          PROWLARR_ENABLED = "true";
          PROWLARR_URL = "http://servarr-prowlarr:8989";
        } // lib.optionalAttrs(serverCfg.containers?transmission) {
          PROWLARR_TORRENT_CLIENT = "transmission";
          TRANSMISSION_URL = "http://transmission-server:9091";
        }) // lib.optionalAttrs(serverCfg.containers?servarr && builtins.elem "flaresolverr" serverCfg.containers.servarr.extra.modules) {
          USING_EXTERNAL_BYPASSER = "true";
          EXT_BYPASSER_URL = "http://servarr-flaresolverr:8191";
          EXT_BYPASSER_PATH = "/v1";
          EXT_BYPASSER_TIMEOUT = "60000";
        };
        overrides = {
          volumes = [
            "${serverCfg.path.dlComplete.path}:/books:rw"
            "${serverCfg.path.book.path}:/output:rw"
            "${serverCfg.path.config.path}/selfmark:/config:rw"
           ];
         };
      };
    };
  };
}
