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 widgets
(
    widget_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_template boolean   NOT NULL DEFAULT FALSE,
    predefined  boolean   NOT NULL DEFAULT FALSE,
    created_at  timestamp NOT NULL DEFAULT timezone('utc'::text, now()),
    deleted_at  timestamp NULL     DEFAULT NULL
);

CREATE TABLE dashboard_widgets
(
    dashboard_id  integer   NOT NULL REFERENCES dashboards (dashboard_id) ON DELETE CASCADE,
    widget_id     integer   NOT NULL REFERENCES widgets (widget_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
);

COMMIT;