-- +micrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TYPE user_roles AS ENUM ('Teacher', 'Student');

CREATE TABLE users(
  id BIGSERIAL PRIMARY KEY,
  username TEXT UNIQUE NOT NULL,
  role user_roles NOT NULL,
  admin BOOLEAN NOT NULL
);

CREATE TABLE teachers(
  id BIGSERIAL PRIMARY KEY,
  user_id BIGINT NOT NULL UNIQUE REFERENCES users(id),
  max_students INT NOT NULL,
  skif BOOLEAN NOT NULL
);

CREATE TABLE students(
  id BIGSERIAL PRIMARY KEY,
  user_id BIGINT NOT NULL UNIQUE REFERENCES users(id),
  skif BOOLEAN NOT NULL
);

ALTER TABLE
  users
ADD
  COLUMN teacher_id BIGINT UNIQUE REFERENCES teachers(id);

ALTER TABLE
  users
ADD
  COLUMN student_id BIGINT UNIQUE REFERENCES students(id);

CREATE TABLE votes(
  id BIGSERIAL PRIMARY KEY,
  student_id BIGINT NOT NULL UNIQUE REFERENCES students(id)
);

ALTER TABLE
  students
ADD
  COLUMN vote_id BIGINT UNIQUE REFERENCES votes(id);

CREATE TABLE teacher_votes(
  id BIGSERIAL PRIMARY KEY,
  vote_id BIGINT NOT NULL REFERENCES votes(id),
  teacher_id BIGINT NOT NULL REFERENCES teachers(id),
  priority INT NOT NULL
);

-- +micrate Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE teacher_votes;

DROP TABLE votes;

DROP TABLE admins;

DROP TABLE teachers;

DROP TABLE students;

DROP TABLE users;

DROP TYPE user_roles;