{ modulesPath, lib, pkgs, ... }:

{
  imports = [
    (modulesPath + "/virtualisation/disk-image.nix")
    (modulesPath + "/profiles/qemu-guest.nix")
  ];

  image = {
    baseName = "photon-vm-bench";
    format = "qcow2";
    efiSupport = false;
  };

  virtualisation.diskSize = 4096;

  boot.kernelParams = [ "console=ttyS0" "console=tty0" ];

  networking.hostName = "photon-vm-bench";
  networking.useDHCP = lib.mkDefault true;

  services.getty.autologinUser = "root";
  users.mutableUsers = false;
  users.users.root.hashedPassword = "$6$ultracloud$aUJCEE5wm/b5O.9KIKGm84qUWdWXwnebsFEiMBF7u9Y7AOWodaMrjbbKGMOf0X59VJyJeMRsgbT7VWeqMHpUe.";

  documentation.enable = false;
  services.openssh.enable = false;
  environment.systemPackages = with pkgs; [ e2fsprogs fio jq util-linux ];

  systemd.services.photon-vm-bench = {
    description = "UltraCloud VM benchmark marker";
    wantedBy = [ "multi-user.target" ];
    wants = [ "systemd-udev-settle.service" ];
    after = [ "local-fs.target" "systemd-udev-settle.service" ];
    path = with pkgs; [
      bash
      coreutils
      e2fsprogs
      fio
      gawk
      gnugrep
      jq
      gnused
      util-linux
    ];
    serviceConfig = {
      Type = "simple";
      Restart = "no";
    };
    script = ''
      set -euo pipefail

      log_console() {
        printf '%s\n' "$*" >/dev/ttyS0
      }

      root_source="$(lsblk -nrpo NAME,MOUNTPOINT | awk '$2 == "/" { print $1; exit }' 2>/dev/null || true)"
      root_disk=""
      if [ -n "$root_source" ] && [ -b "$root_source" ]; then
        root_disk="$(lsblk -ndo PKNAME "$root_source" 2>/dev/null || true)"
        if [ -z "$root_disk" ]; then
          root_disk="$(basename "$root_source")"
        else
          root_disk="/dev/$root_disk"
        fi
      fi

      data_disk=""
      if [ -b /dev/disk/by-label/photon-vm-data ]; then
        data_disk="$(readlink -f /dev/disk/by-label/photon-vm-data)"
      fi

      pick_data_disk() {
        while IFS= read -r disk; do
          [ -n "$disk" ] || continue
          if [ -n "$root_source" ] && [ "$disk" = "$root_source" ]; then
            continue
          fi
          if [ -n "$root_disk" ] && [ "$disk" = "$root_disk" ]; then
            continue
          fi
          if lsblk -nrpo MOUNTPOINT "$disk" 2>/dev/null | grep -qx '/'; then
            continue
          fi
          printf '%s\n' "$disk"
          return 0
        done < <(lsblk -dnpr -o NAME,TYPE,RO | awk '$2 == "disk" && $3 == "0" { print $1 }')
        return 1
      }

      deadline=$((SECONDS + 60))
      attempt=0
      while [ -z "$data_disk" ] && [ "$SECONDS" -lt "$deadline" ]; do
        attempt=$((attempt + 1))
        data_disk="$(pick_data_disk || true)"
        log_console "PHOTON_VM_BENCH_SCAN attempt=$attempt data=''${data_disk:-none}"
        [ -n "$data_disk" ] && break
        udevadm settle >/dev/null 2>&1 || true
        sleep 1
      done

      if [ -z "$data_disk" ]; then
        log_console "PHOTON_VM_BENCH_ERROR step=detect-data-disk"
        lsblk -dn -o NAME,TYPE,SIZE >/dev/ttyS0 2>&1 || true
        exit 1
      fi

      mkdir -p /mnt/photon-vm-data
      if ! blkid "$data_disk" >/dev/null 2>&1; then
        mkfs.ext4 -L photon-vm-data -F "$data_disk" >/dev/null
      fi
      if ! mountpoint -q /mnt/photon-vm-data; then
        mount "$data_disk" /mnt/photon-vm-data
      fi

      bench_file="/mnt/photon-vm-data/fio-bench.dat"
      fio_json() {
        fio \
          --name=photon-vm-bench \
          --filename="$bench_file" \
          --rw="$1" \
          --bs="$2" \
          --size="$3" \
          --ioengine=libaio \
          --direct=1 \
          --output-format=json \
          "$@"
      }

      log_console "PHOTON_VM_BENCH_READY data=$(basename "$data_disk") root=''${root_disk:-unknown}"

      seq_write_json="$(fio \
        --name=photon-vm-bench-write \
        --filename="$bench_file" \
        --rw=write \
        --bs=1M \
        --size=512M \
        --ioengine=libaio \
        --direct=1 \
        --end_fsync=1 \
        --output-format=json)"
      seq_write_mibps="$(printf '%s' "$seq_write_json" | jq -r '(.jobs[0].write.bw_bytes // 0) / 1048576')"

      seq_read_json="$(fio \
        --name=photon-vm-bench-read \
        --filename="$bench_file" \
        --rw=read \
        --bs=1M \
        --size=512M \
        --ioengine=libaio \
        --direct=1 \
        --output-format=json)"
      seq_read_mibps="$(printf '%s' "$seq_read_json" | jq -r '(.jobs[0].read.bw_bytes // 0) / 1048576')"

      randread_json="$(fio \
        --name=photon-vm-bench-randread \
        --filename="$bench_file" \
        --rw=randread \
        --bs=4k \
        --size=512M \
        --ioengine=libaio \
        --direct=1 \
        --iodepth=32 \
        --runtime=15 \
        --time_based=1 \
        --output-format=json)"
      randread_iops="$(printf '%s' "$randread_json" | jq -r '.jobs[0].read.iops // 0 | floor')"

      sync
      log_console "PHOTON_VM_BENCH_RESULT seq_write_mibps=$seq_write_mibps seq_read_mibps=$seq_read_mibps randread_iops=$randread_iops"

      while true; do
        log_console "PHOTON_VM_BENCH_HEARTBEAT ts=$(date +%s)"
        sleep 5
      done
    '';
  };

  system.stateVersion = "24.05";
}
