-- Platform environment_settings table for setup state and runtime config overrides.
-- Used by SetupStateDetector to determine if setup wizard has been completed.
-- This is idempotent and safe to run on new compose databases.

CREATE SCHEMA IF NOT EXISTS platform;

CREATE TABLE IF NOT EXISTS platform.environment_settings (
    key         VARCHAR(256) NOT NULL,
    value       TEXT         NOT NULL,
    tenant_id   VARCHAR(128) NOT NULL DEFAULT '_system',
    updated_by  VARCHAR(256) NOT NULL DEFAULT 'system',
    updated_at  TIMESTAMPTZ  NOT NULL DEFAULT now(),
    PRIMARY KEY (tenant_id, key)
);

-- Mark setup as complete for fresh installs (docker-compose local dev).
-- The setup wizard can re-run and overwrite this if needed.
INSERT INTO platform.environment_settings (key, value, tenant_id, updated_by)
VALUES ('SetupComplete', 'true', '_system', 'postgres-init')
ON CONFLICT (tenant_id, key) DO NOTHING;
