-- Authority Schema (dedicated DB): Bootstrap Fallback.
--
-- Counterpart to 04-authority-schema.sql. Targets the dedicated
-- `stellaops_authority` database created by 00-create-authority-db.sql for
-- OIDC connection-pool isolation.
--
-- AGENTS.md §2.7 / SPRINT_20260422_003 (AUTH-MIGRATE-003): Schema DDL is
-- owned by the Authority service migration runner (embedded SQL in
-- `StellaOps.Authority.Persistence.dll`). This file only creates the
-- `authority` / `authority_app` schemas so the service can connect, and
-- guards a `default` tenant fallback seed.

\c stellaops_authority

CREATE SCHEMA IF NOT EXISTS authority;
CREATE SCHEMA IF NOT EXISTS authority_app;

-- Guarded default tenant seed (no-op if authority.tenants does not yet exist).
DO $$
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.tables
        WHERE table_schema = 'authority'
          AND table_name = 'tenants'
    ) THEN
        INSERT INTO authority.tenants (tenant_id, name, display_name, status)
        VALUES ('default', 'Default', 'Default Tenant', 'active')
        ON CONFLICT (tenant_id) DO NOTHING;
    END IF;
END
$$;
