CREATE TABLE "meta" (
  "id" uuid UNIQUE PRIMARY KEY NOT NULL,
  "system_code" varchar(50) NOT NULL,
  "tool" uuid,
  "practice" varchar(32) NOT NULL,
  "build" varchar(250) NOT NULL,
  "enviroment" varchar(250) NOT NULL,
  "library" varchar(50),
  "method" varchar(10),
  "hash" varchar(500) NOT NULL,
  "created_at" datetime NOT NULL
);

CREATE TABLE "tool" (
  "id" uuid UNIQUE PRIMARY KEY NOT NULL,
  "tool" varchar(50) NOT NULL,
  "practive" varchar(20) NOT NULL
);

CREATE TABLE "problem" (
  "id" uuid UNIQUE PRIMARY KEY NOT NULL,
  "executor" uuid,
  "is_created_manually" bool,
  "status" varchar(32) NOT NULL,
  "status_updated_at" datetime NOT NULL,
  "resolution" varchar(32),
  "priority" varchar(32) NOT NULL,
  "description" varchar(500),
  "remediation" varchar(500),
  "сategory" varchar(250),
  "cve" varchar(50),
  "cwe" varchar(500),
  "cvss" int,
  "location" varchar(500) NOT NULL,
  "defect_id" uuid,
  "defect_status" varchar(32),
  "created_at" datetime NOT NULL,
  "exception_date" datetime,
  "category" varchar(250),
  "hash" varchar(500) NOT NULL,
  "creator" uuid,
  "version" int NOT NULL,
  "group_id" uuid,
  "meta" uuid NOT NULL
);

CREATE TABLE "user" (
  "id" uuid UNIQUE PRIMARY KEY NOT NULL,
  "login" varchar(100) NOT NULL,
  "is_active" bool
);

CREATE TABLE "problem_group" (
  "id" uuid UNIQUE PRIMARY KEY NOT NULL,
  "name" varchar(50) NOT NULL,
  "description" varchar(300)
);

CREATE TABLE "system" (
  "id" uuid UNIQUE PRIMARY KEY NOT NULL,
  "name" varchar(50) NOT NULL,
  "code" varchar(50) NOT NULL,
  "buiseness_critical" bool,
  "security_critical" bool,
  "is_active" bool,
  PRIMARY KEY ("id")
);

CREATE TABLE "user_to_system" (
  "system_id" uuid NOT NULL,
  "user_id" uuid NOT NULL,
  PRIMARY KEY ("system_id", "user_id")
);

CREATE TABLE "comment" (
  "id" uuid PRIMARY KEY NOT NULL,
  "problem_id" uuid NOT NULL,
  "description" varchar(500) NOT NULL,
  "created_by" uuid NOT NULL,
  "created_at" datetime NOT NULL
);

CREATE TABLE "work_history" (
  "id" uuid PRIMARY KEY NOT NULL,
  "problem_id" uuid NOT NULL,
  "action" varchar(100),
  "old_value" varchar(100),
  "new_value" varchar(100),
  "user_id" uuid NOT NULL,
  "created_at" datetime NOT NULL
);

CREATE INDEX ON "system" ("code");

ALTER TABLE "meta" ADD FOREIGN KEY ("system_code") REFERENCES "system" ("code");

ALTER TABLE "meta" ADD FOREIGN KEY ("tool") REFERENCES "tool" ("id");

ALTER TABLE "problem" ADD FOREIGN KEY ("executor") REFERENCES "user" ("id");

ALTER TABLE "problem" ADD FOREIGN KEY ("creator") REFERENCES "user" ("id");

ALTER TABLE "problem" ADD FOREIGN KEY ("group_id") REFERENCES "problem_group" ("id");

ALTER TABLE "problem" ADD FOREIGN KEY ("meta") REFERENCES "meta" ("id");

ALTER TABLE "user_to_system" ADD FOREIGN KEY ("system_id") REFERENCES "system" ("id");

ALTER TABLE "user_to_system" ADD FOREIGN KEY ("user_id") REFERENCES "user" ("id");

ALTER TABLE "comment" ADD FOREIGN KEY ("problem_id") REFERENCES "problem" ("id");

ALTER TABLE "comment" ADD FOREIGN KEY ("created_by") REFERENCES "user" ("id");

ALTER TABLE "work_history" ADD FOREIGN KEY ("problem_id") REFERENCES "problem" ("id");

ALTER TABLE "work_history" ADD FOREIGN KEY ("user_id") REFERENCES "user" ("id");
