CREATE TABLE shares(
  id serial4 NOT NULL,
  "name" varchar NOT NULL,
  description varchar NOT NULL,
  uploader int4 NULL,
  added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT shares_pkey PRIMARY KEY (id),
  CONSTRAINT shares_uploader_fkey FOREIGN KEY (uploader) REFERENCES account(id)
);

CREATE INDEX shares_added_idx ON shares USING btree(added);

CREATE INDEX shares_uploader_idx ON shares USING btree(uploader);

CREATE TABLE lookup_share_relationships(
  share_id int4 NOT NULL,
  service varchar NOT NULL,
  user_id varchar NOT NULL,
  CONSTRAINT lookup_share_relationships_pkey PRIMARY KEY (share_id, service, user_id),
  CONSTRAINT lookup_share_relationships_service_user_id_fkey FOREIGN KEY (service, user_id) REFERENCES lookup(service, id),
  CONSTRAINT lookup_share_relationships_share_id_fkey FOREIGN KEY (share_id) REFERENCES shares(id)
);

CREATE TABLE file_share_relationships(
  share_id int4 NOT NULL,
  upload_url varchar NOT NULL,
  upload_id varchar NOT NULL,
  file_id int4 NULL,
  filename varchar NOT NULL,
  CONSTRAINT file_share_relationships_pkey PRIMARY KEY (share_id, upload_id),
  CONSTRAINT file_share_relationships_file_id_fkey FOREIGN KEY (file_id) REFERENCES files(id),
  CONSTRAINT file_share_relationships_share_id_fkey FOREIGN KEY (share_id) REFERENCES shares(id)
);
