//! Discoverable metadata for outbound webhook `route_key` values (admin / Studio).

use serde::Serialize;

use super::schema::{
    ROUTE_SCHEMA_COLUMN_ADDED, ROUTE_SCHEMA_COLUMN_ALTERED, ROUTE_SCHEMA_COLUMN_DROPPED,
    ROUTE_SCHEMA_COLUMN_RENAMED, ROUTE_SCHEMA_EXTENSION_INSTALLED, ROUTE_SCHEMA_INDEX_CREATED,
    ROUTE_SCHEMA_INDEX_DROPPED, ROUTE_SCHEMA_TABLE_CREATED, ROUTE_SCHEMA_TABLE_DROPPED,
    ROUTE_SCHEMA_VIEW_CREATED, ROUTE_SCHEMA_VIEW_DROPPED,
};
use super::{
    ROUTE_BILLING_DOCUMENT_UPSERTED, ROUTE_GATEWAY_DATA, ROUTE_GATEWAY_DELETE, ROUTE_GATEWAY_FETCH,
    ROUTE_GATEWAY_INSERT, ROUTE_GATEWAY_QUERY, ROUTE_GATEWAY_UPDATE, ROUTE_REST_DELETE,
    ROUTE_REST_GET, ROUTE_REST_PATCH, ROUTE_REST_POST,
};

/// One supported webhook trigger type for documentation and admin UIs.
#[derive(Debug, Clone, Serialize)]
pub struct GatewayWebhookEventDescriptor {
    pub route_key: &'static str,
    /// `gateway`, `rest`, or `schema`.
    pub category: &'static str,
    pub title: &'static str,
    pub description: &'static str,
    /// When true, setting `table_name` on the webhook narrows dispatch to matching triggers that carry that table.
    pub table_scope_supported: bool,
}

fn ev(
    route_key: &'static str,
    category: &'static str,
    title: &'static str,
    description: &'static str,
    table_scope_supported: bool,
) -> GatewayWebhookEventDescriptor {
    GatewayWebhookEventDescriptor {
        route_key,
        category,
        title,
        description,
        table_scope_supported,
    }
}

/// All `route_key` values allowed by `gateway_webhook_route_key_check`, with human-readable copy.
pub fn gateway_webhook_event_catalog() -> Vec<GatewayWebhookEventDescriptor> {
    vec![
        ev(
            ROUTE_GATEWAY_DATA,
            "gateway",
            "Gateway data",
            "After a successful POST /data mirrored read.",
            true,
        ),
        ev(
            ROUTE_GATEWAY_FETCH,
            "gateway",
            "Gateway fetch",
            "After a successful POST /gateway/fetch.",
            true,
        ),
        ev(
            ROUTE_GATEWAY_UPDATE,
            "gateway",
            "Gateway update",
            "After a successful POST /gateway/update.",
            true,
        ),
        ev(
            ROUTE_GATEWAY_QUERY,
            "gateway",
            "Gateway query",
            "After a successful POST /query/sql or GET /query/sql.",
            false,
        ),
        ev(
            ROUTE_GATEWAY_INSERT,
            "gateway",
            "Gateway insert",
            "After a successful PUT /gateway/insert.",
            true,
        ),
        ev(
            ROUTE_GATEWAY_DELETE,
            "gateway",
            "Gateway delete",
            "After a successful POST /gateway/delete.",
            true,
        ),
        ev(
            ROUTE_BILLING_DOCUMENT_UPSERTED,
            "billing",
            "Billing document upserted",
            "After a provider webhook is verified, projected into a canonical billing document, and upserted into a billing.billing_* table.",
            true,
        ),
        ev(
            ROUTE_REST_GET,
            "rest",
            "REST GET",
            "After a successful GET /rest/v1/{table}.",
            true,
        ),
        ev(
            ROUTE_REST_POST,
            "rest",
            "REST POST",
            "After a successful POST /rest/v1/{table}.",
            true,
        ),
        ev(
            ROUTE_REST_PATCH,
            "rest",
            "REST PATCH",
            "After a successful PATCH /rest/v1/{table}.",
            true,
        ),
        ev(
            ROUTE_REST_DELETE,
            "rest",
            "REST DELETE",
            "After a successful DELETE /rest/v1/{table}.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_TABLE_CREATED,
            "schema",
            "Table created",
            "After successful management API create table.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_TABLE_DROPPED,
            "schema",
            "Table dropped",
            "After successful management API drop table.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_VIEW_CREATED,
            "schema",
            "View created",
            "After successful management API create view.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_VIEW_DROPPED,
            "schema",
            "View dropped",
            "After successful management API drop view.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_COLUMN_ADDED,
            "schema",
            "Column added",
            "After successful management API adds a column.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_COLUMN_DROPPED,
            "schema",
            "Column dropped",
            "After successful management API drops a column.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_COLUMN_RENAMED,
            "schema",
            "Column renamed",
            "After successful management API renames a column.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_COLUMN_ALTERED,
            "schema",
            "Column altered",
            "After successful management API alters a column (type, nullability, default, etc.).",
            true,
        ),
        ev(
            ROUTE_SCHEMA_INDEX_CREATED,
            "schema",
            "Index created",
            "After successful management API create index.",
            true,
        ),
        ev(
            ROUTE_SCHEMA_INDEX_DROPPED,
            "schema",
            "Index dropped",
            "After successful management API drop index.",
            false,
        ),
        ev(
            ROUTE_SCHEMA_EXTENSION_INSTALLED,
            "schema",
            "Extension installed",
            "After successful management API installs a Postgres extension.",
            false,
        ),
    ]
}
