-- Migration number: 0002 	 2024-01-06T01:17:06.949Z
CREATE TABLE IF NOT EXISTS rollableTables
(
	id         INTEGER PRIMARY KEY,
	identifier TEXT    NOT NULL UNIQUE,
	name       TEXT    NOT NULL UNIQUE,
	title      TEXT    NOT NULL UNIQUE,
	emoji      TEXT    NOT NULL UNIQUE,
	header     TEXT    NOT NULL GENERATED ALWAYS AS (emoji || ' ' || title),
	badge      TEXT    NOT NULL GENERATED ALWAYS AS (emoji || ' ' || name),
	ordinal    INTEGER NOT NULL UNIQUE DEFAULT id
) STRICT;

CREATE TRIGGER IF NOT EXISTS responseInserted
	AFTER INSERT
	ON responses
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTables
									 WHERE id = NEW.tableId)
BEGIN
	SELECT RAISE(ABORT, 'rollable table does not exist');
END;

CREATE TRIGGER IF NOT EXISTS responseTableIdUpdated
	AFTER UPDATE OF tableId
	ON responses
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTables
									 WHERE id = NEW.tableId)
BEGIN
	SELECT RAISE(ABORT, 'rollable table does not exist');
END;

CREATE TABLE IF NOT EXISTS rollableTableHeaders
(
	id      INTEGER PRIMARY KEY,
	header  TEXT    NOT NULL UNIQUE,
	tableId INTEGER NOT NULL
	-- FOREIGN KEY REFERENCES rollableTables(id) ON DELETE RESTRICT ON UPDATE CASCADE
) STRICT;

CREATE TRIGGER IF NOT EXISTS rollableTableHeaderInsert
	AFTER INSERT
	ON rollableTableHeaders
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTables
									 WHERE id = NEW.tableId)
BEGIN
	SELECT RAISE(ABORT, 'rollable table does not exist');
END;

CREATE TRIGGER IF NOT EXISTS rollableTableHeaderTableIdUpdate
	AFTER UPDATE OF tableId
	ON rollableTableHeaders
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTables
									 WHERE id = NEW.tableId)
BEGIN
	SELECT RAISE(ABORT, 'rollable table does not exist');
END;

CREATE TABLE IF NOT EXISTS rollableTableBadges
(
	id      INTEGER PRIMARY KEY,
	badge   TEXT    NOT NULL UNIQUE,
	tableId INTEGER NOT NULL
	-- FOREIGN KEY REFERENCES rollableTables(id) ON DELETE RESTRICT ON UPDATE CASCADE
) STRICT;

CREATE TRIGGER IF NOT EXISTS rollableTableBadgeInsert
	AFTER INSERT
	ON rollableTableBadges
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTables
									 WHERE id = NEW.tableId)
BEGIN
	SELECT RAISE(ABORT, 'rollable table does not exist');
END;

CREATE TRIGGER IF NOT EXISTS rollableTableBadgeTableIdUpdate
	AFTER UPDATE OF tableId
	ON rollableTableHeaders
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTables
									 WHERE id = NEW.tableId)
BEGIN
	SELECT RAISE(ABORT, 'rollable table does not exist');
END;

CREATE TABLE IF NOT EXISTS rollableTableIdentifiers
(
	id         INTEGER PRIMARY KEY,
	identifier TEXT    NOT NULL UNIQUE,
	tableId    INTEGER NOT NULL -- FOREIGN KEY REFERENCES rollableTables(id) ON DELETE RESTRICT ON UPDATE CASCADE
) STRICT;

CREATE TRIGGER IF NOT EXISTS rollableTableIdentifierInsert
	AFTER INSERT
	ON rollableTableIdentifiers
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTables
									 WHERE id = NEW.tableId)
BEGIN
	SELECT RAISE(ABORT, 'rollable table does not exist');
END;

CREATE TRIGGER IF NOT EXISTS rollableTableIdentifierTableIdUpdate
	AFTER UPDATE OF tableId
	ON rollableTableIdentifiers
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTables
									 WHERE id = NEW.tableId)
BEGIN
	SELECT RAISE(ABORT, 'rollable table does not exist');
END;

-- rollableTables triggers for Identifiers and Headers

