-- Runtime DDL for gateway admission limiter event persistence (logging DB).
CREATE TABLE IF NOT EXISTS public.gateway_admission_events (
    id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    created_at timestamptz NOT NULL DEFAULT now(),
    event_id text NOT NULL,
    method text NOT NULL,
    route text NOT NULL,
    client_name text,
    request_bytes bigint,
    defer_requested boolean NOT NULL DEFAULT false,
    is_query_route boolean NOT NULL DEFAULT false,
    decision text NOT NULL,
    reason text,
    deferred_request_id text,
    retry_after_seconds integer,
    window_seconds integer,
    global_limit_per_window bigint,
    per_client_limit_per_window bigint,
    meta jsonb NOT NULL DEFAULT '{}'::jsonb
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_gateway_admission_events_event_id
    ON public.gateway_admission_events(event_id);

CREATE INDEX IF NOT EXISTS idx_gateway_admission_events_created_at
    ON public.gateway_admission_events(created_at DESC);

CREATE INDEX IF NOT EXISTS idx_gateway_admission_events_decision
    ON public.gateway_admission_events(decision);

CREATE INDEX IF NOT EXISTS idx_gateway_admission_events_client_name
    ON public.gateway_admission_events(client_name);
