# Auto-generated using compose2nix v0.3.1.
{
  config,
  lib,
  pkgs,
  ...
}:
let
  # Only available in the Komodo LXC
  DockerStorage = "/mnt/DockerStorage/komodo";
  env = config.secretsSpec.docker.komodo;
in
{
  # Containers
  virtualisation.oci-containers.containers."komodo-core" = {
    image = "ghcr.io/moghtech/komodo-core:latest";
    environment = env;
    volumes = [
      "${DockerStorage}/cache:/repo-cache:rw"
    ];
    ports = [
      "9120:9120/tcp"
    ];
    labels = {
      "komodo.skip" = "";
    };
    dependsOn = [
      "komodo-mongo"
    ];
    log-driver = "local";
    extraOptions = [
      "--network-alias=core"
      "--network=komodo_default"
      "--pull=always"
    ];
  };

  systemd.services."docker-komodo-core" = {
    serviceConfig = {
      Restart = lib.mkOverride 90 "always";
      RestartMaxDelaySec = lib.mkOverride 90 "1m";
      RestartSec = lib.mkOverride 90 "100ms";
      RestartSteps = lib.mkOverride 90 9;
    };
    after = [
      "docker-network-komodo_default.service"
      # "docker-volume-komodo_repo-cache.service"
    ];
    requires = [
      "docker-network-komodo_default.service"
      # "docker-volume-komodo_repo-cache.service"
    ];
    partOf = [
      "docker-compose-komodo-root.target"
    ];
    wantedBy = [
      "docker-compose-komodo-root.target"
    ];
  };

  virtualisation.oci-containers.containers."komodo-mongo" = {
    image = "mongo";
    environment = env;
    volumes = [
      "${DockerStorage}/mongo/config:/data/configdb:rw"
      "${DockerStorage}/mongo/data:/data/db:rw"
    ];
    cmd = [
      "--quiet"
      "--wiredTigerCacheSizeGB"
      "0.25"
    ];
    labels = {
      "komodo.skip" = "";
    };
    log-driver = "local";
    extraOptions = [
      "--network-alias=mongo"
      "--network=komodo_default"
    ];
  };

  systemd.services."docker-komodo-mongo" = {
    serviceConfig = {
      Restart = lib.mkOverride 90 "always";
      RestartMaxDelaySec = lib.mkOverride 90 "1m";
      RestartSec = lib.mkOverride 90 "100ms";
      RestartSteps = lib.mkOverride 90 9;
    };
    after = [
      "docker-network-komodo_default.service"
      # "docker-volume-komodo_mongo-config.service"
      # "docker-volume-komodo_mongo-data.service"
    ];
    requires = [
      "docker-network-komodo_default.service"
      # "docker-volume-komodo_mongo-config.service"
      # "docker-volume-komodo_mongo-data.service"
    ];
    partOf = [
      "docker-compose-komodo-root.target"
    ];
    wantedBy = [
      "docker-compose-komodo-root.target"
    ];
  };

  virtualisation.oci-containers.containers."komodo-periphery" = {
    image = "ghcr.io/moghtech/komodo-periphery:latest";
    environment = env;
    volumes = [
      "/proc:/proc:rw"
      "/var/run/docker.sock:/var/run/docker.sock:rw"
      "${DockerStorage}/repos:/etc/komodo/repos:rw"
      "${DockerStorage}/ssl:/etc/komodo/ssl:rw"
      "${DockerStorage}/stacks:${DockerStorage}/stacks:rw"
    ];
    labels = {
      "komodo.skip" = "";
    };
    log-driver = "local";
    extraOptions = [
      "--network-alias=periphery"
      "--network=komodo_default"
      "--pull=always"
    ];
  };

  systemd.services."docker-komodo-periphery" = {
    serviceConfig = {
      Restart = lib.mkOverride 90 "always";
      RestartMaxDelaySec = lib.mkOverride 90 "1m";
      RestartSec = lib.mkOverride 90 "100ms";
      RestartSteps = lib.mkOverride 90 9;
    };
    after = [
      "docker-network-komodo_default.service"
      # "docker-volume-komodo_repos.service"
      # "docker-volume-komodo_ssl-certs.service"
      # "docker-volume-komodo_stacks.service"
    ];
    requires = [
      "docker-network-komodo_default.service"
      # "docker-volume-komodo_repos.service"
      # "docker-volume-komodo_ssl-certs.service"
      # "docker-volume-komodo_stacks.service"
    ];
    partOf = [
      "docker-compose-komodo-root.target"
    ];
    wantedBy = [
      "docker-compose-komodo-root.target"
    ];
  };

  # Networks
  systemd.services."docker-network-komodo_default" = {
    path = [ pkgs.docker ];
    serviceConfig = {
      Type = "oneshot";
      RemainAfterExit = true;
      ExecStop = "docker network rm -f komodo_default";
    };
    script = ''
      docker network inspect komodo_default || docker network create komodo_default
    '';
    partOf = [ "docker-compose-komodo-root.target" ];
    wantedBy = [ "docker-compose-komodo-root.target" ];
  };

  # Root service
  # When started, this will automatically create all resources and start
  # the containers. When stopped, this will teardown all resources.
  systemd.targets."docker-compose-komodo-root" = {
    unitConfig = {
      Description = "Root target generated by compose2nix.";
    };
    after = [
      "docker-authentik-worker.service"
    ];
    requires = [
      "docker-authentik-worker.service"
    ];
    wantedBy = [ "multi-user.target" ];
  };
}
