// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.cloud.developerconnect.v1;

import "google/api/annotations.proto";
import "google/api/client.proto";
import "google/api/field_behavior.proto";
import "google/api/field_info.proto";
import "google/api/resource.proto";
import "google/cloud/developerconnect/v1/developer_connect.proto";
import "google/longrunning/operations.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";
import "google/rpc/status.proto";

option csharp_namespace = "Google.Cloud.DeveloperConnect.V1";
option go_package = "cloud.google.com/go/developerconnect/apiv1/developerconnectpb;developerconnectpb";
option java_multiple_files = true;
option java_outer_classname = "InsightsConfigProto";
option java_package = "com.google.cloud.developerconnect.v1";
option php_namespace = "Google\\Cloud\\DeveloperConnect\\V1";

// Creates and manages InsightsConfigs.
//
// The InsightsConfig resource is the core configuration object to capture
// events from your Software Development Lifecycle. It acts as the central hub
// for managing how Developer connect understands your application, its runtime
// environments, and the artifacts deployed within them.
// A user can create an InsightsConfig, list previously-requested
// InsightsConfigs or get InsightsConfigs by their ID to determine the status of
// the InsightsConfig.
service InsightsConfigService {
  option (google.api.default_host) = "developerconnect.googleapis.com";
  option (google.api.oauth_scopes) =
      "https://www.googleapis.com/auth/cloud-platform";

  // Lists InsightsConfigs in a given project and location.
  rpc ListInsightsConfigs(ListInsightsConfigsRequest)
      returns (ListInsightsConfigsResponse) {
    option (google.api.http) = {
      get: "/v1/{parent=projects/*/locations/*}/insightsConfigs"
    };
    option (google.api.method_signature) = "parent";
  }

  // Creates a new InsightsConfig in a given project and location.
  rpc CreateInsightsConfig(CreateInsightsConfigRequest)
      returns (google.longrunning.Operation) {
    option (google.api.http) = {
      post: "/v1/{parent=projects/*/locations/*}/insightsConfigs"
      body: "insights_config"
    };
    option (google.api.method_signature) =
        "parent,insights_config,insights_config_id";
    option (google.longrunning.operation_info) = {
      response_type: "InsightsConfig"
      metadata_type: "google.cloud.developerconnect.v1.OperationMetadata"
    };
  }

  // Gets details of a single Insight.
  rpc GetInsightsConfig(GetInsightsConfigRequest) returns (InsightsConfig) {
    option (google.api.http) = {
      get: "/v1/{name=projects/*/locations/*/insightsConfigs/*}"
    };
    option (google.api.method_signature) = "name";
  }

  // Updates the parameters of a single InsightsConfig.
  rpc UpdateInsightsConfig(UpdateInsightsConfigRequest)
      returns (google.longrunning.Operation) {
    option (google.api.http) = {
      patch: "/v1/{insights_config.name=projects/*/locations/*/insightsConfigs/*}"
      body: "insights_config"
    };
    option (google.longrunning.operation_info) = {
      response_type: "InsightsConfig"
      metadata_type: "google.cloud.developerconnect.v1.OperationMetadata"
    };
  }

  // Delete a single Insight.
  rpc DeleteInsightsConfig(DeleteInsightsConfigRequest)
      returns (google.longrunning.Operation) {
    option (google.api.http) = {
      delete: "/v1/{name=projects/*/locations/*/insightsConfigs/*}"
    };
    option (google.api.method_signature) = "name";
    option (google.longrunning.operation_info) = {
      response_type: "google.protobuf.Empty"
      metadata_type: "google.cloud.developerconnect.v1.OperationMetadata"
    };
  }
}

