BEGIN;


INSERT INTO version_history (component, from_version, to_version, author)
VALUES ('SUBATHON', 0, 1, 'Initial Creation');

CREATE TABLE subathons(
  subathon_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  communityid INTEGER NOT NULL REFERENCES communities(communityid),
  started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  initial_time INTEGER NOT NULL,
  sub1_score NUMERIC NOT NULL DEFAULT 1,
  sub2_score NUMERIC NOT NULL DEFAULT 2,
  sub3_score NUMERIC NOT NULL DEFAULT 6,
  bit_score NUMERIC NOT NULL,
  score_time NUMERIC NOT NULL,
  duration INTEGER NOT NULL DEFAULT 0,
  timecap INTEGER,
  ended_at TIMESTAMPTZ
);

CREATE TABLE running_subathons(
  subathon_id INTEGER PRIMARY KEY REFERENCES subathons(subathon_id),
  last_started TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE subathon_contributions(
  contribution_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  subathon_id INTEGER NOT NULL REFERENCES subathons(subathon_id),
  profileid INTEGER REFERENCES user_profiles(profileid),
  score NUMERIC NOT NULL,
  event_id INTEGER REFERENCES events(event_id),
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE subathon_goals(
  goal_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  subathon_id INTEGER NOT NULL REFERENCES subathons(subathon_id),
  required_score NUMERIC NOT NULL,
  description TEXT NOT NULL,
  notified BOOLEAN DEFAULT false,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

COMMIT;
