/*
  Warnings:

  - The `type` column on the `notifications` table would be dropped and recreated. This will lead to data loss if there is data in the column.

*/
-- CreateEnum
CREATE TYPE "NotificationType" AS ENUM ('BOT_ASSIGNED');

-- CreateEnum
CREATE TYPE "QuotaGrantType" AS ENUM ('BASE', 'BONUS');

-- DropForeignKey
ALTER TABLE "public"."bots" DROP CONSTRAINT "bots_created_by_fkey";

-- DropForeignKey
ALTER TABLE "public"."student_bot_access" DROP CONSTRAINT "student_bot_access_bot_id_fkey";

-- DropIndex
DROP INDEX "public"."conversations_feedback_status_idx";

-- DropIndex
DROP INDEX "public"."notifications_created_at_idx";

-- DropIndex
DROP INDEX "public"."notifications_is_read_idx";

-- DropIndex
DROP INDEX "public"."notifications_user_id_idx";

-- DropIndex
DROP INDEX "public"."openai_config_is_active_idx";

-- AlterTable
ALTER TABLE "bots" ADD COLUMN     "feedback" TEXT,
ADD COLUMN     "is_timer_enabled" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "max_usage_seconds" INTEGER,
ALTER COLUMN "created_by" DROP NOT NULL;

-- AlterTable
ALTER TABLE "notifications" DROP COLUMN "type",
ADD COLUMN     "type" "NotificationType" NOT NULL DEFAULT 'BOT_ASSIGNED';

-- AlterTable
ALTER TABLE "openai_config" ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "updated_at" DROP DEFAULT;

-- CreateTable
CREATE TABLE "system_config" (
    "id" TEXT NOT NULL,
    "quota_management_enabled" BOOLEAN NOT NULL DEFAULT true,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "quota_grant" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "period_start" TIMESTAMP(3) NOT NULL,
    "period_end" TIMESTAMP(3) NOT NULL,
    "seconds" INTEGER NOT NULL,
    "type" "QuotaGrantType" NOT NULL DEFAULT 'BONUS',
    "reason" TEXT,
    "expires_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "usage_ledger" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "seconds" INTEGER NOT NULL,
    "reason" TEXT,
    "source" TEXT,
    "related_conversation_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "quota_grant_user_id_period_start_period_end_idx" ON "quota_grant"("user_id", "period_start", "period_end");

-- CreateIndex
CREATE UNIQUE INDEX "quota_grant_user_id_period_start_type_key" ON "quota_grant"("user_id", "period_start", "type");

-- CreateIndex
CREATE UNIQUE INDEX "usage_ledger_related_conversation_id_key" ON "usage_ledger"("related_conversation_id");

-- CreateIndex
CREATE INDEX "usage_ledger_user_id_created_at_idx" ON "usage_ledger"("user_id", "created_at");

-- AddForeignKey
ALTER TABLE "bots" ADD CONSTRAINT "bots_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "student_bot_access" ADD CONSTRAINT "student_bot_access_bot_id_fkey" FOREIGN KEY ("bot_id") REFERENCES "bots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "quota_grant" ADD CONSTRAINT "quota_grant_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "usage_ledger" ADD CONSTRAINT "usage_ledger_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
