{ config, lib, pkgs, inputs, ... }:
let
  survivalPort = 25565;
  creativePort = 25566;
  cobblemonPort = 25567;
  users = {
    clay53_clayton = "bc2653cd-6cb1-4d15-9d24-cce0d1e8811c";
    fire_chase = "c2b1a7ff-abff-41cb-af2a-4a89e942d288";
    piggywhipster = "6b4419bf-2a5f-4932-b779-33dcdbbf4c06";
  };
  defaultWhitelist = {
    inherit (users)
      clay53_clayton
      fire_chase
      piggywhipster
      ;
  };
  defaultOperators = {
    inherit (users)
      clay53_clayton
      fire_chase;
  };
  minRam = "512M";
  maxRam = "1G";
in
{
  imports = [
    inputs.nix-minecraft.nixosModules.minecraft-servers
  ];
  config = lib.mkMerge [
    (lib.mkIf (config.networking.hostName == "nixnas") {
      nixpkgs.overlays = [
        inputs.nix-minecraft.overlay
      ];

      services.minecraft-servers = {
        enable = true;
        eula = true;
        openFirewall = false;
        dataDir = "/Block/minecraft-servers";
      };

      services.minecraft-servers.servers.survival = {
        enable = true;
        autoStart = true;
        restart = "always";
        enableReload = false;
        whitelist = defaultWhitelist;
        operators = defaultOperators;
        serverProperties = {
          server-port = survivalPort;
          difficulty = 3;
          gamemode = 0;
          motd = "Clayton Hickey's Survival";
          white-list = true;
        };
        package = pkgs.vanillaServers.vanilla-1_21_11;
        jvmOpts = "-Xms${minRam} -Xmx${maxRam}";
      };

      services.minecraft-servers.servers.creative = {
        enable = true;
        autoStart = true;
        restart = "always";
        enableReload = false;
        whitelist = defaultWhitelist;
        operators = defaultOperators;
        serverProperties = {
          server-port = creativePort;
          difficulty = 3;
          gamemode = 1;
          motd = "Clayton Hickey's Creative";
          white-list = true;
        };
        package = pkgs.vanillaServers.vanilla-1_21_11;
        jvmOpts = "-Xms${minRam} -Xmx${maxRam}";
      };

      services.minecraft-servers.servers.cobblemon =
      let 
        inherit (inputs.nix-minecraft.lib) collectFilesAt;
        modpack = pkgs.fetchPackwizModpack {
          url = "https://forgejo.claytonhickey.me/clay53/cobblemon-modpack/raw/branch/master/pack.toml";
          packHash = "sha256-rz51hV1kgju7+6OyeEzSwO4+fmoS4/bi0Fo9iCYBOnI=";
        };
      in
      {
        enable = true;
        autoStart = true;
        restart = "always";
        enableReload = false;
        whitelist = defaultWhitelist;
        operators = defaultOperators;
        serverProperties = {
          server-port = cobblemonPort;
          difficulty = 3;
          gamemode = 0;
          motd = "Clayton Hickey's Cobblemon";
          white-list = true;
        };
        package = pkgs.fabricServers.fabric-1_21_1;
        symlinks = collectFilesAt modpack "mods";
      };

      networking.firewall.interfaces.${config.cos.wireguard.interface}.allowedTCPPorts = [
        survivalPort
        creativePort
        cobblemonPort
      ];
    })

    (lib.mkIf (config.networking.hostName == "loadedskypotato") {
      services.nginx.streamConfig = ''
        server {
          listen ${survivalPort} reuseport;
          proxy_pass 10.100.0.2:${survivalPort};
        }
        server {
          listen ${creativePort} reuseport;
          proxy_pass 10.100.0.2:${creativePort};
        }
        server {
          listen ${cobblemonPort} reuseport;
          proxy_pass 10.100.0.2:${cobblemonPort};
        }
      '';
      
      networking.firewall.allowedTCPPorts = [
        survivalPort
        creativePort
        cobblemonPort
      ];
    })
  ];
}
