BEGIN;
CREATE OR REPLACE FUNCTION openreplay_version()
    RETURNS text AS
$$
SELECT 'v1.5.5'
$$ LANGUAGE sql IMMUTABLE;


-- CREATE TABLE dashboards
-- (
--     dashboard_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY,
--     project_id   integer   NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE,
--     user_id      integer   NOT NULL REFERENCES users (user_id) ON DELETE SET NULL,
--     name         text      NOT NULL,
--     is_public    boolean   NOT NULL DEFAULT TRUE,
--     is_pinned    boolean   NOT NULL DEFAULT FALSE,
--     created_at   timestamp NOT NULL DEFAULT timezone('utc'::text, now()),
--     deleted_at   timestamp NULL     DEFAULT NULL
-- );

-- CREATE TABLE templates
-- (
--     template_id  integer generated BY DEFAULT AS IDENTITY PRIMARY KEY,
--     template_key text,
--     name         text  NOT NULL,
--     category     text  NOT NULL,
--     series       jsonb NOT NULL,
--     config       jsonb NOT NULL,
--     predefined   boolean DEFAULT TRUE
-- );

-- CREATE TABLE dashboard_widgets
-- (
--     widget_id     integer generated BY DEFAULT AS IDENTITY PRIMARY KEY,
--     dashboard_id  integer   NOT NULL REFERENCES dashboards (dashboard_id) ON DELETE CASCADE,
--     metric_id     integer   NOT NULL REFERENCES metrics (metric_id) ON DELETE CASCADE,
-- --     template_id   integer   NOT NULL REFERENCES templates (template_id) ON DELETE CASCADE,
--     user_id       integer   NOT NULL REFERENCES users (user_id) ON DELETE SET NULL,
--     created_at    timestamp NOT NULL DEFAULT timezone('utc'::text, now()),
--     configuration jsonb     NOT NULL DEFAULT '{}'::jsonb,
--     name          text
-- );

-- INSERT INTO public.templates (name, category, series, config, predefined, template_key)
-- VALUES ('captured sessions', 'overview', '[]', '{}', true, 'count_sessions'),
--        ('request load time', 'overview', '[]', '{}', true, 'avg_request_load_time'),
--        ('page load time', 'overview', '[]', '{}', true, 'avg_page_load_time'),
--        ('image load time', 'overview', '[]', '{}', true, 'avg_image_load_time');

ALTER TYPE metric_view_type ADD VALUE IF NOT EXISTS 'areaChart';

ALTER TABLE IF EXISTS metrics
    DROP CONSTRAINT IF EXISTS null_project_id_for_template_only;

ALTER TABLE IF EXISTS metrics
    ADD COLUMN IF NOT EXISTS is_pinned     boolean NOT NULL DEFAULT FALSE,
    ADD COLUMN IF NOT EXISTS category      text    NULL     DEFAULT 'custom',
    ADD COLUMN IF NOT EXISTS is_predefined boolean NOT NULL DEFAULT FALSE,
    ADD COLUMN IF NOT EXISTS is_template   boolean NOT NULL DEFAULT FALSE,
    ADD COLUMN IF NOT EXISTS key           text    NULL     DEFAULT NULL,
    ADD COLUMN IF NOT EXISTS config        jsonb   NOT NULL DEFAULT '{}'::jsonb,
    ALTER COLUMN project_id DROP NOT NULL,
    ADD CONSTRAINT null_project_id_for_template_only
        CHECK ( (metrics.category != 'custom') != (metrics.project_id IS NOT NULL) );



CREATE TABLE IF NOT EXISTS dashboard_widgets
(
    widget_id    integer generated BY DEFAULT AS IDENTITY PRIMARY KEY,
    dashboard_id integer   NOT NULL REFERENCES dashboards (dashboard_id) ON DELETE CASCADE,
    metric_id    integer   NOT NULL REFERENCES metrics (metric_id) ON DELETE CASCADE,
    user_id      integer   NOT NULL REFERENCES users (user_id) ON DELETE SET NULL,
    created_at   timestamp NOT NULL DEFAULT timezone('utc'::text, now()),
    config       jsonb     NOT NULL DEFAULT '{}'::jsonb,
    name         text
);

-- INSERT INTO public.templates (name, category, series, config, predefined, template_key)
-- VALUES ('captured sessions', 'overview', '[]', '{}', true, 'count_sessions'),
--        ('request load time', 'overview', '[]', '{}', true, 'avg_request_load_time'),
--        ('page load time', 'overview', '[]', '{}', true, 'avg_page_load_time'),
--        ('image load time', 'overview', '[]', '{}', true, 'avg_image_load_time');

INSERT INTO metrics (name, category, config, is_predefined, is_template, is_public, key)
VALUES ('captured sessions', 'overview', '{}', true, true, true, 'count_sessions'),
       ('request load time', 'overview', '{}', true, true, true, 'avg_request_load_time'),
       ('page load time', 'overview', '{}', true, true, true, 'avg_page_load_time'),
       ('image load time', 'overview', '{}', true, true, true, 'avg_image_load_time');


COMMIT