CREATE TABLE IF NOT EXISTS "user" (
  "id" INTEGER PRIMARY KEY,
  "creation" TIMESTAMP NOT NULL,
  "email" VARCHAR NOT NULL,
  "name" VARCHAR NOT NULL,
  CONSTRAINT "uniq_user_email" UNIQUE ("email"),
  CONSTRAINT "uniq_user_name" UNIQUE ("name")
);

CREATE TABLE IF NOT EXISTS "job" (
  "id" INTEGER PRIMARY KEY,
  "kind" VARCHAR NOT NULL,
  "last_execution" TIMESTAMP NULL,
  "last_check" TIMESTAMP NULL,
  CONSTRAINT "uniq_job_kind" UNIQUE ("kind")
);

CREATE TABLE IF NOT EXISTS "sign_in"(
  "id" INTEGER PRIMARY KEY,
  "token" VARCHAR NOT NULL,
  "creation" TIMESTAMP NOT NULL,
  "email" VARCHAR NOT NULL,
  "is_used" BOOLEAN NOT NULL,
  CONSTRAINT "uniq_sign_in_token" UNIQUE ("token")
);

CREATE TABLE IF NOT EXISTS "payment"(
  "id" INTEGER PRIMARY KEY,
  "user_id" INTEGER NOT NULL REFERENCES "user",
  "name" VARCHAR NOT NULL,
  "cost" INTEGER NOT NULL,
  "date" DATE NOT NULL,
  "frequency" VARCHAR NOT NULL,
  "created_at" TIMESTAMP NOT NULL,
  "edited_at" TIMESTAMP NULL,
  "deleted_at" TIMESTAMP NULL
);

CREATE TABLE IF NOT EXISTS "income"(
  "id" INTEGER PRIMARY KEY,
  "user_id" INTEGER NOT NULL REFERENCES "user",
  "date" DATE NOT NULL,
  "amount" INTEGERNOT NULL,
  "created_at" TIMESTAMP NOT NULL,
  "edited_at" TIMESTAMP NULL,
  "deleted_at" TIMESTAMP NULL
);

CREATE TABLE IF NOT EXISTS "category"(
  "id" INTEGER PRIMARY KEY,
  "name" VARCHAR NOT NULL,
  "color" VARCHAR NOT NULL,
  "created_at" TIMESTAMP NOT NULL,
  "edited_at" TIMESTAMP NULL,
  "deleted_at" TIMESTAMP NULL
);

CREATE TABLE IF NOT EXISTS "payment_category"(
  "id" INTEGER PRIMARY KEY,
  "name" VARCHAR NOT NULL,
  "category" INTEGER NOT NULL REFERENCES "category",
  "created_at" TIMESTAMP NOT NULL,
  "edited_at" TIMESTAMP NULL,
  CONSTRAINT "uniq_payment_category_name" UNIQUE ("name")
);
