-- Zentraler Konfigurationsdienst (CFG-01, siehe core-kanban/tickets/CFG-01.md).
-- scope = 'global' fuer globale Defaults, sonst der Tenant-Slug. config_values
-- haelt den AKTUELLEN Stand je (key, scope); config_value_history haelt JEDE
-- Aenderung fest (Akzeptanzkriterium 2: versioniert nachvollziehbar).
CREATE TABLE config_values (
    key        TEXT NOT NULL,
    scope      TEXT NOT NULL CHECK (scope <> ''),
    value      TEXT NOT NULL,
    version    INT NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY (key, scope)
);

CREATE TABLE config_value_history (
    id         UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    key        TEXT NOT NULL,
    scope      TEXT NOT NULL,
    value      TEXT NOT NULL,
    version    INT NOT NULL,
    changed_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX config_value_history_key_scope_idx ON config_value_history (key, scope, version);
