-- API-nøkler: kryptert lagring av tredjepartsnøkler
-- Ref: docs/infra/nøkkelhåndtering.md

CREATE TABLE api_keys (
    id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    provider    TEXT NOT NULL,
    label       TEXT,
    key_encrypted BYTEA NOT NULL,
    key_hint    TEXT,
    is_active   BOOLEAN DEFAULT true,
    created_at  TIMESTAMPTZ DEFAULT now(),
    updated_at  TIMESTAMPTZ DEFAULT now(),
    created_by  UUID REFERENCES nodes(id),
    last_used   TIMESTAMPTZ,
    usage_count BIGINT DEFAULT 0
);

CREATE INDEX idx_api_keys_provider ON api_keys(provider, is_active);

GRANT SELECT ON api_keys TO synops_reader;
