/*
  Warnings:

  - The values [WAITING_WALLET_CHARGE_AMOUNT,WAITING_WALLET_CHARGE_TARGET_USER,WAITING_WALLET_CHARGE_TARGET_AMOUNT] on the enum `TelegramSessionStep` will be removed. If these variants are still used in the database, this will fail.
  - The values [WALLET_CHARGE] on the enum `TransactionType` will be removed. If these variants are still used in the database, this will fail.
  - You are about to drop the column `authority` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `botPlanId` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `botSubscriptionId` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `discountCodeId` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `finalAmount` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `gateway` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `gatewayRefId` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `paidAt` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `planId` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `status` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the column `subscriptionId` on the `Transaction` table. All the data in the column will be lost.
  - You are about to drop the `Wallet` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `WalletTransaction` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[paymentRequestId]` on the table `Transaction` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `paymentRequestId` to the `Transaction` table without a default value. This is not possible if the table is not empty.

*/
-- CreateEnum
CREATE TYPE "BanStatus" AS ENUM ('NONE', 'TEMPORARY', 'PERMANENT');

-- CreateEnum
CREATE TYPE "PaymentRequestStatus" AS ENUM ('WaitingReceipt', 'WaitingApproval', 'Approved', 'Rejected', 'Expired', 'Canceled');

-- CreateEnum
CREATE TYPE "PaymentRequestType" AS ENUM ('CHANNEL_SUBSCRIPTION', 'BOT_SUBSCRIPTION');

-- AlterEnum
BEGIN;
CREATE TYPE "TelegramSessionStep_new" AS ENUM ('IDLE', 'WAITING_CHANNEL_ADMIN_CONFIRM', 'WAITING_PLAN_TITLE', 'WAITING_PLAN_DURATION_UNIT', 'WAITING_PLAN_DURATION_VALUE', 'WAITING_PLAN_PRICE', 'WAITING_PLAN_EDIT_VALUE', 'WAITING_DISCOUNT_CODE', 'WAITING_MEMBER_SEARCH_QUERY', 'WAITING_BOT_PLAN_TITLE', 'WAITING_BOT_PLAN_DURATION_UNIT', 'WAITING_BOT_PLAN_DURATION_VALUE', 'WAITING_BOT_PLAN_PRICE', 'WAITING_PAYMENT_RECEIPT', 'WAITING_REJECT_REASON', 'WAITING_NEW_DISCOUNT_CODE', 'WAITING_NEW_DISCOUNT_VALUE', 'WAITING_NEW_DISCOUNT_EXPIRY', 'WAITING_NEW_DISCOUNT_MAX_USAGE', 'WAITING_NEW_DISCOUNT_EXPIRY_DAYS', 'WAITING_PLAN_CONFIRM', 'WAITING_BOT_PLAN_CONFIRM', 'WAITING_BROADCAST_CONTENT', 'WAITING_ADMIN_DISCOUNT_CODE', 'WAITING_ADMIN_DISCOUNT_VALUE', 'WAITING_ADMIN_DISCOUNT_MAX_USAGE', 'WAITING_ADMIN_DISCOUNT_EXPIRY_DAYS', 'WAITING_BANK_CARD_NUMBER', 'WAITING_BANK_CARD_HOLDER');
ALTER TABLE "public"."TelegramSession" ALTER COLUMN "step" DROP DEFAULT;
ALTER TABLE "TelegramSession" ALTER COLUMN "step" TYPE "TelegramSessionStep_new" USING ("step"::text::"TelegramSessionStep_new");
ALTER TYPE "TelegramSessionStep" RENAME TO "TelegramSessionStep_old";
ALTER TYPE "TelegramSessionStep_new" RENAME TO "TelegramSessionStep";
DROP TYPE "public"."TelegramSessionStep_old";
ALTER TABLE "TelegramSession" ALTER COLUMN "step" SET DEFAULT 'IDLE';
COMMIT;

-- AlterEnum
BEGIN;
CREATE TYPE "TransactionType_new" AS ENUM ('BOT_SUBSCRIPTION', 'CHANNEL_SUBSCRIPTION', 'REFUND', 'MANUAL_ADJUSTMENT', 'PLAN_PURCHASE', 'PLAN_RENEWAL', 'PLAN_UPGRADE', 'BOT_SUBSCRIPTION_PURCHASE', 'BOT_SUBSCRIPTION_RENEWAL');
ALTER TABLE "Transaction" ALTER COLUMN "type" TYPE "TransactionType_new" USING ("type"::text::"TransactionType_new");
ALTER TYPE "TransactionType" RENAME TO "TransactionType_old";
ALTER TYPE "TransactionType_new" RENAME TO "TransactionType";
DROP TYPE "public"."TransactionType_old";
COMMIT;

-- DropForeignKey
ALTER TABLE "Transaction" DROP CONSTRAINT "Transaction_botPlanId_fkey";

-- DropForeignKey
ALTER TABLE "Transaction" DROP CONSTRAINT "Transaction_botSubscriptionId_fkey";

-- DropForeignKey
ALTER TABLE "Transaction" DROP CONSTRAINT "Transaction_discountCodeId_fkey";

-- DropForeignKey
ALTER TABLE "Transaction" DROP CONSTRAINT "Transaction_planId_fkey";

-- DropForeignKey
ALTER TABLE "Transaction" DROP CONSTRAINT "Transaction_subscriptionId_fkey";

-- DropForeignKey
ALTER TABLE "Wallet" DROP CONSTRAINT "Wallet_userId_fkey";