// The InsightsConfig resource is the core configuration object to capture
// events from your Software Development Lifecycle. It acts as the central hub
// for managing how Developer connect understands your application, its runtime
// environments, and the artifacts deployed within them.
message InsightsConfig {
  option (google.api.resource) = {
    type: "developerconnect.googleapis.com/InsightsConfig"
    pattern: "projects/{project}/locations/{location}/insightsConfigs/{insights_config}"
    plural: "insightsConfigs"
    singular: "insightsConfig"
    style: DECLARATIVE_FRIENDLY
  };

  // The state of the InsightsConfig.
  enum State {
    // No state specified.
    STATE_UNSPECIFIED = 0;

    // The InsightsConfig is pending application discovery/runtime discovery.
    PENDING = 5;

    // The initial discovery process is complete.
    COMPLETE = 3;

    // The InsightsConfig is in an error state.
    ERROR = 4;
  }

  // The context of the InsightsConfig.
  oneof insights_config_context {
    // Optional. The name of the App Hub Application.
    // Format:
    // projects/{project}/locations/{location}/applications/{application}
    string app_hub_application = 4 [(google.api.field_behavior) = OPTIONAL];
  }

  // Identifier. The name of the InsightsConfig.
  // Format:
  // projects/{project}/locations/{location}/insightsConfigs/{insightsConfig}
  string name = 1 [(google.api.field_behavior) = IDENTIFIER];

  // Output only. [Output only] Create timestamp
  google.protobuf.Timestamp create_time = 2
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. [Output only] Update timestamp
  google.protobuf.Timestamp update_time = 3
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The runtime configurations where the application is deployed.
  repeated RuntimeConfig runtime_configs = 5
      [(google.api.field_behavior) = OUTPUT_ONLY];

  // Optional. The artifact configurations of the artifacts that are deployed.
  repeated ArtifactConfig artifact_configs = 6
      [(google.api.field_behavior) = OPTIONAL];

  // Optional. Output only. The state of the InsightsConfig.
  State state = 7 [(google.api.field_behavior) = OPTIONAL];

  // Optional. User specified annotations. See
  // https://google.aip.dev/148#annotations for more details such as format and
  // size limitations.
  map<string, string> annotations = 8 [(google.api.field_behavior) = OPTIONAL];

  // Optional. Set of labels associated with an InsightsConfig.
  map<string, string> labels = 9 [(google.api.field_behavior) = OPTIONAL];

  // Output only. Reconciling (https://google.aip.dev/128#reconciliation).
  // Set to true if the current state of InsightsConfig does not match the
  // user's intended state, and the service is actively updating the resource to
  // reconcile them. This can happen due to user-triggered updates or
  // system actions like failover or maintenance.
  bool reconciling = 10 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. Any errors that occurred while setting up the InsightsConfig.
  // Each error will be in the format: `field_name: error_message`, e.g.
  // GetAppHubApplication: Permission denied while getting App Hub
  // application. Please grant permissions to the P4SA.
  repeated google.rpc.Status errors = 11
      [(google.api.field_behavior) = OUTPUT_ONLY];
}

// RuntimeConfig represents the runtimes where the application is
// deployed.
message RuntimeConfig {
  // The state of the runtime in the InsightsConfig.
  // Whether the runtime is linked to the InsightsConfig.
  enum State {
    // No state specified.
    STATE_UNSPECIFIED = 0;

    // The runtime configuration has been linked to the InsightsConfig.
    LINKED = 1;

    // The runtime configuration has been unlinked to the InsightsConfig.
    UNLINKED = 2;
  }

  // The type of the runtime.
  oneof runtime {
    // Output only. Google Kubernetes Engine runtime.
    GKEWorkload gke_workload = 3 [(google.api.field_behavior) = OUTPUT_ONLY];
  }

  // Where the runtime is derived from.
  oneof derived_from {
    // Output only. App Hub Workload.
    AppHubWorkload app_hub_workload = 4
        [(google.api.field_behavior) = OUTPUT_ONLY];
  }

  // Required. Immutable. The URI of the runtime configuration.
  // For GKE, this is the cluster name.
  // For Cloud Run, this is the service name.
  string uri = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.field_behavior) = IMMUTABLE
  ];

  // Output only. The state of the Runtime.
  State state = 2 [(google.api.field_behavior) = OUTPUT_ONLY];
}

