-- OpenBrain — Database Initialization
-- Runs once on first container start

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

CREATE TABLE IF NOT EXISTS thoughts (
    id          UUID        PRIMARY KEY DEFAULT gen_random_uuid(),
    content     TEXT        NOT NULL,
    embedding   vector(1024),           -- bge-m3 native dimension
    source      VARCHAR(100)            NOT NULL DEFAULT 'unknown',
    metadata    JSONB                   NOT NULL DEFAULT '{}',
    created_at  TIMESTAMPTZ             NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMPTZ             NOT NULL DEFAULT NOW()
);

-- Vector similarity search index (cosine)
CREATE INDEX IF NOT EXISTS thoughts_embedding_idx
    ON thoughts USING ivfflat (embedding vector_cosine_ops)
    WITH (lists = 100);

-- Recent queries
CREATE INDEX IF NOT EXISTS thoughts_created_at_idx
    ON thoughts (created_at DESC);

-- Filter by source
CREATE INDEX IF NOT EXISTS thoughts_source_idx
    ON thoughts (source);
