BEGIN;

CREATE TABLE voicelog_guilds(
  guildid BIGINT PRIMARY KEY,
  webhook_url TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  _timestamp TIMESTAMPTZ DEFAULT NOW()
);

CREATE TRIGGER voicelog_guilds_timestamp BEFORE UPDATE ON voicelog_guilds 
  FOR EACH ROW EXECUTE FUNCTION update_timestamp_column();

CREATE TABLE voicelog_sessions(
  sessionid INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  guildid BIGINT NOT NULL REFERENCES voicelog_guilds (guildid) ON DELETE CASCADE ON UPDATE CASCADE,
  userid BIGINT NOT NULL,
  channelid BIGINT NOT NULL,
  joined_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  duration INTEGER
);

CREATE INDEX voicelog_sessions_guildid_userid ON voicelog_sessions (guildid, userid);

COMMIT;
