CREATE TABLE users(
    id BIGSERIAL PRIMARY KEY,
    email VARCHAR NOT NULL UNIQUE,
    username VARCHAR NOT NULL UNIQUE,
    hashed_password VARCHAR NOT NULL,
    role INT NOT NULL DEFAULT 0,
    black_phoenix BOOL NOT NULL DEFAULT FALSE,
    avatar_image VARCHAR NOT NULL DEFAULT 'https://images.black-phoenix.ru/static/images/%D1%82%D1%8B%20%D1%83%D0%B6%D0%B5%20%D0%BF%D0%B5%D1%88%D0%BA%D0%B0%20BP.png',
    date_of_birth DATE NOT NULL,
    date_of_registration DATE NOT NULL DEFAULT now()
);

CREATE TABLE chat(
    id BIGSERIAL PRIMARY KEY,
    created_by BIGINT NOT NULL,
    chat_for BIGINT NOT NULL,
    chat_name VARCHAR NOT NULL,
    avatar_image VARCHAR NOT NULL,
    visibility BOOL NOT NULL DEFAULT TRUE,
    FOREIGN KEY (created_by)
        REFERENCES users (id),
    FOREIGN KEY (chat_for)
        REFERENCES users (id)
);

CREATE TABLE pinned_chat(
    user_id BIGINT,
    chat_id BIGINT,
    PRIMARY KEY (user_id, chat_id),
    FOREIGN KEY (user_id)
        REFERENCES users (id),
    FOREIGN KEY (chat_id)
        REFERENCES chat (id)
);

CREATE TABLE pinned_message(
    chat_id BIGINT NOT NULL,
    message_id UUID,
    user_id BIGINT,
    PRIMARY KEY (user_id, message_id),
    FOREIGN KEY (user_id)
        REFERENCES users (id),
    FOREIGN KEY (chat_id)
        REFERENCES chat (id)
);

CREATE TABLE user_avatar(
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    avatar_image VARCHAR NOT NULL,
    FOREIGN KEY (user_id)
       REFERENCES users (id)
);

CREATE TABLE user_chat(
    user_id BIGINT,
    chat_id BIGINT,
    PRIMARY KEY (user_id, chat_id),
    FOREIGN KEY (user_id)
        REFERENCES users (id),
    FOREIGN KEY (chat_id)
        REFERENCES chat (id)
);

