CREATE SEQUENCE IF NOT EXISTS category_id_seq START WITH 1 INCREMENT BY 1;

CREATE SEQUENCE IF NOT EXISTS comment_id_seq START WITH 1 INCREMENT BY 1;

CREATE SEQUENCE IF NOT EXISTS post_id_seq START WITH 1 INCREMENT BY 1;

CREATE SEQUENCE IF NOT EXISTS role_id_seq START WITH 1 INCREMENT BY 1;

CREATE SEQUENCE IF NOT EXISTS tag_id_seq START WITH 1 INCREMENT BY 1;

CREATE SEQUENCE IF NOT EXISTS users_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE category
(
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    create_at   TIMESTAMP WITHOUT TIME ZONE,
    created_by  VARCHAR(20),
    delete_at   TIMESTAMP WITHOUT TIME ZONE,
    is_active   VARCHAR(1)                              NOT NULL,
    is_approved VARCHAR(255),
    is_delete   VARCHAR(1)                              NOT NULL,
    update_at   TIMESTAMP WITHOUT TIME ZONE,
    update_by   VARCHAR(20),
    description VARCHAR(50),
    name        VARCHAR(50),
    slug        VARCHAR(255),
    CONSTRAINT category_pkey PRIMARY KEY (id)
);

CREATE TABLE comment
(
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    create_at   TIMESTAMP WITHOUT TIME ZONE,
    created_by  VARCHAR(20),
    delete_at   TIMESTAMP WITHOUT TIME ZONE,
    is_active   VARCHAR(1)                              NOT NULL,
    is_approved VARCHAR(255),
    is_delete   VARCHAR(1)                              NOT NULL,
    update_at   TIMESTAMP WITHOUT TIME ZONE,
    update_by   VARCHAR(20),
    author      VARCHAR(50),
    is_edited   VARCHAR(1)                              NOT NULL,
    message     OID,
    post_id     BIGINT,
    CONSTRAINT comment_pkey PRIMARY KEY (id)
);

CREATE TABLE post
(
    id                 BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    create_at          TIMESTAMP WITHOUT TIME ZONE,
    created_by         VARCHAR(20),
    delete_at          TIMESTAMP WITHOUT TIME ZONE,
    is_active          VARCHAR(1)                              NOT NULL,
    is_approved        VARCHAR(255),
    is_delete          VARCHAR(1)                              NOT NULL,
    update_at          TIMESTAMP WITHOUT TIME ZONE,
    update_by          VARCHAR(20),
    content            text,
    featured_image_url VARCHAR(255),
    is_published       VARCHAR(1)                              NOT NULL,
    likes              INTEGER                                 NOT NULL,
    name               VARCHAR(255),
    slug               VARCHAR(255),
    CONSTRAINT post_pkey PRIMARY KEY (id)
);

CREATE TABLE posts_categories
(
    post_id     BIGINT NOT NULL,
    category_id BIGINT NOT NULL
);

CREATE TABLE posts_comments
(
    post_id    BIGINT NOT NULL,
    comment_id BIGINT NOT NULL
);

CREATE TABLE posts_tags
(
    post_id BIGINT NOT NULL,
    tag_id  BIGINT NOT NULL
);

CREATE TABLE role
(
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    create_at   TIMESTAMP WITHOUT TIME ZONE,
    created_by  VARCHAR(20),
    delete_at   TIMESTAMP WITHOUT TIME ZONE,
    is_active   VARCHAR(1)                              NOT NULL,
    is_approved VARCHAR(255),
    is_delete   VARCHAR(1)                              NOT NULL,
    update_at   TIMESTAMP WITHOUT TIME ZONE,
    update_by   VARCHAR(20),
    name        VARCHAR(20),
    CONSTRAINT role_pkey PRIMARY KEY (id)
);

CREATE TABLE schema_version
(
    installed_rank INTEGER                                   NOT NULL,
    version        VARCHAR(50),
    description    VARCHAR(200)                              NOT NULL,
    type           VARCHAR(20)                               NOT NULL,
    script         VARCHAR(1000)                             NOT NULL,
    checksum       INTEGER,
    installed_by   VARCHAR(100)                              NOT NULL,
    installed_on   TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL,
    execution_time INTEGER                                   NOT NULL,
    success        BOOLEAN                                   NOT NULL,
    CONSTRAINT schema_version_pk PRIMARY KEY (installed_rank)
);

