/*
  Warnings:

  - You are about to drop the column `subscriptionPlanId` on the `student` table. All the data in the column will be lost.
  - You are about to drop the column `autoRenew` on the `subscription` table. All the data in the column will be lost.
  - You are about to drop the column `trialEndsAt` on the `subscription` table. All the data in the column will be lost.
  - You are about to alter the column `status` on the `subscription` table. The data in that column could be lost. The data in that column will be cast from `Enum(EnumId(0))` to `Enum(EnumId(6))`.
  - You are about to drop the column `offer` on the `transaction` table. All the data in the column will be lost.
  - You are about to drop the column `studentId` on the `transaction` table. All the data in the column will be lost.
  - You are about to drop the column `transactionId` on the `transaction` table. All the data in the column will be lost.
  - Made the column `email` on table `subscription` required. This step will fail if there are existing NULL values in that column.
  - Added the required column `updatedAt` to the `transaction` table without a default value. This is not possible if the table is not empty.

*/
-- DropForeignKey
ALTER TABLE `student` DROP FOREIGN KEY `student_subscriptionPlanId_fkey`;

-- DropForeignKey
ALTER TABLE `subscription` DROP FOREIGN KEY `subscription_institutionId_fkey`;

-- DropForeignKey
ALTER TABLE `subscription` DROP FOREIGN KEY `subscription_planId_fkey`;

-- DropForeignKey
ALTER TABLE `subscription` DROP FOREIGN KEY `subscription_studentId_fkey`;

-- DropForeignKey
ALTER TABLE `transaction` DROP FOREIGN KEY `transaction_institutionId_fkey`;

-- DropForeignKey
ALTER TABLE `transaction` DROP FOREIGN KEY `transaction_studentId_fkey`;

-- DropIndex
DROP INDEX `student_subscriptionPlanId_fkey` ON `student`;

-- DropIndex
DROP INDEX `subscription_email_key` ON `subscription`;

-- DropIndex
DROP INDEX `subscription_institutionId_idx` ON `subscription`;

-- DropIndex
DROP INDEX `subscription_planId_fkey` ON `subscription`;

-- DropIndex
DROP INDEX `subscription_studentId_key` ON `subscription`;

-- DropIndex
DROP INDEX `transaction_institutionId_idx` ON `transaction`;

-- DropIndex
DROP INDEX `transaction_studentId_fkey` ON `transaction`;

-- AlterTable
ALTER TABLE `student` DROP COLUMN `subscriptionPlanId`;

