/*
Consumer profile split migration.

Existing profile data is copied into `individual_consumers` before the source
columns are removed from `consumers`.
*/
-- AlterTable
ALTER TABLE `consumers`
ADD COLUMN `type` ENUM('INDIVIDUAL', 'LEGAL') NOT NULL DEFAULT 'INDIVIDUAL';

UPDATE `consumers` SET `type` = 'INDIVIDUAL';

-- DropForeignKey
ALTER TABLE `consumers` DROP FOREIGN KEY `consumers_partner_id_fkey`;

-- DropIndex
DROP INDEX `consumers_mobile_number_partner_id_key` ON `consumers`;

-- DropIndex
DROP INDEX `consumers_national_code_partner_id_key` ON `consumers`;

-- DropIndex
DROP INDEX `consumers_partner_id_fkey` ON `consumers`;

-- CreateTable
CREATE TABLE `individual_consumers` (
    `first_name` VARCHAR(191) NOT NULL,
    `last_name` VARCHAR(191) NOT NULL,
    `mobile_number` VARCHAR(191) NOT NULL,
    `national_code` VARCHAR(191) NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `partner_id` VARCHAR(191) NOT NULL,
    `consumer_id` VARCHAR(191) NOT NULL,
    UNIQUE INDEX `individual_consumers_mobile_number_consumer_id_key` (
        `mobile_number`,
        `consumer_id`
    ),
    UNIQUE INDEX `individual_consumers_partner_id_national_code_key` (`partner_id`, `national_code`),
    PRIMARY KEY (`consumer_id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `legal_consumers` (
    `name` VARCHAR(191) NOT NULL,
    `registration_code` VARCHAR(191) NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `partner_id` VARCHAR(191) NOT NULL,
    `consumer_id` VARCHAR(191) NOT NULL,
    UNIQUE INDEX `legal_consumers_partner_id_registration_code_key` (
        `partner_id`,
        `registration_code`
    ),
    PRIMARY KEY (`consumer_id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

INSERT INTO
    `individual_consumers` (
        `first_name`,
        `last_name`,
        `mobile_number`,
        `national_code`,
        `created_at`,
        `updated_at`,
        `partner_id`,
        `consumer_id`
    )
SELECT
    `first_name`,
    `last_name`,
    `mobile_number`,
    `national_code`,
    `created_at`,
    `updated_at`,
    `partner_id`,
    `id`
FROM `consumers`;

ALTER TABLE `consumers`
DROP COLUMN `first_name`,
DROP COLUMN `last_name`,
DROP COLUMN `mobile_number`,
DROP COLUMN `national_code`,
DROP COLUMN `partner_id`,
MODIFY `type` ENUM('INDIVIDUAL', 'LEGAL') NOT NULL;

-- AddForeignKey
ALTER TABLE `individual_consumers`
ADD CONSTRAINT `individual_consumers_partner_id_fkey` FOREIGN KEY (`partner_id`) REFERENCES `partners` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `individual_consumers`
ADD CONSTRAINT `individual_consumers_consumer_id_fkey` FOREIGN KEY (`consumer_id`) REFERENCES `consumers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `legal_consumers`
ADD CONSTRAINT `legal_consumers_partner_id_fkey` FOREIGN KEY (`partner_id`) REFERENCES `partners` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `legal_consumers`
ADD CONSTRAINT `legal_consumers_consumer_id_fkey` FOREIGN KEY (`consumer_id`) REFERENCES `consumers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
