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


CREATE TABLE IF NOT EXISTS 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
);


ALTER TABLE IF EXISTS metrics
    DROP CONSTRAINT IF EXISTS null_project_id_for_template_only,
    DROP CONSTRAINT IF EXISTS unique_key;

ALTER TABLE IF EXISTS metrics
    ADD COLUMN IF NOT EXISTS edited_at     timestamp NULL     DEFAULT NULL,
    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) ),
    ADD CONSTRAINT unique_key UNIQUE (key);



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 metrics (name, category, config, is_predefined, is_template, is_public, key)
VALUES ('sessions count', 'overview', '{}', true, true, true, 'count_sessions'),
       ('avg request load time', 'overview', '{}', true, true, true, 'avg_request_load_time'),
       ('avg page load time', 'overview', '{}', true, true, true, 'avg_page_load_time'),
       ('avg image load time', 'overview', '{}', true, true, true, 'avg_image_load_time'),
       ('avg dom content load start', 'overview', '{}', true, true, true, 'avg_dom_content_load_start'),
       ('avg first contentful pixel', 'overview', '{}', true, true, true, 'avg_first_contentful_pixel'),
       ('avg visited pages count', 'overview', '{}', true, true, true, 'avg_visited_pages'),
       ('avg session duration', 'overview', '{}', true, true, true, 'avg_session_duration'),
       ('avg pages dom build time', 'overview', '{}', true, true, true, 'avg_pages_dom_buildtime'),
       ('avg pages response time', 'overview', '{}', true, true, true, 'avg_pages_response_time'),
       ('avg response time', 'overview', '{}', true, true, true, 'avg_response_time'),
       ('avg first paint', 'overview', '{}', true, true, true, 'avg_first_paint'),
       ('avg dom content loaded', 'overview', '{}', true, true, true, 'avg_dom_content_loaded'),
       ('avg time till first bit', 'overview', '{}', true, true, true, 'avg_till_first_bit'),
       ('avg time to interactive', 'overview', '{}', true, true, true, 'avg_time_to_interactive'),
       ('requests count', 'overview', '{}', true, true, true, 'count_requests'),
       ('avg time to render', 'overview', '{}', true, true, true, 'avg_time_to_render'),
       ('avg used js heap size', 'overview', '{}', true, true, true, 'avg_used_js_heap_size'),
       ('avg cpu', 'overview', '{}', true, true, true, 'avg_cpu')
ON CONFLICT (key) DO UPDATE SET name=excluded.name,
                                category=excluded.category,
                                config=excluded.config,
                                is_predefined=excluded.is_predefined,
                                is_template=excluded.is_template,
                                is_public=excluded.is_public;

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