-- Add migration script here

CREATE EXTENSION IF NOT EXISTS pg_trgm;

CREATE TABLE actor (
  did VARCHAR PRIMARY KEY,
  handle VARCHAR UNIQUE,
  indexed_at VARCHAR NOT NULL
);
CREATE INDEX actor_handle_trgm_idx ON actor USING gist (handle gist_trgm_ops);

CREATE TABLE session (
  uri VARCHAR PRIMARY KEY,
  cid VARCHAR NOT NULL,
  owner VARCHAR NOT NULL,

  content VARCHAR NOT NULL,
  contentcid VARCHAR NOT NULL,
  label VARCHAR,
  -- Participants in participant

  created_at VARCHAR NOT NULL,
  indexed_at VARCHAR NOT NULL,
  sort_at VARCHAR GENERATED ALWAYS AS (LEAST(created_at,indexed_at)) STORED NOT NULL
);

CREATE TABLE activity (
  uri VARCHAR PRIMARY KEY,
  cid VARCHAR NOT NULL,

  session VARCHAR,
  sessioncid VARCHAR,
  -- Progress in progress

  performed_at VARCHAR,
  created_at VARCHAR NOT NULL,
  indexed_at VARCHAR NOT NULL,
  sort_at VARCHAR GENERATED ALWAYS AS (LEAST(created_at,indexed_at)) STORED NOT NULL
);

CREATE TABLE participant (
  participantdid VARCHAR NOT NULL,
  session VARCHAR NOT NULL,
  role VARCHAR NOT NULL
);
