CREATE TABLE IF NOT EXISTS "activity" (
	"id" VARCHAR NOT NULL UNIQUE,
	"description" VARCHAR NOT NULL,
	"author_id" INTEGER NOT NULL,
	"time_spent" NUMERIC NOT NULL,
	"date_start" DATETIME NOT NULL,
	"date_end" DATETIME,
	"is_tracked" BOOLEAN NOT NULL DEFAULT false,
	"vetro_project_id" VARCHAR,
	"tag_id" VARCHAR NOT NULL,
	"task_id" INTEGER,
	"project_id" VARCHAR NOT NULL,
	PRIMARY KEY("id"),
	FOREIGN KEY ("author_id") REFERENCES "user"("id")
	ON UPDATE NO ACTION ON DELETE NO ACTION,
	FOREIGN KEY ("task_id") REFERENCES "task"("id")
	ON UPDATE NO ACTION ON DELETE NO ACTION
);

CREATE TABLE IF NOT EXISTS "tag" (
	"id" VARCHAR NOT NULL UNIQUE,
	"title" VARCHAR NOT NULL,
	PRIMARY KEY("id")
);

CREATE TABLE IF NOT EXISTS "vetro_project" (
	"id" VARCHAR NOT NULL UNIQUE,
	"title" VARCHAR NOT NULL,
	PRIMARY KEY("id")
);

CREATE TABLE IF NOT EXISTS "activity_type" (
	"id" INTEGER NOT NULL UNIQUE,
	"title" VARCHAR NOT NULL,
	PRIMARY KEY("id")
);

CREATE TABLE IF NOT EXISTS "task" (
	"id" INTEGER,
	"subject" TEXT,
	"description" TEXT,
	"status" TEXT,
	"priority" TEXT,
	"author" TEXT,
	"tracker" TEXT,
	"project" TEXT,
	PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "time_entry" (
	"id" INTEGER,
	"author_id" INTEGER NOT NULL,
	"description" VARCHAR NOT NULL,
	"date" DATETIME NOT NULL,
	"activity_id" VARCHAR NOT NULL,
	"time_spent" NUMERIC NOT NULL,
	"task_id" INTEGER NOT NULL,
	"activity_type_id" INTEGER NOT NULL,
	PRIMARY KEY("id"),
	FOREIGN KEY ("author_id") REFERENCES "user"("id")
	ON UPDATE NO ACTION ON DELETE NO ACTION,
	FOREIGN KEY ("task_id") REFERENCES "task"("id")
	ON UPDATE NO ACTION ON DELETE NO ACTION,
	FOREIGN KEY ("activity_type_id") REFERENCES "activity_type"("id")
	ON UPDATE NO ACTION ON DELETE NO ACTION
);

CREATE TABLE IF NOT EXISTS "user" (
	"id" INTEGER NOT NULL UNIQUE,
	"first_name" VARCHAR,
	"last_name" VARCHAR,
	"clockify_user_id" VARCHAR NOT NULL UNIQUE,
	"redmine_user_id" VARCHAR NOT NULL UNIQUE,
	"clockify_email" VARCHAR UNIQUE,
	"clockify_username" VARCHAR UNIQUE,
	"redmine_email" VARCHAR UNIQUE,
	"redmine_username" VARCHAR UNIQUE,
	PRIMARY KEY("id")
);
