CREATE TABLE issue_deltas (
    id INTEGER NOT NULL PRIMARY KEY,
    change_id INTEGER NOT NULL,
    new INTEGER,
    issue_id INTEGER NOT NULL,
    issue_status_id INTEGER,
    title VARCHAR(1024),
    FOREIGN KEY(change_id) REFERENCES changes(id) ON DELETE CASCADE,
    FOREIGN KEY(issue_id) REFERENCES issues(id) ON DELETE CASCADE
);

CREATE INDEX
    issue_deltas_issue_id_issue_status_id
ON
    issue_deltas(issue_id,issue_status_id)
;


CREATE TRIGGER
    issue_deltas_ai_1
AFTER INSERT ON
    issue_deltas
FOR EACH ROW
BEGIN

    SELECT debug(
        NEW.change_id,
        NEW.issue_id,
        NEW.issue_status_id,
        NEW.title
    );

    UPDATE
        changes
    SET
        ucount = ucount + 1
    WHERE
        id = NEW.change_id
    ;

    UPDATE
        topics
    SET
        last_change_id = NEW.change_id
    WHERE
        id = NEW.issue_id
    ;

    UPDATE
        changes_pending
    SET
        terms = terms || (
            SELECT
                '-' || x'0A'
                || CASE WHEN
                    NEW.new
                THEN
                    '  _: issue' || x'0A'
                    || '  issue_status_uuid: '
                    || COALESCE(s.uuid, '~') || x'0A'
                ELSE
                    '  _: issue_delta' || x'0A'
                    || '  issue_status_uuid: '
                    || COALESCE(s.uuid, '~') || x'0A'
                    || '  issue_uuid: ' || t.uuid || x'0A'
                END
                || CASE WHEN
                    NEW.new
                THEN
                    '  src_uuid: ' || COALESCE(src.uuid,'~') || x'0A'
                ELSE
                    ''
                END
                || CASE WHEN
                    instr(NEW.title, ' ')
                THEN
                    '  title: ''' || NEW.title || '''' || x'0A'
                ELSE
                    '  title: ' 
                    || COALESCE(NEW.title,'~') || x'0A'
                END
                || CASE WHEN
                    NEW.new
                THEN
                    '  topic_uuid: ' || t.uuid || x'0A'
                ELSE
                    ''
                END
            FROM
                topics t
            INNER JOIN
                issues i
            ON
                i.id = t.id
            LEFT JOIN
                topics s
            ON
                s.id = NEW.issue_status_id
            LEFT JOIN
                topics src
            ON
                src.id = i.src_id
            WHERE
                t.id = NEW.issue_id
        )
    WHERE
        change_id = NEW.change_id
    ;

    INSERT OR IGNORE INTO
        issues_tomerge(issue_id)
    VALUES
        (NEW.issue_id)
    ;

    UPDATE
        issues_tomerge
    SET
        title = title + (NEW.title IS NOT NULL),
        issue_status_id = issue_status_id + (NEW.issue_status_id IS NOT NULL)
    WHERE
        issue_id = NEW.issue_id
    ;


    INSERT INTO
        project_related_changes(
            project_id,
            real_project_id,
            change_id
        )
    SELECT -- This issue's project
        i.project_id,
        i.project_id,
        NEW.change_id
    FROM
        issues i
    WHERE
        i.id = NEW.issue_id
    UNION SELECT -- issues that this issue was pushed to
        i.project_id, 
        i2.project_id,
        NEW.change_id
    FROM
        issues i
    INNER JOIN
        issues i2
    ON
        i2.src_id = i.id
    WHERE
        i.id = NEW.issue_id
    UNION SELECT -- issue that pushed to this issue
        i.project_id,
        i2.project_id, 
        NEW.change_id
    FROM
        issues i
    INNER JOIN
        issues i2
    ON
        i2.id = i.src_id
    WHERE
        i.id = NEW.issue_id
    ;

END;


CREATE TRIGGER
    issue_deltas_ad_1
AFTER DELETE ON
    issue_deltas
FOR EACH ROW
BEGIN

    SELECT debug(
        OLD.change_id,
        OLD.issue_id,
        OLD.issue_status_id,
        OLD.title
    );

    INSERT OR IGNORE INTO
        issues_tomerge(issue_id)
    VALUES
        (OLD.issue_id)
    ;

    UPDATE
        issues_tomerge
    SET
        title    = title + (OLD.title IS NOT NULL),
        issue_status_id = issue_status_id + (OLD.issue_status_id IS NOT NULL)
    WHERE
        issue_id = OLD.issue_id
    ;

END;
