/*
  Warnings:

  - You are about to drop the column `completed_at` on the `enrollments` table. All the data in the column will be lost.
  - You are about to drop the column `enrolled_at` on the `enrollments` table. All the data in the column will be lost.
  - A unique constraint covering the columns `[coupon_id]` on the table `enrollments` will be added. If there are existing duplicate values, this will fail.

*/
-- DropForeignKey
ALTER TABLE "enrollments" DROP CONSTRAINT "enrollments_course_id_fkey";

-- DropForeignKey
ALTER TABLE "enrollments" DROP CONSTRAINT "enrollments_student_id_fkey";

-- AlterTable
ALTER TABLE "districts" ALTER COLUMN "is_active" SET DEFAULT false;

-- AlterTable
ALTER TABLE "enrollments" DROP COLUMN "completed_at",
DROP COLUMN "enrolled_at",
ADD COLUMN     "coupon_id" TEXT,
ADD COLUMN     "redeemed_career_advisor_id" TEXT,
ADD COLUMN     "redeemed_dso_user_id" TEXT;

-- CreateTable
CREATE TABLE "coupon_redemptions" (
    "id" TEXT NOT NULL,
    "coupon_id" TEXT NOT NULL,
    "student_id" TEXT NOT NULL,
    "course_id" TEXT NOT NULL,
    "dso_user_id" TEXT,
    "institute_id" TEXT,
    "career_advisor_id" TEXT,
    "redeemed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "coupon_redemptions_coupon_id_key" ON "coupon_redemptions"("coupon_id");

-- CreateIndex
CREATE INDEX "coupon_redemptions_coupon_id_idx" ON "coupon_redemptions"("coupon_id");

-- CreateIndex
CREATE INDEX "coupon_redemptions_student_id_idx" ON "coupon_redemptions"("student_id");

-- CreateIndex
CREATE INDEX "coupon_redemptions_course_id_idx" ON "coupon_redemptions"("course_id");

-- CreateIndex
CREATE INDEX "coupon_redemptions_dso_user_id_idx" ON "coupon_redemptions"("dso_user_id");

-- CreateIndex
CREATE UNIQUE INDEX "enrollments_coupon_id_key" ON "enrollments"("coupon_id");

-- CreateIndex
CREATE INDEX "enrollments_coupon_id_idx" ON "enrollments"("coupon_id");

-- CreateIndex
CREATE INDEX "enrollments_career_advisor_id_idx" ON "enrollments"("career_advisor_id");

-- CreateIndex
CREATE INDEX "enrollments_redeemed_dso_user_id_idx" ON "enrollments"("redeemed_dso_user_id");

-- AddForeignKey
ALTER TABLE "coupon_redemptions" ADD CONSTRAINT "coupon_redemptions_coupon_id_fkey" FOREIGN KEY ("coupon_id") REFERENCES "coupons"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "coupon_redemptions" ADD CONSTRAINT "coupon_redemptions_student_id_fkey" FOREIGN KEY ("student_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "coupon_redemptions" ADD CONSTRAINT "coupon_redemptions_course_id_fkey" FOREIGN KEY ("course_id") REFERENCES "courses"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "coupon_redemptions" ADD CONSTRAINT "coupon_redemptions_dso_user_id_fkey" FOREIGN KEY ("dso_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "coupon_redemptions" ADD CONSTRAINT "coupon_redemptions_institute_id_fkey" FOREIGN KEY ("institute_id") REFERENCES "institutes"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "coupon_redemptions" ADD CONSTRAINT "coupon_redemptions_career_advisor_id_fkey" FOREIGN KEY ("career_advisor_id") REFERENCES "career_advisors"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "enrollments" ADD CONSTRAINT "enrollments_student_id_fkey" FOREIGN KEY ("student_id") REFERENCES "students"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "enrollments" ADD CONSTRAINT "enrollments_course_id_fkey" FOREIGN KEY ("course_id") REFERENCES "courses"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "enrollments" ADD CONSTRAINT "enrollments_coupon_id_fkey" FOREIGN KEY ("coupon_id") REFERENCES "coupons"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "enrollments" ADD CONSTRAINT "enrollments_redeemed_dso_user_id_fkey" FOREIGN KEY ("redeemed_dso_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "enrollments" ADD CONSTRAINT "enrollments_redeemed_career_advisor_id_fkey" FOREIGN KEY ("redeemed_career_advisor_id") REFERENCES "career_advisors"("id") ON DELETE SET NULL ON UPDATE CASCADE;
