-- Drop all old tables
DROP TABLE IF EXISTS users_old;
DROP TABLE IF EXISTS messages;
DROP TABLE IF EXISTS embeds;
DROP TABLE IF EXISTS embed_fields;
DROP TABLE IF EXISTS command_aliases;
DROP TABLE IF EXISTS macro;
DROP TABLE IF EXISTS roles;

SET FOREIGN_KEY_CHECKS = 0;

-- Drop columns from channels that are no longer used
ALTER TABLE channels DROP COLUMN `name`;
ALTER TABLE channels DROP COLUMN `blacklisted`;

-- Drop columns from guilds table that are no longer used and rebuild table
CREATE TABLE guilds_new (
    id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
    ephemeral_confirmations BOOLEAN NOT NULL DEFAULT 0
);
INSERT INTO guilds_new (id, ephemeral_confirmations) SELECT guild, ephemeral_confirmations FROM guilds;
RENAME TABLE guilds TO guilds_old;
RENAME TABLE guilds_new TO guilds;

-- Update fk on channels to point at new guild table
ALTER TABLE channels
    DROP FOREIGN KEY `channels_ibfk_1`,
    MODIFY COLUMN guild_id BIGINT UNSIGNED,
    ADD FOREIGN KEY `fk_guild_id` (`guild_id`)
        REFERENCES `guilds` (`id`)
        ON DELETE CASCADE
        ON UPDATE CASCADE;
UPDATE channels SET guild_id = (SELECT guild FROM guilds_old WHERE id = guild_id);

-- Rebuild todos table
CREATE TABLE `todos_new` (
    `id`         INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `guild_id`   BIGINT UNSIGNED DEFAULT NULL,
    `channel_id` INT UNSIGNED DEFAULT NULL,
    `user_id`    BIGINT UNSIGNED DEFAULT NULL,
    `value`      VARCHAR(2000) NOT NULL,
    PRIMARY KEY (`id`),
    FOREIGN KEY `fk_channel_id` (`channel_id`)
        REFERENCES `channels` (`id`)
        ON DELETE SET NULL
        ON UPDATE CASCADE,
    FOREIGN KEY `fk_guild_id` (`guild_id`)
        REFERENCES `guilds` (`id`)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
    FOREIGN KEY `fk_user_id` (`user_id`)
        REFERENCES `users` (`id`)
        ON DELETE SET NULL
        ON UPDATE CASCADE
);
INSERT INTO todos_new (id, guild_id, channel_id, user_id, value) SELECT id, (SELECT guild FROM guilds_old WHERE id = guild_id), channel_id, user_id, value FROM todos;
RENAME TABLE todos TO todos_old;
RENAME TABLE todos_new TO todos;
DROP TABLE todos_old;

-- Update fk on reminder_template to point at new guild table
ALTER TABLE reminder_template
    DROP FOREIGN KEY `reminder_template_ibfk_1`,
    MODIFY COLUMN guild_id BIGINT UNSIGNED NOT NULL,
    ADD FOREIGN KEY `fk_guild_id` (`guild_id`)
        REFERENCES `guilds` (`id`)
        ON DELETE CASCADE
        ON UPDATE CASCADE;
UPDATE reminder_template SET guild_id = (SELECT guild FROM guilds_old WHERE id = guild_id);

-- Update fk on command_macro to point at new guild table
ALTER TABLE command_macro
    DROP FOREIGN KEY `command_macro_ibfk_1`,
    MODIFY COLUMN guild_id BIGINT UNSIGNED NOT NULL,
    ADD FOREIGN KEY `fk_guild_id` (`guild_id`)
        REFERENCES `guilds` (`id`)
        ON DELETE CASCADE
        ON UPDATE CASCADE;
UPDATE command_macro SET guild_id = (SELECT guild FROM guilds_old WHERE id = guild_id);

DROP TABLE guilds_old;

SET FOREIGN_KEY_CHECKS = 1;