CREATE TABLE tag
(
    id          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    create_at   TIMESTAMP WITHOUT TIME ZONE,
    created_by  VARCHAR(20),
    delete_at   TIMESTAMP WITHOUT TIME ZONE,
    is_active   VARCHAR(1)                              NOT NULL,
    is_approved VARCHAR(255),
    is_delete   VARCHAR(1)                              NOT NULL,
    update_at   TIMESTAMP WITHOUT TIME ZONE,
    update_by   VARCHAR(20),
    description VARCHAR(255),
    name        VARCHAR(255),
    slug        VARCHAR(255),
    CONSTRAINT tag_pkey PRIMARY KEY (id)
);

CREATE TABLE users
(
    id                  BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    create_at           TIMESTAMP WITHOUT TIME ZONE,
    created_by          VARCHAR(20),
    delete_at           TIMESTAMP WITHOUT TIME ZONE,
    is_active           VARCHAR(1)                              NOT NULL,
    is_approved         VARCHAR(255),
    is_delete           VARCHAR(1)                              NOT NULL,
    update_at           TIMESTAMP WITHOUT TIME ZONE,
    update_by           VARCHAR(20),
    bio                 VARCHAR(255),
    email               VARCHAR(255),
    first_name          VARCHAR(20),
    last_name           VARCHAR(20),
    password_hash       VARCHAR(255),
    profile_picture_url VARCHAR(255),
    teams_accepted      VARCHAR(1)                              NOT NULL,
    username            VARCHAR(255),
    CONSTRAINT users_pkey PRIMARY KEY (id)
);

CREATE TABLE users_roles
(
    user_id BIGINT NOT NULL,
    role_id BIGINT NOT NULL
);

ALTER TABLE post
    ADD CONSTRAINT uk1cg7fr7ckgm878v5j9bwjixis UNIQUE (slug);

ALTER TABLE users
    ADD CONSTRAINT uk6dotkott2kjsp8vw4d0m25fb7 UNIQUE (email);

ALTER TABLE comment
    ADD CONSTRAINT ukf1sl0xkd2lucs7bve3ktt3tu5 UNIQUE (post_id);

ALTER TABLE users
    ADD CONSTRAINT ukr43af9ap4edm43mmtq01oddj6 UNIQUE (username);

CREATE INDEX schema_version_s_idx ON schema_version (success);

ALTER TABLE users_roles
    ADD CONSTRAINT fk2o0jvgh89lemvvo17cbqvdxaa FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION;

ALTER TABLE posts_tags
    ADD CONSTRAINT fk3kpemk30oljhiknulqrqo98jt FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE NO ACTION;

ALTER TABLE posts_categories
    ADD CONSTRAINT fk749027lw44rwd1n2rlxkq1l42 FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE NO ACTION;

ALTER TABLE posts_comments
    ADD CONSTRAINT fk9fphpud93o6x0e1o8wqgrhg3q FOREIGN KEY (comment_id) REFERENCES comment (id) ON DELETE NO ACTION;

ALTER TABLE posts_tags
    ADD CONSTRAINT fkayq0t2pypsj75e6y2g5quqc14 FOREIGN KEY (tag_id) REFERENCES tag (id) ON DELETE NO ACTION;

ALTER TABLE posts_categories
    ADD CONSTRAINT fkhdh8311d3lr7qb6pydw29a0er FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE NO ACTION;

ALTER TABLE posts_comments
    ADD CONSTRAINT fkot2q6gro333cxj7uj5nqcxrrd FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE NO ACTION;

ALTER TABLE comment
    ADD CONSTRAINT fks1slvnkuemjsq2kj4h3vhx7i1 FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE NO ACTION;

ALTER TABLE users_roles
    ADD CONSTRAINT fkt4v0rrweyk393bdgt107vdx0x FOREIGN KEY (role_id) REFERENCES role (id) ON DELETE NO ACTION;