# Auto-generated by compose2nix.
{
  pkgs,
  lib,
  config,
  ...
}: let
  cfg = config.mods.server.seedbox;
  port = 3013;
  bport = 3014;
in
  with lib; {
    options.mods.server.seedbox = {
      enable = mkEnableOption {
        default = false;
        description = "enables seedbox-lite containers";
      };
    };

    config = mkIf cfg.enable {
      networking.firewall = {
        allowedTCPPorts = [port bport];
      };

      # Runtime
      virtualisation.docker = {
        enable = true;
        autoPrune.enable = true;
      };
      virtualisation.oci-containers.backend = "docker";

      # Containers
      virtualisation.oci-containers.containers."seedbox-backend" = {
        image = "compose2nix/seedbox-backend";
        volumes = [
          "seedbox-lite_seedbox_cache:/app/cache:rw"
          "seedbox-lite_seedbox_data:/app/data:rw"
        ];
        ports = [
          "${toString bport}:3000/tcp"
        ];
        log-driver = "journald";
        extraOptions = [
          "--network-alias=seedbox-backend"
          "--network=seedbox-lite_seedbox-network"
        ];
        environment = {
          NODE_ENV = "production";
          ACCESS_PASSWORD = "temp_pass";
          FRONTEND_URL = "http://localhost:${toString port}";
        };
      };
      systemd.services."docker-seedbox-backend" = {
        serviceConfig = {
          Restart = lib.mkOverride 90 "always";
          RestartMaxDelaySec = lib.mkOverride 90 "1m";
          RestartSec = lib.mkOverride 90 "100ms";
          RestartSteps = lib.mkOverride 90 9;
        };
        after = [
          "docker-network-seedbox-lite_seedbox-network.service"
          "docker-volume-seedbox-lite_seedbox_cache.service"
          "docker-volume-seedbox-lite_seedbox_data.service"
        ];
        requires = [
          "docker-network-seedbox-lite_seedbox-network.service"
          "docker-volume-seedbox-lite_seedbox_cache.service"
          "docker-volume-seedbox-lite_seedbox_data.service"
        ];
        partOf = [
          "docker-compose-seedbox-lite-root.target"
        ];
        wantedBy = [
          "docker-compose-seedbox-lite-root.target"
        ];
      };
      virtualisation.oci-containers.containers."seedbox-frontend" = {
        image = "compose2nix/seedbox-frontend";
        ports = [
          "${toString port}:8080/tcp"
        ];
        dependsOn = [
          "seedbox-backend"
        ];
        log-driver = "journald";
        extraOptions = [
          "--network-alias=seedbox-frontend"
          "--network=seedbox-lite_seedbox-network"
        ];
        environment = {
          NODE_ENV = "production";
          ACCESS_PASSWORD = "temp_pass";
        };
      };
      systemd.services."docker-seedbox-frontend" = {
        serviceConfig = {
          Restart = lib.mkOverride 90 "always";
          RestartMaxDelaySec = lib.mkOverride 90 "1m";
          RestartSec = lib.mkOverride 90 "100ms";
          RestartSteps = lib.mkOverride 90 9;
        };
        after = [
          "docker-network-seedbox-lite_seedbox-network.service"
        ];
        requires = [
          "docker-network-seedbox-lite_seedbox-network.service"
        ];
        partOf = [
          "docker-compose-seedbox-lite-root.target"
        ];
        wantedBy = [
          "docker-compose-seedbox-lite-root.target"
        ];
      };

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

      # Volumes
      systemd.services."docker-volume-seedbox-lite_seedbox_cache" = {
        path = [pkgs.docker];
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
        };
        script = ''
          docker volume inspect seedbox-lite_seedbox_cache || docker volume create seedbox-lite_seedbox_cache --driver=local
        '';
        partOf = ["docker-compose-seedbox-lite-root.target"];
        wantedBy = ["docker-compose-seedbox-lite-root.target"];
      };
      systemd.services."docker-volume-seedbox-lite_seedbox_data" = {
        path = [pkgs.docker];
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
        };
        script = ''
          docker volume inspect seedbox-lite_seedbox_data || docker volume create seedbox-lite_seedbox_data --driver=local
        '';
        partOf = ["docker-compose-seedbox-lite-root.target"];
        wantedBy = ["docker-compose-seedbox-lite-root.target"];
      };

      # Builds
      systemd.services."docker-build-seedbox-backend" = {
        path = [pkgs.docker pkgs.git];
        serviceConfig = {
          Type = "oneshot";
          TimeoutSec = 300;
        };
        script = ''
          cd /tmp
          git clone https://github.com/hotheadhacker/seedbox-lite.git && cd seedbox-lite/server || cd seedbox-lite/server
          podman build -t compose2nix/seedbox-backend .
        '';
      };
      systemd.services."docker-build-seedbox-frontend" = {
        path = [pkgs.docker pkgs.git];
        serviceConfig = {
          Type = "oneshot";
          TimeoutSec = 300;
        };
        script = ''
          cd /tmp
          git clone https://github.com/hotheadhacker/seedbox-lite.git && cd seedbox-lite/client || cd seedbox-lite/client
          podman build -t compose2nix/seedbox-frontend --build-arg VITE_API_BASE_URL=http://localhost:${toString bport} .
        '';
      };

      # 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-seedbox-lite-root" = {
        unitConfig = {
          Description = "Root target generated by compose2nix.";
        };
        wantedBy = ["multi-user.target"];
      };
    };
  }
