DROP TABLE IF EXISTS activities CASCADE;
DROP TABLE IF EXISTS actors CASCADE;
DROP TABLE IF EXISTS instances CASCADE;

CREATE TABLE actors (
	id SERIAL PRIMARY KEY,
	document JSONB,
    identifiedat TIMESTAMP with time zone DEFAULT now(),
    instance VARCHAR(1000) NOT NULL
);

CREATE UNIQUE INDEX actors_uri ON actors ( (document->'id') );

CREATE TABLE activities (
	id SERIAL PRIMARY KEY,
	document JSONB,
    normalized TEXT,
    instance VARCHAR(1000) NOT NULL
);

CREATE UNIQUE INDEX activities_uri ON activities ( (document->'id') );

CREATE TABLE instances (
    endpoint VARCHAR(2083) NOT NULL PRIMARY KEY UNIQUE,
    autostart BOOLEAN,
    state VARCHAR(16),
    username VARCHAR(32),
    password VARCHAR(32),
    software VARCHAR(50)
);

ALTER TABLE activities ADD COLUMN normalized_idx tsvector;
UPDATE activities SET normalized_idx = to_tsvector('english', normalized);
CREATE INDEX ON activities USING gin(normalized_idx);

CREATE INDEX actors_id_idx ON actors (id);
CREATE INDEX activities_id_idx ON activities (id);
CREATE INDEX actors_uri_idx ON actors ( (document->'id') );
CREATE INDEX activities_uri_idx ON activities ( (document->'id') );
