/*
  Warnings:

  - You are about to drop the column `note` on the `WalletTransaction` table. All the data in the column will be lost.
  - A unique constraint covering the columns `[transactionId]` on the table `WalletTransaction` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `scope` to the `DiscountCode` table without a default value. This is not possible if the table is not empty.
  - Changed the type of `type` on the `WalletTransaction` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.

*/
-- CreateEnum
CREATE TYPE "DiscountScope" AS ENUM ('CHANNEL_PLAN', 'BOT_PLAN');

-- CreateEnum
CREATE TYPE "WalletTxType" AS ENUM ('CHARGE_GATEWAY', 'CHARGE_MANUAL', 'PURCHASE', 'REFUND');

-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.


ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_BOT_PLAN_TITLE';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_BOT_PLAN_DURATION_UNIT';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_BOT_PLAN_DURATION_VALUE';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_BOT_PLAN_PRICE';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_WALLET_CHARGE_AMOUNT';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_WALLET_CHARGE_TARGET_USER';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_WALLET_CHARGE_TARGET_AMOUNT';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_NEW_DISCOUNT_CODE';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_NEW_DISCOUNT_VALUE';
ALTER TYPE "TelegramSessionStep" ADD VALUE 'WAITING_NEW_DISCOUNT_EXPIRY';

-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.


ALTER TYPE "TransactionType" ADD VALUE 'BOT_SUBSCRIPTION_PURCHASE';
ALTER TYPE "TransactionType" ADD VALUE 'BOT_SUBSCRIPTION_RENEWAL';

-- DropForeignKey
ALTER TABLE "DiscountCode" DROP CONSTRAINT "DiscountCode_ownerId_fkey";

-- AlterTable
ALTER TABLE "DiscountCode" ADD COLUMN     "botPlanId" TEXT,
ADD COLUMN     "scope" "DiscountScope" NOT NULL,
ALTER COLUMN "ownerId" DROP NOT NULL;

-- AlterTable
ALTER TABLE "Transaction" ADD COLUMN     "botPlanId" TEXT,
ADD COLUMN     "botSubscriptionId" TEXT;

-- AlterTable
ALTER TABLE "Wallet" ADD COLUMN     "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;

-- AlterTable
ALTER TABLE "WalletTransaction" DROP COLUMN "note",
ADD COLUMN     "description" TEXT,
ADD COLUMN     "performedByUserId" TEXT,
ADD COLUMN     "transactionId" TEXT,
DROP COLUMN "type",
ADD COLUMN     "type" "WalletTxType" NOT NULL;

-- DropEnum
DROP TYPE "WalletTransactionType";

-- CreateTable
CREATE TABLE "BotPlan" (
    "id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "price" INTEGER NOT NULL,
    "durationUnit" "DurationUnit" NOT NULL,
    "durationValue" INTEGER NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "BotSubscription" (
    "id" TEXT NOT NULL,
    "ownerId" TEXT NOT NULL,
    "botPlanId" TEXT NOT NULL,
    "status" "SubscriptionStatus" NOT NULL,
    "startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expiresAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "BotSubscription_ownerId_idx" ON "BotSubscription"("ownerId");

-- CreateIndex
CREATE INDEX "BotSubscription_botPlanId_idx" ON "BotSubscription"("botPlanId");

-- CreateIndex
CREATE INDEX "Broadcast_ownerId_idx" ON "Broadcast"("ownerId");

-- CreateIndex
CREATE INDEX "Channel_ownerId_idx" ON "Channel"("ownerId");

-- CreateIndex
CREATE INDEX "DiscountCode_ownerId_idx" ON "DiscountCode"("ownerId");

-- CreateIndex
CREATE INDEX "Plan_channelId_idx" ON "Plan"("channelId");

-- CreateIndex
CREATE INDEX "Subscription_userId_idx" ON "Subscription"("userId");

-- CreateIndex
CREATE INDEX "Subscription_channelId_idx" ON "Subscription"("channelId");

-- CreateIndex
CREATE INDEX "Transaction_userId_idx" ON "Transaction"("userId");

-- CreateIndex
CREATE INDEX "Transaction_planId_idx" ON "Transaction"("planId");

-- CreateIndex
CREATE INDEX "Transaction_botPlanId_idx" ON "Transaction"("botPlanId");

-- CreateIndex
CREATE UNIQUE INDEX "WalletTransaction_transactionId_key" ON "WalletTransaction"("transactionId");

-- CreateIndex
CREATE INDEX "WalletTransaction_walletId_idx" ON "WalletTransaction"("walletId");

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

-- AddForeignKey
ALTER TABLE "Transaction" ADD CONSTRAINT "Transaction_botSubscriptionId_fkey" FOREIGN KEY ("botSubscriptionId") REFERENCES "BotSubscription"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BotSubscription" ADD CONSTRAINT "BotSubscription_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "Owner"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BotSubscription" ADD CONSTRAINT "BotSubscription_botPlanId_fkey" FOREIGN KEY ("botPlanId") REFERENCES "BotPlan"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

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

-- AddForeignKey
ALTER TABLE "WalletTransaction" ADD CONSTRAINT "WalletTransaction_transactionId_fkey" FOREIGN KEY ("transactionId") REFERENCES "Transaction"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "WalletTransaction" ADD CONSTRAINT "WalletTransaction_performedByUserId_fkey" FOREIGN KEY ("performedByUserId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
