-- Add documentation fields to functions and types.
-- examples: extracted code blocks from ## Ejemplo
-- notes: extracted text from ## Notas
-- documentation: remaining body text from .md
-- code: source code from the referenced .go/.py/.tsx file

ALTER TABLE functions ADD COLUMN notes         TEXT NOT NULL DEFAULT '';
ALTER TABLE functions ADD COLUMN documentation  TEXT NOT NULL DEFAULT '';
ALTER TABLE functions ADD COLUMN code           TEXT NOT NULL DEFAULT '';

ALTER TABLE types ADD COLUMN examples      TEXT NOT NULL DEFAULT '';
ALTER TABLE types ADD COLUMN notes         TEXT NOT NULL DEFAULT '';
ALTER TABLE types ADD COLUMN documentation TEXT NOT NULL DEFAULT '';
ALTER TABLE types ADD COLUMN code          TEXT NOT NULL DEFAULT '';

-- Rebuild FTS for functions: add examples, notes, documentation, code
DROP TRIGGER IF EXISTS functions_ai;
DROP TRIGGER IF EXISTS functions_ad;
DROP TRIGGER IF EXISTS functions_au;

INSERT INTO functions_fts(functions_fts) VALUES('rebuild');
DROP TABLE IF EXISTS functions_fts;

CREATE VIRTUAL TABLE functions_fts USING fts5(
    id,
    name,
    description,
    tags,
    signature,
    domain,
    example,
    notes,
    documentation,
    code,
    content='functions',
    content_rowid='rowid'
);

-- Populate FTS from existing data
INSERT INTO functions_fts(rowid, id, name, description, tags, signature, domain, example, notes, documentation, code)
SELECT rowid, id, name, description, tags, signature, domain, example, notes, documentation, code
FROM functions;

CREATE TRIGGER functions_ai AFTER INSERT ON functions BEGIN
    INSERT INTO functions_fts(rowid, id, name, description, tags, signature, domain, example, notes, documentation, code)
    VALUES (new.rowid, new.id, new.name, new.description, new.tags, new.signature, new.domain, new.example, new.notes, new.documentation, new.code);
END;

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

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

-- Rebuild FTS for types: add examples, notes, documentation, code
DROP TRIGGER IF EXISTS types_ai;
DROP TRIGGER IF EXISTS types_ad;
DROP TRIGGER IF EXISTS types_au;

INSERT INTO types_fts(types_fts) VALUES('rebuild');
DROP TABLE IF EXISTS types_fts;

CREATE VIRTUAL TABLE types_fts USING fts5(
    id,
    name,
    description,
    tags,
    domain,
    examples,
    notes,
    documentation,
    code,
    content='types',
    content_rowid='rowid'
);

-- Populate FTS from existing data
INSERT INTO types_fts(rowid, id, name, description, tags, domain, examples, notes, documentation, code)
SELECT rowid, id, name, description, tags, domain, examples, notes, documentation, code
FROM types;

CREATE TRIGGER types_ai AFTER INSERT ON types BEGIN
    INSERT INTO types_fts(rowid, id, name, description, tags, domain, examples, notes, documentation, code)
    VALUES (new.rowid, new.id, new.name, new.description, new.tags, new.domain, new.examples, new.notes, new.documentation, new.code);
END;

CREATE TRIGGER types_ad AFTER DELETE ON types BEGIN
    INSERT INTO types_fts(types_fts, rowid, id, name, description, tags, domain, examples, notes, documentation, code)
    VALUES ('delete', old.rowid, old.id, old.name, old.description, old.tags, old.domain, old.examples, old.notes, old.documentation, old.code);
END;

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