CREATE TABLE users(
  "id" serial primary key not null,
  "email" varchar unique,
  "username" varchar unique not null,
  "password" varchar not null,
  "avatar_picture" varchar,
  "google_sign_in_payload" varchar,
  "banned_at" timestamp,
  "banned_until" timestamp,
  "ban_reason" varchar,
  "is_permaban" boolean,
  "is_admin" boolean,
  "is_critics" boolean,
  "is_verified" boolean,
  "social_media" jsonb,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);

CREATE TABLE client_ips(
  "id" serial primary key not null,
  "ipv4" varchar(15) not null,
  "ipv6" varchar(40),
  "banned_at" timestamp,
  "banned_until" timestamp,
  "reason" varchar,
  "is_permaban" boolean,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);


CREATE TYPE  user_reports_type as ENUM(
  'comments',
  'reviews',
  'locations',
  'users',
  'stories'
);

CREATE TABLE user_reports(
  "id" serial primary key not null,
  "message" text not null,
  "date" timestamp not null,
  "report_target" integer not null,
  "report_type" user_reports_type not null,
  "submitted_by" integer references "users"("id") not null,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);


CREATE TABLE regions(
  "id" serial primary key not null,
  "region_name" varchar,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);

CREATE TABLE provinces(
  "id" serial primary key not null,
  "province_name" varchar,
  "region_id" smallint references "regions"("id") not null,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);

CREATE TABLE regencies(
  "id" serial primary key not null,
  "regency_name" varchar,
  "province_id" smallint references "provinces"("id") not null,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);

CREATE TABLE locations(
  "id" serial primary key not null,
  "address" varchar not null,
  "name" varchar not null,
  "google_maps_link" varchar,
  "submitted_by" integer references "users"("id") not null,
  "total_visited" integer,
  "thumbnail" varchar not null,
  "regency_id" smallint references "regencies"("id") not null,
  "is_deleted" boolean,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);

CREATE TABLE tags (
 "id" serial primary key not null,
 "name" varchar(50) not null,
 "target_id" integer, 
 "tags_type" varchar(20),
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);

CREATE TABLE location_images (
  "id" serial primary key not null,
  "url" varchar not null,
  "location_id" integer references "locations"("id") not null,
  "uploaded_by" integer references "users"("id"),
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);

CREATE TABLE reviews (
  "id" serial primary key not null,
  "submitted_by" integer references "users"("id") not null,
  "comments" text not null,
  "score" smallint not null, 
  "is_hided" boolean, -- if comments violate TOS just hide the reviews
  "location_id" integer references "locations"("id") not null,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);

CREATE TYPE comment_type AS ENUM(
  'stories',
  'news',
  'reviews'
);

CREATE TABLE comments(
  "id" serial primary key not null,
  "submitted_by" integer not null,
  "comment_on" integer not null,
  "comment_type" comment_type not null,
  "reply_to" integer,
  "is_hide" boolean,
  "created_at" timestamp default(now()),
  "updated_at" timestamp default(now())
);