// SPDX-License-Identifier: Apache-2.0
// Webhook subscriptions: events are delivered as JSON (CloudEvents 1.0
// envelope) with an HMAC-SHA256 signature, retried with backoff, and parked
// in a dead-letter list after the last attempt.
syntax = "proto3";

package kyvernon.v1;

import "kyvernon/v1/common.proto";

// Webhook subscriptions and their deliveries.
service WebhookService {
  // Creates a webhook subscription.
  rpc CreateSubscription(CreateSubscriptionRequest) returns (Subscription);
  // The webhook subscriptions.
  rpc ListSubscriptions(Empty) returns (ListSubscriptionsResponse);
  // Deletes a subscription.
  rpc DeleteSubscription(DeleteSubscriptionRequest) returns (Empty);
  // Sends a synthetic event to the subscription and reports the outcome.
  rpc TestSubscription(TestSubscriptionRequest) returns (DeliveryResult);
  // Deliveries that failed (dead letters).
  rpc ListDeadLetters(ListDeadLettersRequest) returns (ListDeadLettersResponse);
}

// A webhook subscription.
message Subscription {
  // Id of the subscription.
  string id = 1;
  // Name.
  string name = 2;
  // HTTPS target the events are posted to.
  string url = 3;
  // Event types to deliver (empty = all).
  repeated string event_types = 4;
  // Free-form labels; alarms reference them in their actions.
  repeated string labels = 5;
  // Deliveries are made.
  bool enabled = 6;
  // Delivery attempts before an event becomes a dead letter.
  uint32 max_attempts = 7;
  // Events that failed every attempt and are kept.
  uint32 dead_letters = 8;
  // Events delivered.
  uint64 delivered = 9;
  // Delivery attempts that failed.
  uint64 failed = 10;
  // Tenant filter (tenant-N): only that tenant's self-service events.
  string tenant = 11;
}

// Creates a subscription.
message CreateSubscriptionRequest {
  // Name.
  string name = 1;
  // HTTPS target.
  string url = 2;
  // Shared secret for HMAC-SHA256; never returned.
  string secret = 3;
  // Event types to deliver; empty for all.
  repeated string event_types = 4;
  // Labels of the subscription.
  repeated string labels = 5;
  // Delivery attempts before a dead letter; 0 for the default.
  uint32 max_attempts = 6;
  // Deliver only the self-service events of this tenant (tenant-N; empty: all).
  string tenant = 7;
}

// The subscriptions.
message ListSubscriptionsResponse {
  // Every subscription the caller can see.
  repeated Subscription items = 1;
}

// Deletes a subscription.
message DeleteSubscriptionRequest {
  // Id of the subscription.
  string id = 1;
}

// Sends a test event.
message TestSubscriptionRequest {
  // Id of the subscription.
  string id = 1;
}

// Outcome of a delivery.
message DeliveryResult {
  // The target accepted the event.
  bool delivered = 1;
  // HTTP status the target answered; 0 without an answer.
  uint32 status = 2;
  // Why the delivery failed; empty on success.
  string error = 3;
  // Attempts made.
  uint32 attempts = 4;
}

// An event that failed every attempt.
message DeadLetter {
  // Id of the event.
  string event_id = 1;
  // Attempts made.
  uint32 attempts = 2;
  // Error of the last attempt.
  string last_error = 3;
  // Time of the last attempt, RFC 3339.
  string failed_at = 4;
}

// Lists the dead letters of a subscription.
message ListDeadLettersRequest {
  // Id of the subscription.
  string id = 1;
}

// The dead letters.
message ListDeadLettersResponse {
  // Dead letters of the subscription.
  repeated DeadLetter items = 1;
}
