-- 024_webhooks.sql
-- Per-license outbound webhook registry.
-- Operators register URLs + event subscriptions; the backend POSTs signed
-- JSON payloads on matching events (player_banned, server_down, …).

CREATE TABLE webhooks (
    id               uuid         NOT NULL DEFAULT uuid_generate_v4(),
    license_id       uuid         NOT NULL REFERENCES licenses(id) ON DELETE CASCADE,
    name             varchar(100) NOT NULL,
    url              text         NOT NULL,
    -- Comma-separated event keys, e.g. 'player_banned,server_down'
    -- TypeORM simple-array maps this transparently to string[].
    events           text         NOT NULL,
    -- HMAC-SHA256 signing secret; generated server-side if omitted on create.
    secret           varchar(128) NOT NULL,
    is_active        boolean      NOT NULL DEFAULT true,
    -- Populated after each delivery attempt.
    last_delivery_at timestamptz  NULL,
    -- 'ok' | 'failed' — last HTTP delivery outcome.
    last_status      varchar(20)  NULL,
    created_at       timestamptz  NOT NULL DEFAULT now(),

    CONSTRAINT webhooks_pkey PRIMARY KEY (id)
);

CREATE INDEX idx_webhooks_license_id ON webhooks (license_id);
