-- 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);

-- Clusters --

create table if not exists clusters (
    id uuid not null primary key default uuidv7(),
    host text not null unique,
    username text not null default 'phono',
    password text not null
);

-- Workspaces --

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

create table if not exists workspace_memberships (
    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,
    unique (workspace_id, user_id)
);
create index on workspace_memberships (user_id);
create index on workspace_memberships (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,
    form_public boolean not null default false,
    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,
    ordinality float not null
);

-- Service Credentials --

create table if not exists service_creds (
    id uuid not null primary key default uuidv7(),
    cluster_id uuid not null references clusters(id) on delete restrict,
    owner_id uuid not null references users(id) on delete cascade,
    rolname text not null,
    password text not null,
    unique (cluster_id, rolname)
);
create index on service_creds (cluster_id);
create index on service_creds (owner_id);

-- 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);

create table if not exists form_sessions (
    id uuid not null primary key default uuidv7(),
    user_id uuid references users(id) on delete cascade
);

create table if not exists form_touch_points (
    id uuid not null primary key default uuidv7(),
    -- `on delete restrict` errs on the side of conservatism, but is not known
    -- to be crucial.
    form_session_id uuid not null references form_sessions(id) on delete restrict,
    -- `on delete restrict` errs on the side of conservatism, but is not known
    -- to be crucial.
    portal_id uuid not null references portals(id) on delete restrict,
    -- Points to a row in the portal's backing table, so foreign key constraints
    -- do not apply here.
    row_id uuid not null
);
