CREATE EXTENSION IF NOT EXISTS "uuid-ossp";


CREATE TABLE tgchats (
    id BIGINT PRIMARY KEY UNIQUE,
    chat_type TEXT NOT NULL,
    title TEXT NOT NULL
);

CREATE TABLE users (
    id BIGINT PRIMARY KEY UNIQUE,
    username TEXT,
    first_name TEXT,
    last_name TEXT
);

CREATE TABLE tg_messages (
    id BIGINT PRIMARY KEY UNIQUE,
    text TEXT NOT NULL,
    message_time TIMESTAMP NOT NULL,
    user_id BIGINT NOT NULL REFERENCES users(id),
    chat_id BIGINT NOT NULL REFERENCES tgchats(id),
    slice_id UUID NOT NULL
);

CREATE TABLE successed (
    id BIGSERIAL PRIMARY KEY UNIQUE,
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    reason TEXT NOT NULL,
    chat_id UUID NOT NULL REFERENCES tgchats(slice_id) ON DELETE CASCADE,
    created_at TIMESTAMP DEFAULT NOW() NOT NULL
);
