\set previous_version 'v1.15.0-ee'
\set next_version 'v1.16.0-ee'
SELECT openreplay_version()                       AS current_version,
       openreplay_version() = :'previous_version' AS valid_previous,
       openreplay_version() = :'next_version'     AS is_next
\gset

\if :valid_previous
\echo valid previous DB version :'previous_version', starting DB upgrade to :'next_version'
BEGIN;
SELECT format($fn_def$
CREATE OR REPLACE FUNCTION openreplay_version()
    RETURNS text AS
$$
SELECT '%1$s'
$$ LANGUAGE sql IMMUTABLE;
$fn_def$, :'next_version')
\gexec

--

CREATE TYPE ui_tests_status AS ENUM ('preview', 'in-progress', 'paused', 'closed');

CREATE TABLE IF NOT EXISTS ut_tests
(
    test_id            integer generated BY DEFAULT AS IDENTITY PRIMARY KEY,
    project_id         integer                     NOT NULL REFERENCES public.projects (project_id) ON DELETE CASCADE,
    title              VARCHAR(255)                NOT NULL,
    starting_path      VARCHAR(255)                NULL,
    status             VARCHAR(20)                 NOT NULL CHECK (status IN ('preview', 'in-progress', 'paused', 'closed')),
    require_mic        BOOLEAN                              DEFAULT FALSE,
    require_camera     BOOLEAN                              DEFAULT FALSE,
    description        TEXT                        NULL,
    guidelines         TEXT                        NULL,
    conclusion_message TEXT                        NULL,
    created_by         integer                     REFERENCES public.users (user_id) ON DELETE SET NULL,
    updated_by         integer                     REFERENCES public.users (user_id) ON DELETE SET NULL,
    visibility         BOOLEAN                              DEFAULT FALSE,
    created_at         timestamp without time zone NOT NULL DEFAULT timezone('utc'::text, now()),
    updated_at         timestamp without time zone NOT NULL DEFAULT timezone('utc'::text, now()),
    deleted_at         timestamp without time zone NULL     DEFAULT NULL
);

CREATE TABLE IF NOT EXISTS ut_tests_tasks
(
    task_id      integer generated BY DEFAULT AS IDENTITY PRIMARY KEY,
    test_id      integer      NOT NULL REFERENCES ut_tests (test_id) ON DELETE CASCADE,
    title        VARCHAR(255) NOT NULL,
    description  TEXT         NOT NULL,
    allow_typing BOOLEAN DEFAULT FALSE,
    FOREIGN KEY (test_id) REFERENCES ut_tests (test_id)
);

CREATE TABLE IF NOT EXISTS ut_tests_signals
(
    session_id BIGINT      NOT NULL,
    test_id    BIGINT      NOT NULL,
    task_id    BIGINT      NULL,
    status     VARCHAR(20) NOT NULL CHECK (status IN ('begin', 'done', 'skipped')),
    comment    TEXT        NULL,
    timestamp  BIGINT      NOT NULL,
    duration   BIGINT      NULL,
    PRIMARY KEY (session_id, test_id, status, timestamp)
);

CREATE TABLE IF NOT EXISTS events.canvas_recordings
(
    session_id   bigint NOT NULL REFERENCES public.sessions (session_id) ON DELETE CASCADE,
    recording_id text   NOT NULL,
    timestamp    bigint NOT NULL
);
CREATE INDEX IF NOT EXISTS canvas_recordings_session_id_idx ON events.canvas_recordings (session_id);

DROP SCHEMA IF EXISTS backup_v1_10_0 CASCADE;

UPDATE metrics
SET default_config='{
  "col": 4,
  "row": 2,
  "position": 0
}'::jsonb
WHERE metric_type = 'pathAnalysis';

COMMIT;

\elif :is_next
\echo new version detected :'next_version', nothing to do
\else
\warn skipping DB upgrade of :'next_version', expected previous version :'previous_version', found :'current_version'
\endif