CREATE TYPE "status" AS ENUM (
  'SUBMITTED',
  'UNDER_REVIEW',
  'REVISION',
  'RESUBMITTED',
  'REJECTED',
  'ACCEPTED',
  'PUBLISHED'
);

CREATE TABLE "person" (
  "email" text PRIMARY KEY,
  "first_name" text NOT NULL,
  "last_name" text NOT NULL
);

CREATE TABLE "affiliation" (
  "org_name" text PRIMARY KEY,
  "website" text NOT NULL,
  "country" text NOT NULL
);

CREATE TABLE "person_affiliation" (
  "email" text,
  "org_name" text,
  "from_date" date,
  "to_date" date,
  PRIMARY KEY ("email", "org_name")
);

CREATE TABLE "paper" (
  "paper_id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  "title" text NOT NULL,
  "abstract" text NOT NULL,
  "filename" text NOT NULL,
  "contact_email" text NOT NULL,
  "year" int NOT NULL
);

CREATE TABLE "paper_author" (
  "paper_id" int,
  "email" text,
  "rank" int NOT NULL,
  PRIMARY KEY ("paper_id", "email")
);

CREATE TABLE "conference" (
  "year" int PRIMARY KEY,
  "location" text NOT NULL
);

CREATE TABLE "review" (
  "paper_id" int,
  "email" text,
  "merit" int NOT NULL,
  "relevance" int NOT NULL,
  "readability" int NOT NULL,
  "originality" int NOT NULL,
  "author_comments" text NOT NULL,
  "committee_comments" text,
  PRIMARY KEY ("paper_id", "email")
);

CREATE TABLE "reviewer" (
  "email" text PRIMARY KEY,
  "phone" text
);

CREATE TABLE "topic" (
  "topic_id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  "topic_name" text NOT NULL
);

CREATE TABLE "paper_topic" (
  "paper_id" int,
  "topic_id" int,
  PRIMARY KEY ("paper_id", "topic_id")
);

CREATE TABLE "expertise" (
  "email" text,
  "topic_id" int,
  PRIMARY KEY ("email", "topic_id")
);

CREATE TABLE "history" (
  "paper_id" int,
  "timestamp" timestamp,
  "paper_status" status NOT NULL,
  "notes" text,
  PRIMARY KEY ("paper_id", "timestamp")
);

COMMENT ON COLUMN "paper_author"."rank" IS 'author order';

COMMENT ON TABLE "review" IS 'Scores range from 1 to 5';

ALTER TABLE "person_affiliation" ADD FOREIGN KEY ("email") REFERENCES "person" ("email");

ALTER TABLE "person_affiliation" ADD FOREIGN KEY ("org_name") REFERENCES "affiliation" ("org_name");

ALTER TABLE "paper" ADD FOREIGN KEY ("contact_email") REFERENCES "person" ("email");

ALTER TABLE "paper" ADD FOREIGN KEY ("year") REFERENCES "conference" ("year");

ALTER TABLE "paper_author" ADD FOREIGN KEY ("paper_id") REFERENCES "paper" ("paper_id");

ALTER TABLE "paper_author" ADD FOREIGN KEY ("email") REFERENCES "person" ("email");

ALTER TABLE "review" ADD FOREIGN KEY ("paper_id") REFERENCES "paper" ("paper_id");

ALTER TABLE "review" ADD FOREIGN KEY ("email") REFERENCES "reviewer" ("email");

ALTER TABLE "reviewer" ADD FOREIGN KEY ("email") REFERENCES "person" ("email");

ALTER TABLE "paper_topic" ADD FOREIGN KEY ("paper_id") REFERENCES "paper" ("paper_id");

ALTER TABLE "paper_topic" ADD FOREIGN KEY ("topic_id") REFERENCES "topic" ("topic_id");

ALTER TABLE "expertise" ADD FOREIGN KEY ("email") REFERENCES "reviewer" ("email");

ALTER TABLE "expertise" ADD FOREIGN KEY ("topic_id") REFERENCES "topic" ("topic_id");

ALTER TABLE "history" ADD FOREIGN KEY ("paper_id") REFERENCES "paper" ("paper_id");
