-- Users --

create table if not exists users (
    id uuid not null primary key default uuidv7(),
    uid text unique not null,
    email text not null
);
create index on users (uid);

-- async_session Browser Sessions --

create table if not exists browser_sessions (
    id text not null primary key,
    serialized text not null,
    created_at timestamptz not null default now(),
    expiry timestamptz
);
create index on browser_sessions (expiry);
create index on browser_sessions (created_at);

-- Workspaces --

create table if not exists workspaces (
    id uuid not null primary key default uuidv7(),
    name text not null default '',
    url text not null,
    owner_id uuid not null references users(id) on delete restrict
);
create index on workspaces (owner_id);

create table if not exists workspace_user_perms (
    id uuid not null primary key default uuidv7(),
    workspace_id uuid not null references workspaces(id) on delete cascade,
    user_id uuid not null references users(id) on delete cascade,
    perm text not null,
    unique (workspace_id, user_id, perm)
);
create index on workspace_user_perms (user_id);
create index on workspace_user_perms (workspace_id);

-- Relation Invitations --

create table if not exists rel_invitations (
    id uuid not null primary key default uuidv7(),
    email text not null,
    workspace_id uuid not null references workspaces(id) on delete cascade,
    class_oid oid not null,
    created_by uuid not null references users(id) on delete restrict,
    privilege text not null,
    expires_at timestamptz,
    unique (email, workspace_id, class_oid, privilege)
);
create index on rel_invitations (workspace_id, class_oid);
create index on rel_invitations (email);

-- Portals --

create table if not exists portals (
    id uuid not null primary key default uuidv7(),
    name text not null,
    workspace_id uuid not null references workspaces(id) on delete cascade,
    class_oid oid not null,
    table_filter jsonb not null default 'null',
    table_order_by jsonb not null default '[]'
);
create index on portals (workspace_id);

create table if not exists fields (
    id uuid not null primary key default uuidv7(),
    portal_id uuid not null references portals(id) on delete cascade,
    name text not null,
    presentation jsonb not null,
    table_label text,
    table_width_px int not null default 200
);

-- Forms --

create table if not exists form_transitions (
    id uuid not null primary key default uuidv7(),
    source_id uuid not null references portals(id) on delete cascade,
    dest_id uuid not null references portals(id) on delete restrict,
    condition jsonb not null default 'null'
);
create index on form_transitions (source_id);

create table if not exists field_form_prompts (
    id uuid not null primary key default uuidv7(),
    field_id uuid not null references fields(id) on delete cascade,
    language text not null,
    content text not null default '',
    unique (field_id, language)
);
create index on field_form_prompts (field_id);
