syntax = "proto3";
import "google/protobuf/timestamp.proto";

package Klang.Seed.Services.InfraOperator;

service InfraOperatorService {
    rpc GetClusterDns(ClusterDnsRequest) returns (ClusterDnsResponse) {}
    rpc GetEnvironment(EnvironmentRequest) returns (EnvironmentResponse) {}
    rpc GetWorlds(WorldsRequest) returns (WorldsResponse) {}
    rpc GetSectors(SectorsRequest) returns (SectorsResponse) {}
    rpc GetEnvironments(EnvironmentsRequest) returns (EnvironmentsResponse) {}

    rpc GetEnvironmentStatus(EnvironmentStatusRequest) returns (StatusResponse) {}
    rpc GetWorldStatus(WorldStatusRequest) returns (StatusResponse) {}
    rpc GetSectorStatus(SectorStatusRequest) returns (StatusResponse) {}
    
    rpc PauseSectorDeployment (PauseSectorDeploymentRequest) returns (PauseSectorDeploymentResponse);
    rpc ResumeSectorDeployment (ResumeSectorDeploymentRequest) returns (ResumeSectorDeploymentResponse);
    rpc ResetSectorDeployment (ResetSectorDeploymentRequest) returns (ResetSectorDeploymentResponse);
    rpc TakeSectorSnapshot(TakeSectorSnapshotRequest) returns (TakeSectorSnapshotResponse);
    rpc UpdateAutoPauseOnSector(UpdateAutoPauseOnSectorRequest) returns (UpdateAutoPauseOnSectorResponse);
    rpc UpdateClientVersionOnSector(UpdateClientVersionOnSectorRequest) returns (UpdateClientVersionOnSectorResponse);
    rpc UpdateWorkerScaleOnSector(UpdateWorkerScaleOnSectorRequest) returns (UpdateWorkerScaleOnSectorResponse);
    rpc UpdateWorkerImagesOnSector(UpdateWorkerImagesOnSectorRequest) returns (UpdateWorkerImagesOnSectorResponse);
    rpc UpdateContentCatalogsOnSector(UpdateContentCatalogsOnSectorRequest) returns (UpdateContentCatalogsOnSectorResponse);

    rpc GetFusionAuthTenantId(FusionauthTenantIdRequest) returns (FusionauthTenantIdResponse) {}
    rpc GetFusionAuthApplicationId(FusionauthApplicationIdRequest) returns (FusionauthApplicationIdResponse) {}
    rpc GetFusionAuthSteamIdpId(FusionauthSteamIdpIdRequest) returns (FusionauthSteamIdpIdResponse) {}
    
    // world backup commands
    rpc CreateWorldBackup(CreateWorldBackupRequest) returns (CreateWorldBackupResponse);
    rpc GetWorldBackup(GetWorldBackupRequest) returns (stream GetWorldBackupResponse);
    rpc ListWorldBackups(ListWorldBackupsRequest) returns (ListWorldBackupsResponse);
    rpc ListAllWorldBackups(ListAllWorldBackupsRequest) returns (ListAllWorldBackupsResponse);
}

message ClusterDnsRequest {
}

message ClusterDnsResponse {
    string dns = 1;
}

message CreateWorldBackupRequest {
    string environment = 1;
    string world = 2;
}

message CreateWorldBackupResponse {
    optional bool success = 1;
    string error = 2;
    optional string backupId = 3;
    optional string backupGsUri = 4;
}

message GetWorldBackupRequest {
    string backupId = 1;
    string environment = 2;
    string world = 3;
}

message GetWorldBackupResponse {
    optional bool success = 1;
    string error = 2;
    optional int64 backupFileSize = 3;
    optional bytes backupFile = 4;
}

message ListWorldBackupsRequest {
    string environment = 1;
    string world = 2;
}

message ListWorldBackupsResponse {
    optional bool success = 1;
    optional string error = 2;
    repeated Backup backups = 3;
    
    message Backup {
        string backupId = 1;
        string backupGsUri = 2;
        int64 timestamp = 3;
    }
}

message ListAllWorldBackupsRequest {
}

message ListAllWorldBackupsResponse {
    optional bool success = 1;
    optional string error = 2;
    repeated WorldBackup worldBackups = 3;
}

message WorldBackup {
    string environment = 1;
    string world = 2;
    string backupId = 3;
    string backupGsUri = 4;
    int64 timestamp = 5;
}

// WorkerType is obsolete. It is kept here for backward compatibility. Remove as soon as all infra-operator clients are updated.
enum WorkerType {
    SERVER_WORKER = 0;
    CLIENT_INTEREST_WORKER = 1;
    COMPONENT_METRICS_WORKER = 2;
    LOAD_BALANCER_WORKER = 3;
    PLAYER_OBSERVER_WORKER = 4;
}

enum NamespaceStatus {
    OK = 0;
    NOT_READY = 1;
}

message FusionauthTenantIdRequest {
}

message FusionauthTenantIdResponse {
    string tenantId = 1;
}

message FusionauthApplicationIdRequest {
}

message FusionauthApplicationIdResponse {
    string applicationId = 1;
}

message FusionauthSteamIdpIdRequest {
}

message FusionauthSteamIdpIdResponse {
    string steamIdpId = 1;
}

