-- Spore Agent Commons — Initial Schema
-- Requires: pgvector extension

CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Universal entity table
CREATE TABLE IF NOT EXISTS entities (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    rid TEXT UNIQUE NOT NULL,
    kind TEXT NOT NULL,
    status TEXT NOT NULL DEFAULT 'active',
    content JSONB NOT NULL DEFAULT '{}',
    embedding vector(1024),
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_entities_rid ON entities (rid);
CREATE INDEX IF NOT EXISTS idx_entities_kind ON entities (kind);
CREATE INDEX IF NOT EXISTS idx_entities_content ON entities USING GIN (content);

-- Governance specification documents (DAG)
CREATE TABLE IF NOT EXISTS governance_docs (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    doc_id TEXT UNIQUE NOT NULL,
    doc_kind TEXT NOT NULL,
    title TEXT NOT NULL DEFAULT '',
    status TEXT NOT NULL DEFAULT 'draft',
    body TEXT NOT NULL DEFAULT '',
    frontmatter JSONB NOT NULL DEFAULT '{}',
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_governance_docs_kind ON governance_docs (doc_kind);
CREATE INDEX IF NOT EXISTS idx_governance_docs_status ON governance_docs (status);

-- Governance DAG edges (depends_on relationships)
CREATE TABLE IF NOT EXISTS governance_deps (
    from_doc TEXT NOT NULL REFERENCES governance_docs(doc_id) ON DELETE CASCADE,
    to_doc TEXT NOT NULL REFERENCES governance_docs(doc_id) ON DELETE CASCADE,
    PRIMARY KEY (from_doc, to_doc)
);

CREATE INDEX IF NOT EXISTS idx_governance_deps_to ON governance_deps (to_doc);

-- Holons (agents, teams, organizations)
CREATE TABLE IF NOT EXISTS holons (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    rid TEXT UNIQUE NOT NULL,
    slug TEXT UNIQUE NOT NULL,
    name TEXT NOT NULL,
    holon_type TEXT NOT NULL DEFAULT 'agent',
    description TEXT NOT NULL DEFAULT '',
    membrane_config JSONB NOT NULL DEFAULT '{}',
    metadata JSONB NOT NULL DEFAULT '{}',
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_holons_slug ON holons (slug);
CREATE INDEX IF NOT EXISTS idx_holons_type ON holons (holon_type);

-- Immutable append-only event log
CREATE TABLE IF NOT EXISTS events (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    sequence_num BIGSERIAL UNIQUE,
    entity_rid TEXT NOT NULL,
    event_kind TEXT NOT NULL,
    actor_rid TEXT,
    payload JSONB NOT NULL DEFAULT '{}',
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_events_entity ON events (entity_rid);
CREATE INDEX IF NOT EXISTS idx_events_sequence ON events (sequence_num);
CREATE INDEX IF NOT EXISTS idx_events_kind ON events (event_kind);

-- Federation peer nodes
CREATE TABLE IF NOT EXISTS koi_peers (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    node_rid TEXT UNIQUE NOT NULL,
    node_url TEXT NOT NULL,
    trust_tier TEXT NOT NULL DEFAULT 'monitored',
    handshake_status TEXT NOT NULL DEFAULT 'pending',
    config JSONB NOT NULL DEFAULT '{}',
    last_seen_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_koi_peers_trust ON koi_peers (trust_tier);
