CREATE TABLE IF NOT EXISTS `channel` (
    `id` INTEGER PRIMARY KEY AUTOINCREMENT,
    `name` VARCHAR,
    `last_message_id` INTEGER,
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS `channel_user` (
    `id` INTEGER PRIMARY KEY AUTOINCREMENT,
    `channel_id` INTEGER NOT NULL,
    `user_id` INTEGER NOT NULL,
    `admin` BOOLEAN NOT NULL DEFAULT 0,
    FOREIGN KEY(`channel_id`) REFERENCES `channel`(`id`) ON DELETE CASCADE,
    FOREIGN KEY(`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS `message` (
    `id` INTEGER PRIMARY KEY AUTOINCREMENT,
    `channel_id` INTEGER NOT NULL,
    `author_id` INTEGER,
    `content` TEXT,
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(`channel_id`) REFERENCES `channel`(`id`) ON DELETE CASCADE,
    FOREIGN KEY(`author_id`) REFERENCES `user`(`id`) ON DELETE
    SET
        NULL
);