# storage-node01 - Storage Control Plane Primary
#
# Services: ChainFire, FlareDB, IAM, PlasmaVMC, CoronaFS, LightningStor

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

{
  imports = [
    ./common.nix
    ../modules/chainfire.nix
    ../modules/flaredb.nix
    ../modules/iam.nix
    ../modules/plasmavmc.nix
    ../modules/coronafs.nix
    ../modules/lightningstor.nix
  ];

  photonTestCluster = {
    sshBasePort = 2300;
    vdeSock = "/tmp/photoncloud-test-cluster-vde.sock-storage";
  };

  networking.hostName = "node01";

  virtualisation = {
    cores = lib.mkForce 8;
    memorySize = 10240;
    diskSize = 61440;
  };

  networking.interfaces.eth1.ipv4.addresses = [{
    address = "10.100.0.11";
    prefixLength = 24;
  }];

  services.chainfire = {
    enable = true;
    nodeId = "node01";
    raftAddr = "10.100.0.11:2380";
    apiAddr = "10.100.0.11:2379";
    initialPeers = [
      "node01=10.100.0.11:2380"
      "node02=10.100.0.12:2380"
      "node03=10.100.0.13:2380"
    ];
  };

  services.flaredb = {
    enable = true;
    nodeId = "node01";
    raftAddr = "10.100.0.11:2480";
    apiAddr = "10.100.0.11:2479";
    initialPeers = [
      "node01=10.100.0.11:2479"
      "node02=10.100.0.12:2479"
      "node03=10.100.0.13:2479"
    ];
    settings.namespace_modes = {
      default = "strong";
      validation = "eventual";
      plasmavmc = "strong";
      lightningstor = "eventual";
      creditservice = "strong";
    };
  };

  services.iam = {
    enable = true;
    port = 50080;
    chainfireAddr = config.photonTestCluster.chainfireControlPlaneAddrs;
    flaredbAddr = config.photonTestCluster.flaredbControlPlaneAddrs;
  };

  services.plasmavmc = {
    enable = true;
    mode = "server";
    port = 50082;
    httpPort = 8084;
    iamAddr = "10.100.0.11:50080";
    chainfireAddr = config.photonTestCluster.chainfireControlPlaneAddrs;
    flaredbAddr = config.photonTestCluster.flaredbControlPlaneAddrs;
    lightningstorAddr = "10.100.0.11:50086";
    coronafsControllerEndpoint = "http://127.0.0.1:50088";
    coronafsNodeEndpoint = "http://127.0.0.1:50088";
  };

  services.coronafs = {
    enable = true;
    metadataBackend = "chainfire";
    chainfireKeyPrefix = "/coronafs/test-cluster/storage/volumes";
    port = 50088;
    advertiseHost = "10.100.0.11";
    exportBasePort = 11000;
    exportPortCount = 256;
    exportSharedClients = 32;
    exportCacheMode = "none";
    exportAioMode = "io_uring";
    exportDiscardMode = "ignore";
    exportDetectZeroesMode = "off";
    preallocate = true;
    syncOnWrite = false;
  };

  services.lightningstor = {
    enable = true;
    mode = "all-in-one";
    port = 50086;
    nodePort = 50090;
    s3Port = 9000;
    objectStorageBackend = "distributed";
    distributedRequestTimeoutMs = 300000;
    distributedNodeEndpoints = [
      "http://10.100.0.21:50086"
      "http://10.100.0.22:50086"
    ];
    replicaCount = 3;
    readQuorum = 1;
    writeQuorum = 2;
    nodeMetricsPort = 9198;
    chainfireAddr = config.photonTestCluster.chainfireControlPlaneAddrs;
    iamAddr = "10.100.0.11:50080";
    flaredbAddr = config.photonTestCluster.flaredbControlPlaneAddrs;
    zone = "zone-a";
    region = "test";
  };

  systemd.services.iam.environment = {
    IAM_ALLOW_RANDOM_SIGNING_KEY = "1";
    IAM_ALLOW_UNAUTHENTICATED_ADMIN = "true";
  };

  systemd.services.lightningstor.environment = {
    S3_ACCESS_KEY_ID = "photoncloud-test";
    S3_SECRET_KEY = "photoncloud-test-secret";
  };
}
