-- e2e_tests: integration tests that verify function composition within an app
CREATE TABLE e2e_tests (
    id            TEXT PRIMARY KEY,
    name          TEXT NOT NULL,
    description   TEXT NOT NULL DEFAULT '',
    relation_id   TEXT DEFAULT '' REFERENCES relations(id),
    steps         TEXT NOT NULL DEFAULT '[]',
    input_fixture TEXT NOT NULL DEFAULT '{}',
    expected      TEXT NOT NULL DEFAULT '{}',
    last_status   TEXT NOT NULL DEFAULT '',
    last_run_at   TEXT NOT NULL DEFAULT '',
    execution_id  TEXT NOT NULL DEFAULT '',
    duration_ms   INTEGER NOT NULL DEFAULT 0,
    created_at    TEXT NOT NULL,
    updated_at    TEXT NOT NULL
);

CREATE INDEX idx_e2e_tests_relation ON e2e_tests(relation_id);
CREATE INDEX idx_e2e_tests_status ON e2e_tests(last_status);

CREATE VIRTUAL TABLE e2e_tests_fts USING fts5(
    id, name, description, steps,
    content='e2e_tests',
    content_rowid='rowid'
);

CREATE TRIGGER e2e_tests_ai AFTER INSERT ON e2e_tests BEGIN
    INSERT INTO e2e_tests_fts(rowid, id, name, description, steps)
    VALUES (new.rowid, new.id, new.name, new.description, new.steps);
END;

CREATE TRIGGER e2e_tests_ad AFTER DELETE ON e2e_tests BEGIN
    INSERT INTO e2e_tests_fts(e2e_tests_fts, rowid, id, name, description, steps)
    VALUES ('delete', old.rowid, old.id, old.name, old.description, old.steps);
END;

CREATE TRIGGER e2e_tests_au AFTER UPDATE ON e2e_tests BEGIN
    INSERT INTO e2e_tests_fts(e2e_tests_fts, rowid, id, name, description, steps)
    VALUES ('delete', old.rowid, old.id, old.name, old.description, old.steps);
    INSERT INTO e2e_tests_fts(rowid, id, name, description, steps)
    VALUES (new.rowid, new.id, new.name, new.description, new.steps);
END;
