CREATE TABLE IF NOT EXISTS gateway_debug_logs (
    id                  BIGSERIAL PRIMARY KEY,

    upstream_request_id TEXT,
    account_id          BIGINT,
    account_email       TEXT,
    account_platform    TEXT,

    event_type          TEXT NOT NULL DEFAULT 'api_call',

    method              TEXT,
    full_url            TEXT,
    request_headers     TEXT,
    request_body        TEXT,
    request_size        INTEGER,

    response_status     INTEGER,
    response_headers    TEXT,
    response_body_preview TEXT,
    response_size       INTEGER,

    model_requested     TEXT,
    model_upstream      TEXT,
    is_stream           BOOLEAN DEFAULT FALSE,
    duration_ms         INTEGER,

    tls_profile         TEXT,

    error_message       TEXT,

    created_at          TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_gdl_account_id ON gateway_debug_logs (account_id);
CREATE INDEX IF NOT EXISTS idx_gdl_created_at ON gateway_debug_logs (created_at);
CREATE INDEX IF NOT EXISTS idx_gdl_event_type ON gateway_debug_logs (event_type);
CREATE INDEX IF NOT EXISTS idx_gdl_model ON gateway_debug_logs (model_requested);