-- AlterTable
ALTER TABLE `subscription` DROP COLUMN `autoRenew`,
    DROP COLUMN `trialEndsAt`,
    ADD COLUMN `action` ENUM('TRIAL', 'RENEW', 'UPGRADE') NOT NULL DEFAULT 'TRIAL',
    ADD COLUMN `chargedAmount` DECIMAL(10, 2) NOT NULL DEFAULT 0,
    ADD COLUMN `creditUsed` DECIMAL(10, 2) NOT NULL DEFAULT 0,
    ADD COLUMN `isCurrent` BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN `previousSubscriptionId` VARCHAR(191) NULL,
    ADD COLUMN `updatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    MODIFY `status` ENUM('ACTIVE', 'EXPIRED') NOT NULL DEFAULT 'ACTIVE',
    MODIFY `amount` DECIMAL(10, 2) NOT NULL DEFAULT 0,
    MODIFY `email` VARCHAR(191) NOT NULL;

-- AlterTable
ALTER TABLE `subscriptionplan` ADD COLUMN `isDeleted` BOOLEAN NOT NULL DEFAULT false,
    MODIFY `actualPrice` DECIMAL(10, 2) NOT NULL DEFAULT 0,
    MODIFY `discountPercentage` DECIMAL(10, 2) NOT NULL DEFAULT 0,
    MODIFY `gstPercentage` DECIMAL(10, 2) NOT NULL DEFAULT 0,
    MODIFY `priceAfterDiscount` DECIMAL(10, 2) NOT NULL DEFAULT 0;

-- AlterTable
ALTER TABLE `transaction` DROP COLUMN `offer`,
    DROP COLUMN `studentId`,
    DROP COLUMN `transactionId`,
    ADD COLUMN `failureReason` VARCHAR(191) NULL,
    ADD COLUMN `metadata` JSON NULL,
    ADD COLUMN `orderId` VARCHAR(191) NULL,
    ADD COLUMN `paymentId` VARCHAR(191) NULL,
    ADD COLUMN `signature` VARCHAR(191) NULL,
    ADD COLUMN `status` ENUM('PENDING', 'SUCCESS', 'FAILED', 'CANCELLED', 'EXPIRED', 'REFUNDED', 'PARTIAL_REFUND') NOT NULL DEFAULT 'PENDING',
    ADD COLUMN `updatedAt` DATETIME(3) NOT NULL,
    MODIFY `amount` DECIMAL(10, 2) NOT NULL DEFAULT 0;

-- CreateTable
CREATE TABLE `transaction_receipt` (
    `id` VARCHAR(191) NOT NULL,
    `receiptNo` VARCHAR(191) NOT NULL,
    `totalAmount` DECIMAL(10, 2) NOT NULL,
    `currency` VARCHAR(191) NOT NULL DEFAULT 'INR',
    `status` ENUM('PAID', 'UNPAID', 'OVERDUE', 'FAILED') NOT NULL DEFAULT 'PAID',
    `issuedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `institutionId` VARCHAR(191) NULL,
    `transactionId` VARCHAR(191) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `transaction_receipt_receiptNo_key`(`receiptNo`),
    UNIQUE INDEX `transaction_receipt_transactionId_key`(`transactionId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `transaction_receipt_item` (
    `id` VARCHAR(191) NOT NULL,
    `receiptId` VARCHAR(191) NOT NULL,
    `subscriptionId` VARCHAR(191) NOT NULL,
    `studentId` VARCHAR(191) NULL,
    `amount` DECIMAL(10, 2) NOT NULL,

    UNIQUE INDEX `transaction_receipt_item_receiptId_subscriptionId_key`(`receiptId`, `subscriptionId`),
    UNIQUE INDEX `transaction_receipt_item_receiptId_studentId_key`(`receiptId`, `studentId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateIndex
CREATE INDEX `subscription_studentId_isCurrent_idx` ON `subscription`(`studentId`, `isCurrent`);

-- CreateIndex
CREATE INDEX `subscription_email_isCurrent_idx` ON `subscription`(`email`, `isCurrent`);

-- CreateIndex
CREATE INDEX `subscription_status_isCurrent_idx` ON `subscription`(`status`, `isCurrent`);

-- CreateIndex
CREATE INDEX `transaction_status_idx` ON `transaction`(`status`);

-- AddForeignKey
ALTER TABLE `subscription` ADD CONSTRAINT `subscription_previousSubscriptionId_fkey` FOREIGN KEY (`previousSubscriptionId`) REFERENCES `subscription`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `subscription` ADD CONSTRAINT `subscription_institutionId_fkey` FOREIGN KEY (`institutionId`) REFERENCES `institution`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `subscription` ADD CONSTRAINT `subscription_planId_fkey` FOREIGN KEY (`planId`) REFERENCES `subscriptionplan`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transaction` ADD CONSTRAINT `transaction_institutionId_fkey` FOREIGN KEY (`institutionId`) REFERENCES `institution`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transaction_receipt` ADD CONSTRAINT `transaction_receipt_institutionId_fkey` FOREIGN KEY (`institutionId`) REFERENCES `institution`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transaction_receipt` ADD CONSTRAINT `transaction_receipt_transactionId_fkey` FOREIGN KEY (`transactionId`) REFERENCES `transaction`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transaction_receipt_item` ADD CONSTRAINT `transaction_receipt_item_receiptId_fkey` FOREIGN KEY (`receiptId`) REFERENCES `transaction_receipt`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transaction_receipt_item` ADD CONSTRAINT `transaction_receipt_item_subscriptionId_fkey` FOREIGN KEY (`subscriptionId`) REFERENCES `subscription`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transaction_receipt_item` ADD CONSTRAINT `transaction_receipt_item_studentId_fkey` FOREIGN KEY (`studentId`) REFERENCES `student`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `subscription` ADD CONSTRAINT `subscription_studentId_fkey` FOREIGN KEY (`studentId`) REFERENCES `student`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
