/*
  Warnings:

  - You are about to drop the column `charged_license_transaction_id` on the `licenses` table. All the data in the column will be lost.
  - You are about to drop the `activated_licenses` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `charged_license_transactions` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[mobile_number,partner_id]` on the table `consumers` will be added. If there are existing duplicate values, this will fail.
  - A unique constraint covering the columns `[national_code,partner_id]` on the table `consumers` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `charge_transaction_id` to the `licenses` table without a default value. This is not possible if the table is not empty.

*/
-- DropForeignKey
ALTER TABLE `activated_licenses` DROP FOREIGN KEY `activated_licenses_consumer_id_fkey`;

-- DropForeignKey
ALTER TABLE `activated_licenses` DROP FOREIGN KEY `activated_licenses_license_id_fkey`;

-- DropForeignKey
ALTER TABLE `charged_license_transactions` DROP FOREIGN KEY `charged_license_transactions_partner_id_fkey`;

-- DropForeignKey
ALTER TABLE `licenses` DROP FOREIGN KEY `licenses_charged_license_transaction_id_fkey`;

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

-- DropIndex
DROP INDEX `licenses_charged_license_transaction_id_fkey` ON `licenses`;

-- AlterTable
ALTER TABLE `licenses` DROP COLUMN `charged_license_transaction_id`,
    ADD COLUMN `charge_transaction_id` VARCHAR(191) NOT NULL;

-- DropTable
DROP TABLE `activated_licenses`;

-- DropTable
DROP TABLE `charged_license_transactions`;

-- CreateTable
CREATE TABLE `licenses_activated` (
    `id` VARCHAR(191) NOT NULL,
    `starts_at` DATETIME(3) NOT NULL,
    `expires_at` DATETIME(3) NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `license_id` VARCHAR(191) NOT NULL,
    `business_activity_id` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `licenses_activated_id_key`(`id`),
    UNIQUE INDEX `licenses_activated_license_id_key`(`license_id`),
    UNIQUE INDEX `licenses_activated_business_activity_id_key`(`business_activity_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `license_charged_transactions` (
    `id` VARCHAR(191) NOT NULL,
    `activation_expires_at` DATETIME(3) NOT NULL,
    `tracking_code` VARCHAR(191) NOT NULL,
    `purchased_count` INTEGER NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `partner_id` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `license_charged_transactions_tracking_code_key`(`tracking_code`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `license_renew` (
    `id` VARCHAR(191) NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `charge_transaction_id` VARCHAR(191) NOT NULL,
    `activation_id` VARCHAR(191) NOT NULL,

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

-- CreateTable
CREATE TABLE `license_renew_charge_transaction` (
    `id` VARCHAR(191) NOT NULL,
    `activation_expires_at` DATETIME(3) NOT NULL,
    `tracking_code` VARCHAR(191) NOT NULL,
    `purchased_count` INTEGER NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `partner_id` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `license_renew_charge_transaction_tracking_code_key`(`tracking_code`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `partner_account_quota_charge_transaction` (
    `id` VARCHAR(191) NOT NULL,
    `purchased_count` INTEGER NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `partner_id` VARCHAR(191) NOT NULL,

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

-- CreateTable
CREATE TABLE `partner_account_quota_allocation` (
    `id` VARCHAR(191) NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `charge_transaction_id` VARCHAR(191) NOT NULL,
    `license_id` VARCHAR(191) NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX `consumers_mobile_number_partner_id_key` ON `consumers`(`mobile_number`, `partner_id`);

-- CreateIndex
CREATE UNIQUE INDEX `consumers_national_code_partner_id_key` ON `consumers`(`national_code`, `partner_id`);

-- AddForeignKey
ALTER TABLE `licenses` ADD CONSTRAINT `licenses_charge_transaction_id_fkey` FOREIGN KEY (`charge_transaction_id`) REFERENCES `license_charged_transactions`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `licenses_activated` ADD CONSTRAINT `licenses_activated_license_id_fkey` FOREIGN KEY (`license_id`) REFERENCES `licenses`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `licenses_activated` ADD CONSTRAINT `licenses_activated_business_activity_id_fkey` FOREIGN KEY (`business_activity_id`) REFERENCES `business_activities`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE `license_renew` ADD CONSTRAINT `license_renew_charge_transaction_id_fkey` FOREIGN KEY (`charge_transaction_id`) REFERENCES `license_renew_charge_transaction`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `license_renew` ADD CONSTRAINT `license_renew_activation_id_fkey` FOREIGN KEY (`activation_id`) REFERENCES `licenses_activated`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE `partner_account_quota_allocation` ADD CONSTRAINT `partner_account_quota_allocation_charge_transaction_id_fkey` FOREIGN KEY (`charge_transaction_id`) REFERENCES `partner_account_quota_charge_transaction`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `partner_account_quota_allocation` ADD CONSTRAINT `partner_account_quota_allocation_license_id_fkey` FOREIGN KEY (`license_id`) REFERENCES `licenses`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
