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

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

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

  virtualisation.diskSize = 4096;

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

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

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

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

  systemd.services.photon-vm-smoke = {
    description = "PhotonCloud VM smoke marker";
    wantedBy = [ "multi-user.target" ];
    wants = [ "systemd-udev-settle.service" ];
    after = [ "local-fs.target" "systemd-udev-settle.service" ];
    path = with pkgs; [
      bash
      coreutils
      e2fsprogs
      gawk
      gnugrep
      gnused
      util-linux
    ];
    serviceConfig = {
      Type = "simple";
      Restart = "always";
      RestartSec = "1";
    };
    script = ''
      mkdir -p /var/lib/photon-vm-smoke
      count_file=/var/lib/photon-vm-smoke/boot-count
      if [ -f "$count_file" ]; then
        count=$(( $(cat "$count_file") + 1 ))
      else
        count=1
      fi
      echo "$count" > "$count_file"
      echo "PHOTON_VM_SMOKE_READY count=$count" >/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
      echo "PHOTON_VM_SMOKE_DATA_ROOT count=$count source=''${root_source:-none} root=''${root_disk:-unknown}" >/dev/ttyS0

      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))
      scan_attempt=0
      while [ -z "$data_disk" ] && [ "$SECONDS" -lt "$deadline" ]; do
        scan_attempt=$((scan_attempt + 1))
        data_disk="$(pick_data_disk || true)"
        echo "PHOTON_VM_SMOKE_DATA_SCAN count=$count attempt=$scan_attempt data=''${data_disk:-none}" >/dev/ttyS0
        [ -n "$data_disk" ] && break
        udevadm settle >/dev/null 2>&1 || true
        sleep 1
      done

      if [ -z "$data_disk" ]; then
        echo "PHOTON_VM_SMOKE_DATA_MISSING count=$count" >/dev/ttyS0
        lsblk -dn -o NAME,TYPE,SIZE >/dev/ttyS0 2>&1 || true
        exit 1
      fi

      echo "PHOTON_VM_SMOKE_DATA_PROBE count=$count root=''${root_disk:-unknown} data=$(basename "$data_disk")" >/dev/ttyS0
      mkdir -p /mnt/photon-vm-data
      if ! blkid "$data_disk" >/dev/null 2>&1; then
        mkfs_output="$(mkfs.ext4 -L photon-vm-data -F "$data_disk" 2>&1)" || {
          mkfs_output="$(printf '%s' "$mkfs_output" | tr '\r\n' ' ' | sed 's/  */ /g')"
          echo "PHOTON_VM_SMOKE_DATA_ERROR count=$count step=mkfs device=$(basename "$data_disk") detail=''${mkfs_output}" >/dev/ttyS0
          lsblk -dn -o NAME,TYPE,RO,SIZE >/dev/ttyS0 2>&1 || true
          blockdev --getsize64 "$data_disk" >/dev/ttyS0 2>&1 || true
          exit 1
        }
      fi
      if ! mountpoint -q /mnt/photon-vm-data; then
        if ! mount "$data_disk" /mnt/photon-vm-data; then
          echo "PHOTON_VM_SMOKE_DATA_ERROR count=$count step=mount device=$(basename "$data_disk")" >/dev/ttyS0
          lsblk -f >/dev/ttyS0 2>&1 || true
          exit 1
        fi
      fi
      data_count_file=/mnt/photon-vm-data/boot-count
      if [ -f "$data_count_file" ]; then
        data_count=$(( $(cat "$data_count_file") + 1 ))
      else
        data_count=1
      fi
      echo "$data_count" > "$data_count_file"
      sync
      echo "PHOTON_VM_SMOKE_DATA_READY count=$data_count device=$(basename "$data_disk")" >/dev/ttyS0

      while true; do
        echo "PHOTON_VM_SMOKE_HEARTBEAT count=$count ts=$(date +%s)" >/dev/ttyS0
        sleep 2
      done
    '';
  };

  system.stateVersion = "24.05";
}