// GKEWorkload represents the Google Kubernetes Engine runtime.
message GKEWorkload {
  // Required. Immutable. The name of the GKE cluster.
  // Format:
  // `projects/{project}/locations/{location}/clusters/{cluster}`.
  string cluster = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.field_behavior) = IMMUTABLE
  ];

  // Output only. The name of the GKE deployment.
  // Format:
  // `projects/{project}/locations/{location}/clusters/{cluster}/namespaces/{namespace}/deployments/{deployment}`.
  string deployment = 2 [(google.api.field_behavior) = OUTPUT_ONLY];
}

// AppHubWorkload represents the App Hub Workload.
message AppHubWorkload {
  // Required. Output only. Immutable. The name of the App Hub Workload.
  // Format:
  // `projects/{project}/locations/{location}/applications/{application}/workloads/{workload}`.
  string workload = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.field_behavior) = IMMUTABLE,
    (google.api.field_behavior) = OUTPUT_ONLY
  ];

  // Output only. The criticality of the App Hub Workload.
  string criticality = 2 [(google.api.field_behavior) = OUTPUT_ONLY];

  // Output only. The environment of the App Hub Workload.
  string environment = 3 [(google.api.field_behavior) = OUTPUT_ONLY];
}

// The artifact config of the artifact that is deployed.
message ArtifactConfig {
  // The storage location of the artifact.
  oneof artifact_storage {
    // Optional. Set if the artifact is stored in Artifact regsitry.
    GoogleArtifactRegistry google_artifact_registry = 2
        [(google.api.field_behavior) = OPTIONAL];
  }

  // The storage location of the artifact metadata.
  oneof artifact_metadata_storage {
    // Optional. Set if the artifact metadata is stored in Artifact analysis.
    GoogleArtifactAnalysis google_artifact_analysis = 3
        [(google.api.field_behavior) = OPTIONAL];
  }

  // Required. Immutable. The URI of the artifact that is deployed.
  // e.g. `us-docker.pkg.dev/my-project/my-repo/image`.
  // The URI does not include the tag / digest because it captures a lineage of
  // artifacts.
  string uri = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.field_behavior) = IMMUTABLE
  ];
}

// Google Artifact Analysis configurations.
message GoogleArtifactAnalysis {
  // Required. The project id of the project where the provenance is stored.
  string project_id = 1 [(google.api.field_behavior) = REQUIRED];
}

// Google Artifact Registry configurations.
message GoogleArtifactRegistry {
  // Required. The host project of Artifact Registry.
  string project_id = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. Immutable. The name of the artifact registry package.
  string artifact_registry_package = 2 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.field_behavior) = IMMUTABLE
  ];
}

// Request for creating an InsightsConfig.
message CreateInsightsConfigRequest {
  // Required. Value for parent.
  string parent = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      child_type: "developerconnect.googleapis.com/InsightsConfig"
    }
  ];

  // Required. ID of the requesting InsightsConfig.
  string insights_config_id = 2 [(google.api.field_behavior) = REQUIRED];

  // Required. The resource being created.
  InsightsConfig insights_config = 3 [(google.api.field_behavior) = REQUIRED];

  // Optional. If set, validate the request, but do not actually post it.
  bool validate_only = 4 [(google.api.field_behavior) = OPTIONAL];
}

// Request for getting an InsightsConfig.
message GetInsightsConfigRequest {
  // Required. Name of the resource.
  string name = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      type: "developerconnect.googleapis.com/InsightsConfig"
    }
  ];
}