-- DropForeignKey
ALTER TABLE "WalletTransaction" DROP CONSTRAINT "WalletTransaction_performedByUserId_fkey";

-- DropForeignKey
ALTER TABLE "WalletTransaction" DROP CONSTRAINT "WalletTransaction_transactionId_fkey";

-- DropForeignKey
ALTER TABLE "WalletTransaction" DROP CONSTRAINT "WalletTransaction_walletId_fkey";

-- DropIndex
DROP INDEX "Transaction_authority_key";

-- DropIndex
DROP INDEX "Transaction_botPlanId_idx";

-- DropIndex
DROP INDEX "Transaction_planId_idx";

-- DropIndex
DROP INDEX "Transaction_status_paidAt_idx";

-- AlterTable
ALTER TABLE "Owner" ADD COLUMN     "banReason" TEXT,
ADD COLUMN     "banStatus" "BanStatus" NOT NULL DEFAULT 'NONE',
ADD COLUMN     "bannedUntil" TIMESTAMP(3);

-- AlterTable
ALTER TABLE "Transaction" DROP COLUMN "authority",
DROP COLUMN "botPlanId",
DROP COLUMN "botSubscriptionId",
DROP COLUMN "discountCodeId",
DROP COLUMN "finalAmount",
DROP COLUMN "gateway",
DROP COLUMN "gatewayRefId",
DROP COLUMN "paidAt",
DROP COLUMN "planId",
DROP COLUMN "status",
DROP COLUMN "subscriptionId",
ADD COLUMN     "paymentRequestId" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "User" ADD COLUMN     "banReason" TEXT,
ADD COLUMN     "banStatus" "BanStatus" NOT NULL DEFAULT 'NONE',
ADD COLUMN     "bannedUntil" TIMESTAMP(3);

-- DropTable
DROP TABLE "Wallet";

-- DropTable
DROP TABLE "WalletTransaction";

-- DropEnum
DROP TYPE "TransactionStatus";

-- DropEnum
DROP TYPE "WalletTxType";

-- CreateTable
CREATE TABLE "BankCard" (
    "id" TEXT NOT NULL,
    "cardNumber" TEXT NOT NULL,
    "holderName" TEXT NOT NULL,
    "isDefault" BOOLEAN NOT NULL DEFAULT false,
    "ownerId" TEXT,
    "adminUserId" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "BankCard_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "PaymentRequest" (
    "id" TEXT NOT NULL,
    "paymentCode" TEXT NOT NULL,
    "type" "PaymentRequestType" NOT NULL,
    "amount" INTEGER NOT NULL,
    "payerId" TEXT NOT NULL,
    "receiverOwnerId" TEXT,
    "receiverAdminUserId" TEXT,
    "bankCardId" TEXT NOT NULL,
    "planId" TEXT,
    "botPlanId" TEXT,
    "subscriptionId" TEXT,
    "renewOrUpgrade" TEXT,
    "receiptFileId" TEXT,
    "rejectReason" TEXT,
    "status" "PaymentRequestStatus" NOT NULL DEFAULT 'WaitingReceipt',
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "PaymentRequest_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "BankCard_ownerId_idx" ON "BankCard"("ownerId");

-- CreateIndex
CREATE INDEX "BankCard_adminUserId_idx" ON "BankCard"("adminUserId");

-- CreateIndex
CREATE UNIQUE INDEX "PaymentRequest_paymentCode_key" ON "PaymentRequest"("paymentCode");

-- CreateIndex
CREATE INDEX "PaymentRequest_payerId_idx" ON "PaymentRequest"("payerId");

-- CreateIndex
CREATE INDEX "PaymentRequest_receiverOwnerId_idx" ON "PaymentRequest"("receiverOwnerId");

-- CreateIndex
CREATE INDEX "PaymentRequest_status_expiresAt_idx" ON "PaymentRequest"("status", "expiresAt");

-- CreateIndex
CREATE UNIQUE INDEX "Transaction_paymentRequestId_key" ON "Transaction"("paymentRequestId");

-- AddForeignKey
ALTER TABLE "Transaction" ADD CONSTRAINT "Transaction_paymentRequestId_fkey" FOREIGN KEY ("paymentRequestId") REFERENCES "PaymentRequest"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BankCard" ADD CONSTRAINT "BankCard_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "Owner"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BankCard" ADD CONSTRAINT "BankCard_adminUserId_fkey" FOREIGN KEY ("adminUserId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PaymentRequest" ADD CONSTRAINT "PaymentRequest_payerId_fkey" FOREIGN KEY ("payerId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PaymentRequest" ADD CONSTRAINT "PaymentRequest_receiverOwnerId_fkey" FOREIGN KEY ("receiverOwnerId") REFERENCES "Owner"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PaymentRequest" ADD CONSTRAINT "PaymentRequest_receiverAdminUserId_fkey" FOREIGN KEY ("receiverAdminUserId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PaymentRequest" ADD CONSTRAINT "PaymentRequest_bankCardId_fkey" FOREIGN KEY ("bankCardId") REFERENCES "BankCard"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PaymentRequest" ADD CONSTRAINT "PaymentRequest_planId_fkey" FOREIGN KEY ("planId") REFERENCES "Plan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PaymentRequest" ADD CONSTRAINT "PaymentRequest_botPlanId_fkey" FOREIGN KEY ("botPlanId") REFERENCES "BotPlan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PaymentRequest" ADD CONSTRAINT "PaymentRequest_subscriptionId_fkey" FOREIGN KEY ("subscriptionId") REFERENCES "Subscription"("id") ON DELETE SET NULL ON UPDATE CASCADE;