message StatusResponse {
    NamespaceStatus status = 1;
    string comment = 2;
    map<string, StatusResponse> details = 3;
}

message EnvironmentStatusRequest {
    string name = 1;
}

message WorldStatusRequest {
    string name = 1;
}

message SectorStatusRequest {
    string name = 1;
}

message EnvironmentRequest {
    string name = 1;
}

message EnvironmentResponse { 
    Environment Data = 1;
    optional bool success = 2;
    optional string error = 3;
}

message EnvironmentsRequest {
}

message EnvironmentsResponse {
    repeated Environment environments = 1;
    optional bool success = 2;
    optional string error = 3;
}

message Environment {
    string name = 1;
    string gitBranch = 2;
    string gitBranchBase64 = 3;
    string gitHash = 4;
    string version = 5;
    string issuer = 6;
    string description = 7;
    string teamCityBuildId = 8; // obsolete replaced by githubJobId
    string teamCityStatus = 9; // obsolete replaced by githubJobStatus
    string githubJobStatus = 10;
    string githubJobId = 11;
}

message WorldsRequest {
    optional string environment = 1;
}

message WorldsResponse {
    repeated World worlds = 1;
    optional bool success = 2;
    optional string error = 3;
}

message World {
    reserved 2, 3, 4, 5, 9;
    string name = 1;
    bool persist = 6;
    google.protobuf.Timestamp creationTimestamp = 7;
    string environmentName = 8;
    string githubJobStatus = 10;
    string githubJobId = 11;
    string issuer = 12;
    string launchdarklyMobileKey = 13;
    string launchdarklySdkKey = 14;
    bool deployedWithIac = 15;
    optional string gitBranch = 16;
    bool useCNPG = 17;
    bool useNemo = 18;
}

message SectorsRequest {
    optional string environment = 1;
    optional string world = 2;
}

message SectorsResponse {
    repeated Sector sectors = 1;
    optional bool success = 2;
    optional string error = 3;
}

message Sector {
    string name = 1;
    string gitBranch = 2;
    string gitBranchBase64 = 3;
    string gitHash = 4;

    bool persist = 6;
    google.protobuf.Timestamp creationTimestamp = 7;
    string teamCityBuildId = 8; // obsolete replaced by githubJobId
    
    string environmentName = 12;
    string worldName = 13;
    string teamCityStatus = 14; // obsolete replaced by githubJobStatus

    // it is optional because we want to support sector deployments without a runtime
    // in order to allow devs to run it locally
    optional RuntimeDeployment runtimeDeployment = 15;
    // obsolete, replaced by 'workers'
    map<string, WorkerDeployment> workerDeployments = 16;
    repeated WorkerInfo workers = 17;
    string clientVersion = 18;
    bool autoPauseEnabled = 19;
    int32 autoPauseMinutes = 20;
    int64 autoPauseStartTime = 21;
    
    string githubJobStatus = 22;
    string githubJobId = 23;
    string issuer = 24;
    
    map<string, string> contentCatalogs = 25;
    
    bool useNemo = 26;
}

message RuntimeDeployment {
    string externalIp = 1;
    int32  httpPort = 2;
    int32  grpcPort = 5;
}

// obsolete, replaced by 'WorkerInfo'
message  WorkerDeployment {
    int32 desiredReplicas = 2;
    int32 currentReplicas = 3;
}

message WorkerInfo {
    WorkerType worker = 1;
    int32 desiredReplicas = 2;
    int32 currentReplicas = 3;
    string image = 4;
    string workerType = 5;
}

message PauseSectorDeploymentRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
}

message PauseSectorDeploymentResponse {
    optional bool success = 1;
    optional string error = 2;
}

message ResumeSectorDeploymentRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
}

message ResumeSectorDeploymentResponse {
    optional bool success = 1;
    optional string error = 2;
}

message ResetSectorDeploymentRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
}

message ResetSectorDeploymentResponse {
    optional bool success = 1;
    optional string error = 2;
}

message TakeSectorSnapshotRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
    bool copySnapshotToDefault = 4;
}

message TakeSectorSnapshotResponse {
    optional bool success = 1;
    optional string error = 2;
}

message UpdateAutoPauseOnSectorRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
    bool autoPause = 4;
    int32 autoPauseMinutes = 5;
}

message UpdateAutoPauseOnSectorResponse {
    optional bool success = 1;
    optional string error = 2;
}

message UpdateClientVersionOnSectorRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
    string clientVersion = 4;
}

message UpdateClientVersionOnSectorResponse {
    optional bool success = 1;
    optional string error = 2;
}

message UpdateWorkerScaleOnSectorRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
    string workerType = 4;
    int32 scale = 5;
}

message UpdateWorkerScaleOnSectorResponse {
    optional bool success = 1;
    optional string error = 2;
}

message UpdateWorkerImagesOnSectorRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
    string workerType = 4;
    string image = 5;
}

message UpdateWorkerImagesOnSectorResponse {
    optional bool success = 1;
    optional string error = 2;
}

message UpdateContentCatalogsOnSectorRequest {
    string environment = 1;
    string world = 2;
    string sector = 3;
    map<string, string> contentCatalogs = 4;
}

message UpdateContentCatalogsOnSectorResponse {
    optional bool success = 1;
    optional string error = 2;
}

