-- Brain service schema — PostgreSQL + pgvector
-- This is a reference migration. Tables are auto-created by SQLAlchemy on startup.

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE IF NOT EXISTS items (
    id UUID PRIMARY KEY,
    user_id VARCHAR(64) NOT NULL,
    type VARCHAR(32) NOT NULL DEFAULT 'link',
    title TEXT,
    url TEXT,
    raw_content TEXT,
    extracted_text TEXT,
    folder VARCHAR(64),
    tags TEXT[],
    summary TEXT,
    confidence FLOAT,
    metadata_json JSONB DEFAULT '{}',
    processing_status VARCHAR(32) NOT NULL DEFAULT 'pending',
    processing_error TEXT,
    embedding vector(1536),
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS ix_items_user_status ON items(user_id, processing_status);
CREATE INDEX IF NOT EXISTS ix_items_user_folder ON items(user_id, folder);
CREATE INDEX IF NOT EXISTS ix_items_created ON items(created_at);

-- HNSW index for fast approximate nearest neighbor search
CREATE INDEX IF NOT EXISTS ix_items_embedding ON items
    USING hnsw (embedding vector_cosine_ops)
    WITH (m = 16, ef_construction = 64);

CREATE TABLE IF NOT EXISTS item_assets (
    id UUID PRIMARY KEY,
    item_id UUID NOT NULL REFERENCES items(id) ON DELETE CASCADE,
    asset_type VARCHAR(32) NOT NULL,
    filename VARCHAR(512) NOT NULL,
    content_type VARCHAR(128),
    size_bytes INTEGER,
    storage_path VARCHAR(1024) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS ix_item_assets_item ON item_assets(item_id);

CREATE TABLE IF NOT EXISTS app_links (
    id UUID PRIMARY KEY,
    item_id UUID NOT NULL REFERENCES items(id) ON DELETE CASCADE,
    app VARCHAR(64) NOT NULL,
    app_entity_id VARCHAR(128) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS ix_app_links_item ON app_links(item_id);
