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

CREATE TABLE actors (
    actor_type VARCHAR(1000) NOT NULL,
    id VARCHAR(2083) NOT NULL PRIMARY KEY UNIQUE,
    inbox VARCHAR(2083) NOT NULL,
    outbox VARCHAR(2083) NOT NULL,
    followers VARCHAR(2083) NOT NULL,
    following VARCHAR(2083) NOT NULL,
    url VARCHAR(2083) NOT NULL,
    preferredusername VARCHAR(1000) NOT NULL,
    name VARCHAR(1000) NOT NULL,
    summary TEXT,
    icon VARCHAR(2083),
    image VARCHAR(2083),
    publickey TEXT,
    identifiedat TIMESTAMP with time zone DEFAULT now(),
    instance VARCHAR(1000) NOT NULL
);

CREATE TABLE activities (
    id VARCHAR(2083) NOT NULL PRIMARY KEY UNIQUE,
    inreplyto VARCHAR(2083),
    published TIMESTAMP with time zone NOT NULL,
    summary TEXT,
    content TEXT,
    normalized TEXT,
    attributedto VARCHAR(2083) REFERENCES actors,
    received_at TIMESTAMP with time zone DEFAULT now(),
    instance VARCHAR(1000) NOT NULL
);

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