// Request for requesting list of InsightsConfigs.
message ListInsightsConfigsRequest {
  // Required. Parent value for ListInsightsConfigsRequest.
  string parent = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      child_type: "developerconnect.googleapis.com/InsightsConfig"
    }
  ];

  // Optional. Requested page size. Server may return fewer items than
  // requested. If unspecified, server will pick an appropriate default.
  int32 page_size = 2 [(google.api.field_behavior) = OPTIONAL];

  // Optional. A token identifying a page of results the server should return.
  string page_token = 3 [(google.api.field_behavior) = OPTIONAL];

  // Optional. Filtering results. See https://google.aip.dev/160 for more
  // details. Filter string, adhering to the rules in
  // https://google.aip.dev/160. List only InsightsConfigs matching the filter.
  // If filter is empty, all InsightsConfigs are listed.
  string filter = 4 [(google.api.field_behavior) = OPTIONAL];

  // Optional. Hint for how to order the results.
  string order_by = 5 [(google.api.field_behavior) = OPTIONAL];
}

// Request for response to listing InsightsConfigs.
message ListInsightsConfigsResponse {
  // The list of InsightsConfigs.
  repeated InsightsConfig insights_configs = 1;

  // A token identifying a page of results the server should return.
  string next_page_token = 2;

  // Locations that could not be reached.
  repeated string unreachable = 3;
}

//
// Request for deleting an InsightsConfig.
message DeleteInsightsConfigRequest {
  // Required. Value for parent.
  string name = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      type: "developerconnect.googleapis.com/InsightsConfig"
    }
  ];

  // Optional. An optional request ID to identify requests. Specify a unique
  // request ID so that if you must retry your request, the server will know to
  // ignore the request if it has already been completed. The server will
  // guarantee that for at least 60 minutes after the first request.
  //
  // For example, consider a situation where you make an initial request and the
  // request times out. If you make the request again with the same request
  // ID, the server can check if original operation with the same request ID
  // was received, and if so, will ignore the second request. This prevents
  // clients from accidentally creating duplicate commitments.
  //
  // The request ID must be a valid UUID with the exception that zero UUID is
  // not supported (00000000-0000-0000-0000-000000000000).
  string request_id = 2 [
    (google.api.field_info).format = UUID4,
    (google.api.field_behavior) = OPTIONAL
  ];

  // Optional. If set, validate the request, but do not actually post it.
  bool validate_only = 3 [(google.api.field_behavior) = OPTIONAL];

  // Optional. This checksum is computed by the server based on the value of
  // other fields, and may be sent on update and delete requests to ensure the
  // client has an up-to-date value before proceeding.
  string etag = 4 [(google.api.field_behavior) = OPTIONAL];
}

// Request for updating an InsightsConfig.
message UpdateInsightsConfigRequest {
  // Required. The resource being updated.
  InsightsConfig insights_config = 2 [(google.api.field_behavior) = REQUIRED];

  // Optional. An optional request ID to identify requests. Specify a unique
  // request ID so that if you must retry your request, the server will know to
  // ignore the request if it has already been completed. The server will
  // guarantee that for at least 60 minutes after the first request.
  //
  // For example, consider a situation where you make an initial request and the
  // request times out. If you make the request again with the same request
  // ID, the server can check if original operation with the same request ID
  // was received, and if so, will ignore the second request. This prevents
  // clients from accidentally creating duplicate commitments.
  //
  // The request ID must be a valid UUID with the exception that zero UUID is
  // not supported (00000000-0000-0000-0000-000000000000).
  string request_id = 3 [
    (google.api.field_info).format = UUID4,
    (google.api.field_behavior) = OPTIONAL
  ];

  // Optional. If set to true, and the insightsConfig is not found a new
  // insightsConfig will be created. In this situation `update_mask` is ignored.
  // The creation will succeed only if the input insightsConfig has all the
  // necessary information (e.g a github_config with both  user_oauth_token and
  // installation_id properties).
  bool allow_missing = 4 [(google.api.field_behavior) = OPTIONAL];

  // Optional. If set, validate the request, but do not actually post it.
  bool validate_only = 5 [(google.api.field_behavior) = OPTIONAL];
}
