# Configuration options specific to a single workadventure instance.

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

with lib;
with pkgs;
{
  options = rec {
    
    backend = {
      enable = mkOption {
        default = true;
        type = types.bool;
      };
      
      httpPort = mkOption {
        default = 8081;
        type = types.ints.u16;
        description = "The TCP port the backend will bind to for http";
      };

      grpcPort = mkOption {
        default = 50051;
        type = types.ints.u16;
        description = "The TCP port the backend will bind to for grpc";
      };

      package = mkOption {
        default = workadventure.back;
        defaultText = "third_party.workadventure-nix.back";
        type = types.package;
        description = "Backend package to use";
      };
    };

    pusher = {
      enable = mkOption {
        default = true;
        type = types.bool;
      };
      
      port = mkOption {
        default = 8080;
        type = types.ints.u16;
        description = "The TCP port the pusher will bind to";
      };

      package = mkOption {
        default = workadventure.pusher;
        defaultText = "third_party.workadventure-nix.pusher";
        type = types.package;
        description = "Pusher package to use";
      };
    };

    frontend = {
      package = mkOption {
        default = workadventure.front;
        defaultText = "third_party.workadventure-nix.front";
        type = types.package;
        description = "Front package to use";
      };
      
      debugMode = mkOption {
        default = false;
        description = "Whether or not to run the frontend in debug mode";
        type = types.bool;
      };
      
      startRoomUrl = mkOption {
        default = "/_/global/localhost/maps/Floor0/floor0.json";
        description = "The workadventure map url that users join by default";
        type = types.str;
      };
      
      resolution = mkOption {
        default = 2;
        description = "resolution of workadventure";
        type = types.int;
      };
      
      zoomLevel = mkOption {
        default = 1;
        description = "The default zoom level of maps";
        type = types.int;
      };
      
      positionDelay = mkOption {
        default = 200;
        description = "Delay in milliseconds between sending position events";
        type = types.int;
      };
      
      maxExtrapolationTime = mkOption {
        default = 100;
        description = "Maximum time period in which movements of other players are extrapolated";
        type = types.int;
      };
      
      
      urls = {
        api = mkOption {
          default = "/pusher";
          type = types.str;
          description = "The base url for the api, from the browser's point of view";
        };

        uploader = mkOption {
          default = "/uploader";
          type = types.str;
          description = "The base url for the uploader, from the browser's point of view";
        };

        admin = mkOption {
          default = "/pusher/admin";
          type = types.str;
          description = "The base url for the admin, from the browser's point of view";
        };

        maps = mkOption {
          default = "/maps";
          type = types.str;
          description = "The base url for serving maps, from the browser's point of view";
        };
      };
    };

    commonConfig = {
      secretKey = mkOption {
        default = "THECODINGMACHINE_SECRET_KEY";
        type = types.str;
      };
      
      minimumDistance = mkOption {
        default = 64;
        type = types.int;
      };
      
      groupRadius = mkOption {
        default = 48;
        type = types.int;
      };
      
      allowArtillery = mkOption {
        default = false;
        type = types.bool;
      };
      
      maxUsersPerRoom = mkOption {
        default = 600;
        type = types.int;
      };
      
      cpuOverheatThreshold = mkOption {
        default = 80;
        type = types.int;
      };
    
      socketIdleTime = mkOption {
        default = 30;
        type = types.int;
      };  
    
      webrtc = {
        stun = {
          url = mkOption {
            default = "stun:stun.l.google.com:19302";
            description = "The STUN server to use for peer connections";
            type = types.str;
          };
        };
        turn = {
          url = mkOption {
            default = "turn:coturn.workadventure.localhost:3478";
            description = "The TURN server to use for peer connections";
            type = types.str;
          };
          user = mkOption {
            default = "workadventure";
            description = "Username for TURN authentication";
            type = types.str; # TODO: also allow no user
          };
          password = mkOption {
            default = "workadventure";
            description = "Password for TURN authentication";
            type = types.str;
          };
        };
      };
      
      jitsi = {
        url = mkOption {
          default = "meet.jit.si";
          description = "Jitsi instance to use for conference rooms";
          type = types.str;
        };
        privateMode = mkOption {
          default = false;
          description = "Jitsi private mode";
          type = types.bool;
        };
        iss = mkOption {
          default = "";
          type = types.str;
        };
        secretKey = mkOption {
          default = "";
          type = types.str;
        };
      };
    };


    nginx = {
      enable = mkOption {
        default = true;
        type = types.bool;
        description = "enable nginx as proxy, and for serving maps";
      };
    
      default = mkOption {
        default = false;
        type = types.bool;
        description = "Whether this instance will be the default one served by nginx";
      };

      domain = mkOption {
        default = "localhost";
        type = types.str;
        description = "The domain name to serve workadenture services under.";
      };

      maps = {
        serve = mkOption {
          default = true;
          type = types.bool;
          description = "Whether to serve maps through nginx.";
        };
        path = mkOption {
          default = workadventure.maps.outPath + "/workadventuremaps/";
          defaultText = "third_party.workadventure-nix.maps";
          type = types.path;
          description = "Maps package to use";
        };
      };
    };
  };
}