CREATE TRIGGER IF NOT EXISTS rollableTableInsert
	AFTER INSERT
	ON rollableTables
	FOR EACH ROW
BEGIN
	INSERT INTO rollableTableHeaders (header, tableId) VALUES (NEW.header, NEW.id);
	INSERT INTO rollableTableBadges (badge, tableId) VALUES (NEW.badge, NEW.id);
	INSERT INTO rollableTableIdentifiers (identifier, tableId) VALUES (NEW.identifier, NEW.id);
END;

CREATE TRIGGER IF NOT EXISTS rollableTableIdUpdate
	AFTER UPDATE OF id
	ON rollableTables
	FOR EACH ROW
BEGIN
	UPDATE rollableTableHeaders SET tableId = NEW.id WHERE tableId = OLD.id;
	UPDATE rollableTableBadges SET tableId = NEW.id WHERE tableId = OLD.id;
	UPDATE rollableTableIdentifiers SET tableId = NEW.id WHERE tableId = OLD.id;

	UPDATE responses SET tableId = NEW.id WHERE tableId = OLD.id;
END;

CREATE TRIGGER IF NOT EXISTS rollableTableBadgeUpdate
	AFTER UPDATE
	ON rollableTables
	FOR EACH ROW
	WHEN OLD.badge != NEW.badge AND NOT EXISTS (SELECT id
																							FROM rollableTableBadges
																							WHERE badge = NEW.badge
																								AND tableId = NEW.id)
BEGIN
	INSERT INTO rollableTableBadges (badge, tableId)
	VALUES (NEW.badge, NEW.id);
END;

CREATE TRIGGER IF NOT EXISTS rollableTableHeaderUpdate
	AFTER UPDATE
	ON rollableTables
	FOR EACH ROW
	WHEN OLD.header != NEW.header AND NOT EXISTS (SELECT id
																								FROM rollableTableHeaders
																								WHERE header = NEW.header
																									AND tableId = NEW.id)
BEGIN
	INSERT INTO rollableTableHeaders (header, tableId) VALUES (NEW.header, NEW.id);
END;

CREATE TRIGGER IF NOT EXISTS rollableTableIdentifierUpdate
	AFTER UPDATE OF identifier
	ON rollableTables
	FOR EACH ROW
	WHEN NOT EXISTS (SELECT id
									 FROM rollableTableIdentifiers
									 WHERE identifier = NEW.identifier
										 AND tableId = NEW.id)
BEGIN
	INSERT INTO rollableTableIdentifiers (identifier, tableId) VALUES (NEW.identifier, NEW.id);
END;

CREATE TRIGGER IF NOT EXISTS rollableTableDeletedButReferenced
	AFTER DELETE
	ON rollableTables
	FOR EACH ROW
BEGIN
	SELECT RAISE(ABORT, 'rollable table is still referenced')
	FROM (SELECT *
				FROM rollableTableHeaders
				WHERE rollableTableHeaders.tableId = OLD.id
				UNION ALL
				SELECT *
				FROM rollableTableIdentifiers
				WHERE rollableTableIdentifiers.tableId = OLD.id
				UNION ALL
				SELECT *
				FROM rollableTableBadges
				WHERE rollableTableBadges.tableId = OLD.id);
	DELETE FROM responses WHERE tableId = OLD.id;
END;

INSERT INTO rollableTables (id, ordinal, identifier, name, title, emoji)
VALUES (0, 0, 'setting', 'Setting', 'The action takes place...', CHAR(0x1f3d9, 0xfe0f)),
			 (1, 1, 'theme', 'Theme', 'The encounter is themed around...', CHAR(0x1f4d4)),
			 (2, 2, 'start', 'Inciting Incident', 'The action begins when...', CHAR(0x25b6, 0xfe0f)),
			 (3, 3, 'challenge', 'Challenge', 'Things are more difficult because...', CHAR(0x1f613)),
			 (4, 4, 'twist', 'Twist', 'Partway through, unexpectedly...', CHAR(0x1f500)),
			 (5, 5, 'focus', 'Vore Scene Focus', 'The vore scene is focused on...', CHAR(0x1f444)),
			 (6, 6, 'word', 'Word of the Day', 'The word of the day is...', CHAR(0x2728));

CREATE INDEX IF NOT EXISTS responses_table_text ON responses (tableId, text);
