-- Tasklist data {{{

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

CREATE TABLE tasklist(
  taskid INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  profileid INTEGER NOT NULL REFERENCES user_profiles(profileid) ON DELETE CASCADE ON UPDATE CASCADE,
  content TEXT NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  duration INTEGER NOT NULL DEFAULT 0,
  started_at TIMESTAMPTZ,
  completed_at TIMESTAMPTZ,
  _timestamp TIMESTAMPTZ DEFAILT NOW()
);
CREATE TRIGGER tasklist_timestamp BEFORE UPDATE ON tasklist
  FOR EACH ROW EXECUTE FUNCTION update_timestamp_column();


CREATE TABLE nowlist(
  taskid INTEGER PRIMARY KEY REFERENCES tasklist(taskid) ON DELETE CASCADE ON UPDATE CASCADE,
  last_started TIMESTAMPTZ NOT NULL
);

CREATE TABLE taskplan(
  taskid INTEGER PRIMARY KEY REFERENCES tasklist(taskid) ON DELETE CASCADE ON UPDATE CASCADE,
  order_idx INTEGER NOT NULL
);

CREATE TABLE task_profiles(
  profileid INTEGER PRIMARY KEY REFERENCES user_profiles(profileid) ON DELETE CASCADE ON UPDATE CASCADE
);


-- }}}
