/*
  Warnings:

  - You are about to drop the column `id_users_category` on the `users` table. All the data in the column will be lost.
  - You are about to drop the `category_users` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `media_users_categories` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `users_categories` table. If the table is not empty, all the data it contains will be lost.
  - Added the required column `id_group` to the `users` table without a default value. This is not possible if the table is not empty.

*/
-- DropForeignKey
ALTER TABLE `category_users` DROP FOREIGN KEY `category_users_id_category_fkey`;

-- DropForeignKey
ALTER TABLE `category_users` DROP FOREIGN KEY `category_users_id_user_fkey`;

-- DropForeignKey
ALTER TABLE `media_users_categories` DROP FOREIGN KEY `media_users_categories_id_media_category_fkey`;

-- DropForeignKey
ALTER TABLE `media_users_categories` DROP FOREIGN KEY `media_users_categories_id_users_category_fkey`;

-- DropForeignKey
ALTER TABLE `users` DROP FOREIGN KEY `users_id_users_category_fkey`;

-- DropIndex
DROP INDEX `users_id_users_category_fkey` ON `users`;

-- AlterTable
ALTER TABLE `users` DROP COLUMN `id_users_category`,
    ADD COLUMN `id_group` VARCHAR(191) NOT NULL;

-- DropTable
DROP TABLE `category_users`;

-- DropTable
DROP TABLE `media_users_categories`;

-- DropTable
DROP TABLE `users_categories`;

-- CreateTable
CREATE TABLE `groups` (
    `id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `deleted_at` DATETIME(3) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `media_categories_users_groups` (
    `id` VARCHAR(191) NOT NULL,
    `id_group` VARCHAR(191) NOT NULL,
    `id_media_category` VARCHAR(191) NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `deleted_at` DATETIME(3) NULL,

    UNIQUE INDEX `media_categories_users_groups_id_group_id_media_category_key`(`id_group`, `id_media_category`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `user_groups` (
    `id` VARCHAR(191) NOT NULL,
    `id_group` VARCHAR(191) NOT NULL,
    `id_user` VARCHAR(191) NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `deleted_at` DATETIME(3) NULL,

    UNIQUE INDEX `user_groups_id_group_id_user_key`(`id_group`, `id_user`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `users` ADD CONSTRAINT `users_id_group_fkey` FOREIGN KEY (`id_group`) REFERENCES `groups`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `media_categories_users_groups` ADD CONSTRAINT `media_categories_users_groups_id_group_fkey` FOREIGN KEY (`id_group`) REFERENCES `groups`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `media_categories_users_groups` ADD CONSTRAINT `media_categories_users_groups_id_media_category_fkey` FOREIGN KEY (`id_media_category`) REFERENCES `media_categories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `user_groups` ADD CONSTRAINT `user_groups_id_group_fkey` FOREIGN KEY (`id_group`) REFERENCES `groups`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `user_groups` ADD CONSTRAINT `user_groups_id_user_fkey` FOREIGN KEY (`id_user`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
