CREATE TABLE "langs" (
  "id" integer UNIQUE PRIMARY KEY NOT NULL,
  "lang" text NOT NULL,
  "icon_alpha" varchar(2) NOT NULL
);

CREATE TABLE "authors" (
  "id" integer UNIQUE PRIMARY KEY NOT NULL,
  "name" text NOT NULL,
  "username" text UNIQUE NOT NULL,
  "password" text NOT NULL,
  "created_at" time DEFAULT (now()),
  "profile_picture" text,
  "bio" text
);

CREATE TABLE "author_links" (
  "author_id" integer NOT NULL,
  "link_name" text NOT NULL,
  "link_url" text NOT NULL
);

CREATE TABLE "post" (
  "id" integer UNIQUE PRIMARY KEY NOT NULL,
  "slug" text UNIQUE NOT NULL,
  "author_id" integer,
  "lang_id" integer,
  "title" varchar,
  "picture" text,
  "date" time DEFAULT (now()),
  "body" text,
  "views" integer DEFAULT 0,
  "likes" integer DEFAULT 0,
  "dislikes" integer DEFAULT 0,
  "shares" integer DEFAULT 0
);

CREATE TABLE "post_likes" (
  "id" integer UNIQUE PRIMARY KEY NOT NULL,
  "post_id" integer,
  "like_status" bool NOT NULL,
  "ip_address" inet NOT NULL
);

CREATE TABLE "category" (
  "id" integer UNIQUE PRIMARY KEY NOT NULL,
  "slug" text NOT NULL,
  "icon" text,
  "type_icon" text
);

CREATE TABLE "category_text" (
  "category_id" integer NOT NULL,
  "lang_id" integer NOT NULL,
  "name" text NOT NULL
);

CREATE TABLE "post_categories" (
  "category_id" integer NOT NULL,
  "post_id" integer NOT NULL
);

CREATE TABLE "author_categories" (
  "category_id" integer NOT NULL,
  "author_id" integer NOT NULL
);

CREATE TABLE "comment" (
  "id" integer UNIQUE PRIMARY KEY NOT NULL,
  "post_id" integer NOT NULL,
  "comment_id" integer,
  "date" time DEFAULT (now()),
  "user" text,
  "body" text
);

CREATE TABLE "comment_likes" (
  "id" integer UNIQUE PRIMARY KEY NOT NULL,
  "comment_id" integer,
  "like_status" bool NOT NULL,
  "ip_address" inet NOT NULL
);

CREATE TABLE "flagged_comment" (
  "comment_id" integer,
  "flag_count" integer DEFAULT 1
);

ALTER TABLE "author_links" ADD FOREIGN KEY ("author_id") REFERENCES "authors" ("id");

ALTER TABLE "post" ADD FOREIGN KEY ("author_id") REFERENCES "authors" ("id");

ALTER TABLE "post" ADD FOREIGN KEY ("lang_id") REFERENCES "langs" ("id");

ALTER TABLE "post_likes" ADD FOREIGN KEY ("post_id") REFERENCES "post" ("id");

ALTER TABLE "category_text" ADD FOREIGN KEY ("lang_id") REFERENCES "langs" ("id");

ALTER TABLE "category_text" ADD FOREIGN KEY ("category_id") REFERENCES "category" ("id");

ALTER TABLE "post_categories" ADD FOREIGN KEY ("category_id") REFERENCES "category" ("id");

ALTER TABLE "post_categories" ADD FOREIGN KEY ("post_id") REFERENCES "post" ("id");

ALTER TABLE "author_categories" ADD FOREIGN KEY ("category_id") REFERENCES "category" ("id");

ALTER TABLE "author_categories" ADD FOREIGN KEY ("author_id") REFERENCES "authors" ("id");

ALTER TABLE "comment" ADD FOREIGN KEY ("post_id") REFERENCES "post" ("id");

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

ALTER TABLE "comment_likes" ADD FOREIGN KEY ("comment_id") REFERENCES "comment" ("id");

ALTER TABLE "flagged_comment" ADD FOREIGN KEY ("comment_id") REFERENCES "comment" ("id");
