-- registry schema v1.0.0
-- Functions and types tables with FTS5 search.

CREATE TABLE IF NOT EXISTS functions (
	id               TEXT PRIMARY KEY,
	name             TEXT NOT NULL,
	kind             TEXT NOT NULL CHECK(kind IN ('function','pipeline','component')),
	lang             TEXT NOT NULL,
	domain           TEXT NOT NULL,
	version          TEXT NOT NULL DEFAULT '1.0.0',
	purity           TEXT NOT NULL CHECK(purity IN ('pure','impure')),
	signature        TEXT NOT NULL DEFAULT '',
	description      TEXT NOT NULL,
	tags             TEXT NOT NULL DEFAULT '[]',
	uses_functions   TEXT NOT NULL DEFAULT '[]',
	uses_types       TEXT NOT NULL DEFAULT '[]',
	returns          TEXT NOT NULL DEFAULT '[]',
	returns_optional INTEGER NOT NULL DEFAULT 0,
	error_type       TEXT NOT NULL DEFAULT '',
	imports          TEXT NOT NULL DEFAULT '[]',
	example          TEXT NOT NULL DEFAULT '',
	tested           INTEGER NOT NULL DEFAULT 0,
	tests            TEXT NOT NULL DEFAULT '[]',
	test_file_path   TEXT NOT NULL DEFAULT '',
	file_path        TEXT NOT NULL DEFAULT '',
	created_at       TEXT NOT NULL,
	updated_at       TEXT NOT NULL,
	-- Component fields
	props            TEXT NOT NULL DEFAULT '[]',
	emits            TEXT NOT NULL DEFAULT '[]',
	has_state        INTEGER,
	framework        TEXT NOT NULL DEFAULT '',
	variant          TEXT NOT NULL DEFAULT '[]'
);

CREATE TABLE IF NOT EXISTS types (
	id          TEXT PRIMARY KEY,
	name        TEXT NOT NULL,
	lang        TEXT NOT NULL,
	domain      TEXT NOT NULL,
	version     TEXT NOT NULL DEFAULT '1.0.0',
	algebraic   TEXT NOT NULL CHECK(algebraic IN ('product','sum')),
	definition  TEXT NOT NULL DEFAULT '',
	description TEXT NOT NULL,
	tags        TEXT NOT NULL DEFAULT '[]',
	uses_types  TEXT NOT NULL DEFAULT '[]',
	file_path   TEXT NOT NULL DEFAULT '',
	created_at  TEXT NOT NULL,
	updated_at  TEXT NOT NULL
);

CREATE VIRTUAL TABLE IF NOT EXISTS functions_fts USING fts5(
	id,
	name,
	description,
	tags,
	signature,
	domain,
	content='functions',
	content_rowid='rowid'
);

CREATE VIRTUAL TABLE IF NOT EXISTS types_fts USING fts5(
	id,
	name,
	description,
	tags,
	domain,
	content='types',
	content_rowid='rowid'
);

-- Triggers to keep FTS in sync
CREATE TRIGGER IF NOT EXISTS functions_ai AFTER INSERT ON functions BEGIN
	INSERT INTO functions_fts(rowid, id, name, description, tags, signature, domain)
	VALUES (new.rowid, new.id, new.name, new.description, new.tags, new.signature, new.domain);
END;

CREATE TRIGGER IF NOT EXISTS functions_ad AFTER DELETE ON functions BEGIN
	INSERT INTO functions_fts(functions_fts, rowid, id, name, description, tags, signature, domain)
	VALUES ('delete', old.rowid, old.id, old.name, old.description, old.tags, old.signature, old.domain);
END;

CREATE TRIGGER IF NOT EXISTS functions_au AFTER UPDATE ON functions BEGIN
	INSERT INTO functions_fts(functions_fts, rowid, id, name, description, tags, signature, domain)
	VALUES ('delete', old.rowid, old.id, old.name, old.description, old.tags, old.signature, old.domain);
	INSERT INTO functions_fts(rowid, id, name, description, tags, signature, domain)
	VALUES (new.rowid, new.id, new.name, new.description, new.tags, new.signature, new.domain);
END;

CREATE TRIGGER IF NOT EXISTS types_ai AFTER INSERT ON types BEGIN
	INSERT INTO types_fts(rowid, id, name, description, tags, domain)
	VALUES (new.rowid, new.id, new.name, new.description, new.tags, new.domain);
END;

CREATE TRIGGER IF NOT EXISTS types_ad AFTER DELETE ON types BEGIN
	INSERT INTO types_fts(types_fts, rowid, id, name, description, tags, domain)
	VALUES ('delete', old.rowid, old.id, old.name, old.description, old.tags, old.domain);
END;

CREATE TRIGGER IF NOT EXISTS types_au AFTER UPDATE ON types BEGIN
	INSERT INTO types_fts(types_fts, rowid, id, name, description, tags, domain)
	VALUES ('delete', old.rowid, old.id, old.name, old.description, old.tags, old.domain);
	INSERT INTO types_fts(rowid, id, name, description, tags, domain)
	VALUES (new.rowid, new.id, new.name, new.description, new.tags, new.domain);
END;
