-- Gateway request log table stores HTTP metadata for each proxied request.
CREATE TABLE
    IF NOT EXISTS gateway_request_log (
        id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
        created_at timestamptz NOT NULL DEFAULT now (),
        request_id text NOT NULL,
        client text,
        method text,
        path text,
        query_string text,
        status_code integer,
        ipv4 text,
        user_agent text,
        headers jsonb,
        body jsonb,
        user_id text,
        company_id text,
        organization_id text,
        api_key_id text,
        presented_api_key_public_id text,
        presented_api_key_hash text,
        presented_api_key_salt text,
        api_key_authenticated boolean DEFAULT false,
        api_key_authorized boolean DEFAULT false,
        api_key_enforced boolean DEFAULT false,
        api_key_auth_reason text,
        host text,
        cached boolean,
        duration_ms bigint,
        cache_lookup_outcome text,
        cache_source text,
        operation text,
        table_name text,
        time numeric NOT NULL,
        PRIMARY KEY (id)
    );

-- Gateway operation log captures insert/fetch/update/delete/query/pipeline events.
CREATE TABLE
    IF NOT EXISTS gateway_operation_log (
        id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
        created_at timestamptz NOT NULL DEFAULT now (),
        request_id text NOT NULL,
        operation text,
        table_name text,
        client text,
        method text,
        path text,
        status_code integer,
        duration_ms bigint,
        details jsonb,
        time numeric NOT NULL,
        error boolean DEFAULT false,
        message text,
        cache_key text,
        cache_lookup_outcome text,
        cache_source text,
        cached boolean,
        PRIMARY KEY (id)
    );

-- API key auth logging lives in sql/api_keys.sql to avoid drift with the
-- auth-store schema. Keep gateway_logs.sql focused on request/operation logs.