{ pkgs, lib, config, ... }:

{
  system.stateVersion = "25.05";

  environment.systemPackages = with pkgs; [
    jq
  ];

  # Set up two users to demonstrate the user separation
  users.users.alice = {
    isNormalUser = true;
    password = "a";
    uid = 1000;
  };

  users.users.bob = {
    isNormalUser = true;
    password = "b";
    uid = 1001;
  };

  # Set up intake for both users
  services.intake.extraPackages = with pkgs; [
    jq
  ];
  services.intake.users = {
    alice = {
      enable = true;
      listen.addr = "0.0.0.0";
      listen.port = 6001;
    };
    bob = {
      enable = true;
      listen.addr = "0.0.0.0";
      listen.port = 6002;
    };
  };

  # Forward both ports
  virtualisation.forwardPorts = [
    {
      from = "host";
      host.port = 6001;
      guest.port = 6001;
    }
    {
      from = "host";
      host.port = 6002;
      guest.port = 6002;
    }
  ];

  # Disable nixos-shell autologin
  services.getty.autologinUser = lib.mkForce null;

  # Disable default mounts
  nixos-shell.mounts = {
    mountHome = false;
    mountNixProfile = false;
    cache = "none";
  };

  # Define a setup service to create some demo content
  systemd.services =
  let
    setupFor = userName: script: {
      description = "Intake demo setup for ${userName}";
      serviceConfig = {
        User = userName;
        Type = "oneshot";
        RemainAfterExit = true;
      };
      path = config.environment.systemPackages ++ [ pkgs.intake ];
      environment.INTAKE_DATA_DIR = "/home/${userName}/.local/share/intake";
      wantedBy = [ "intake-${userName}.service" ];
      before = [ "intake-${userName}.service" ];
      after = [ "network.target" ];
      script = builtins.readFile ./${userName}.sh;
    };
  in
  {
    intake-alice-setup = setupFor "alice";
    intake-bob-setup = setupFor "bob";
  };

  # Include some demo instructions
  environment.etc.issue.text = ''
    ###
    # Welcome to the intake demo! Log in as `alice` with password `a` to begin.
    #
    # Exit the VM with ctrl+a x, or switch to the qemu console with ctrl+a c and `quit`.
    ###

  '';

  users.motd = ''

    ###
    # The web interfaces are exposed at http://localhost:6001 and http://localhost:6002
    #
    # Within this demo VM, you can run `intake` CLI commands.
    ###

  '';
}