-- Удалить сперва таблицы, затем типы. 
--DROP TYPE prog_lang;
--DROP TYPE prog_level;


CREATE TYPE prog_level AS ENUM (
    'Нулевой',
    'Начальный',
    'Базовый',
    'Средний',
    'Продвинутый',
    'Эксперт',
    'Архитектор'
);


CREATE TYPE prog_lang AS ENUM (
  'Go',
  'Js',
  'Ts',
  'Python',
  'Java', 
  'C++',
  'C',  
  'PHP',
  'SQL'
  );


DROP TABLE "role_rules";
CREATE TABLE "role_rules" (
  "id_roles" INTEGER NOT NULL,
  "id_rules" INTEGER NOT NULL,
  PRIMARY KEY ("id_roles", "id_rules")
);


DROP TABLE "rules";
CREATE TABLE "rules" (
  "id" SERIAL PRIMARY KEY,
  "id_rules" INTEGER,
  "name" VARCHAR(255) NOT NULL,
  "body" JSONB NOT NULL
);


DROP TABLE "role_account";
CREATE TABLE "role_account" (
  "id_role" INTEGER NOT NULL,
  "id_accont" INTEGER NOT NULL,
  PRIMARY KEY ("id_role", "id_accont")
);

DROP TABLE "role";
CREATE TABLE "role" (
  "id" SERIAL PRIMARY KEY,
  "name" TEXT NOT NULL,
  "description" VARCHAR(255) NOT NULL
);


DROP TABLE "solution";
CREATE TABLE "solution" (
  "id_tasks" INTEGER NOT NULL,
  "id_students" INTEGER NOT NULL,
  "message" TEXT NOT NULL,
  "status" INTEGER,
  "dt_check" TIMESTAMP,
  PRIMARY KEY ("id_tasks", "id_students")
);


DROP TABLE "task_for_student";
CREATE TABLE "task_for_student" (
  "id_task" INTEGER NOT NULL,
  "id_student" INTEGER NOT NULL,
  "dt_of_delivery" DATE,
  PRIMARY KEY ("id_task", "id_student")
);



DROP TABLE "task";
CREATE TABLE "task" (
  "id" SERIAL PRIMARY KEY,
  "id_category" INTEGER,
  "id_profile" INTEGER,
  "tp_level" PROG_LEVEL NOT NULL,
  "tp_lang" PROG_LANG NOT NULL,
  "name" VARCHAR(255),
  "description" TEXT NOT NULL,
  "code" TEXT NOT NULL,
  "unit_test" TEXT NOT NULL,
  "dt_create" TIMESTAMP
);


DROP TABLE "profile";
CREATE  TABLE "profile" (
  "id" SERIAL PRIMARY KEY,
  "id_account" INTEGER NOT NULL,
  "name" VARCHAR(100) NOT NULL,
  "suname" VARCHAR(100) NOT NULL,
  "git" VARCHAR(255) NOT NULL,
  "dt_create" DATE
);


DROP TABLE "account";
CREATE  TABLE "account" (
  "id" SERIAL PRIMARY KEY,
  "login" VARCHAR(50) NOT NULL,
  "pswd" VARCHAR(255) NOT NULL,
  "is_block" BOOLEAN NOT NULL,
  "dt_create" DATE NOT NULL,
  "token" VARCHAR(255),
  "refresh" VARCHAR(255)
);



-- TABLE "role_rules"
-- DROP INDEX "idx_role_rules__id_rules";
CREATE INDEX "idx_role_rules__id_rules" ON "role_rules" ("id_rules");
ALTER TABLE "role_rules" ADD CONSTRAINT "fk_role_rules__id_roles" FOREIGN KEY ("id_roles") REFERENCES "role" ("id") ON DELETE CASCADE;
ALTER TABLE "role_rules" ADD CONSTRAINT "fk_role_rules__id_rules" FOREIGN KEY ("id_rules") REFERENCES "rules" ("id") ON DELETE CASCADE;


-- TABLE "profile"
-- DROP INDEX "idx_profile__id_account";
CREATE INDEX "idx_profile__id_account" ON "profile" ("id_account");
ALTER TABLE "profile" ADD CONSTRAINT "fk_profile__id_account" FOREIGN KEY ("id_account") REFERENCES "account" ("id");


-- TABLE "rules"
-- DROP INDEX "idx_rules__id_rules";
CREATE INDEX "idx_rules__id_rules" ON "rules" ("id_rules");
ALTER TABLE "rules" ADD CONSTRAINT "fk_rules__id_rules" FOREIGN KEY ("id_rules") REFERENCES "rules" ("id") ON DELETE SET NULL;


-- TABLE "task_for_student"
-- DROP INDEX "idx_task_for_student__id_student";
CREATE INDEX "idx_task_for_student__id_student" ON "task_for_student" ("id_student");
ALTER TABLE "task_for_student" ADD CONSTRAINT "fk_task_for_student__id_student" FOREIGN KEY ("id_student") REFERENCES "profile" ("id") ON DELETE CASCADE;
ALTER TABLE "task_for_student" ADD CONSTRAINT "fk_task_for_student__id_task" FOREIGN KEY ("id_task") REFERENCES "task" ("id") ON DELETE CASCADE;


--  TABLE "role_account"
-- DROP INDEX "idx_role_account__id_accont";
CREATE INDEX "idx_role_account__id_accont" ON "role_account" ("id_accont");
ALTER TABLE "role_account" ADD CONSTRAINT "fk_role_account__id_accont" FOREIGN KEY ("id_accont") REFERENCES "account" ("id") ON DELETE CASCADE;
ALTER TABLE "role_account" ADD CONSTRAINT "fk_role_account__id_role" FOREIGN KEY ("id_role") REFERENCES "role" ("id") ON DELETE CASCADE;


--  TABLE "solution"
-- DROP INDEX "idx_solution__id_students";
CREATE INDEX "idx_solution__id_students" ON "solution" ("id_students");
ALTER TABLE "solution" ADD CONSTRAINT "fk_solution__id_students" FOREIGN KEY ("id_students") REFERENCES "profile" ("id") ON DELETE CASCADE;
ALTER TABLE "solution" ADD CONSTRAINT "fk_solution__id_tasks" FOREIGN KEY ("id_tasks") REFERENCES "task" ("id") ON DELETE CASCADE;
