/*
  Warnings:

  - You are about to drop the column `is_active` on the `district_sectors` table. All the data in the column will be lost.
  - A unique constraint covering the columns `[exam_code]` on the table `coupons` will be added. If there are existing duplicate values, this will fail.
  - A unique constraint covering the columns `[exam_official_code]` on the table `coupons` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `group_id` to the `coupons` table without a default value. This is not possible if the table is not empty.
  - Added the required column `coupon_group_id` to the `purchase_orders` table without a default value. This is not possible if the table is not empty.

*/
-- CreateEnum
CREATE TYPE "CouponGroupType" AS ENUM ('NORMAL', 'KIT');

-- CreateEnum
CREATE TYPE "DistrictSectorStatus" AS ENUM ('INACTIVE', 'HOLD', 'ACTIVE');

-- 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 "PurchaseOrderType" ADD VALUE 'INSTITUTE_TO_ADMIN';
ALTER TYPE "PurchaseOrderType" ADD VALUE 'CAREER_ADVISOR_TO_ADMIN';

-- DropIndex
DROP INDEX "district_sectors_is_active_idx";

-- AlterTable
ALTER TABLE "coupons" ADD COLUMN     "exam_code" TEXT,
ADD COLUMN     "exam_official_code" TEXT,
ADD COLUMN     "group_id" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "district_sectors" DROP COLUMN "is_active",
ADD COLUMN     "held_at" TIMESTAMP(3),
ADD COLUMN     "status" "DistrictSectorStatus" NOT NULL DEFAULT 'INACTIVE',
ADD COLUMN     "status_remarks" TEXT;

-- AlterTable
ALTER TABLE "purchase_orders" ADD COLUMN     "coupon_group_id" TEXT NOT NULL;

-- CreateTable
CREATE TABLE "coupon_groups" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "CouponGroupType" NOT NULL,
    "price" DECIMAL(10,2),
    "description" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "includes_exam_coupon" BOOLEAN NOT NULL DEFAULT false,
    "kit_components" JSONB,
    "created_by_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "coupon_groups_name_key" ON "coupon_groups"("name");

-- CreateIndex
CREATE INDEX "coupon_groups_type_idx" ON "coupon_groups"("type");

-- CreateIndex
CREATE INDEX "coupon_groups_is_active_idx" ON "coupon_groups"("is_active");

-- CreateIndex
CREATE UNIQUE INDEX "coupons_exam_code_key" ON "coupons"("exam_code");

-- CreateIndex
CREATE UNIQUE INDEX "coupons_exam_official_code_key" ON "coupons"("exam_official_code");

-- CreateIndex
CREATE INDEX "coupons_group_id_idx" ON "coupons"("group_id");

-- CreateIndex
CREATE INDEX "district_sectors_status_idx" ON "district_sectors"("status");

-- CreateIndex
CREATE INDEX "purchase_orders_coupon_group_id_idx" ON "purchase_orders"("coupon_group_id");

-- AddForeignKey
ALTER TABLE "coupon_groups" ADD CONSTRAINT "coupon_groups_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "coupons" ADD CONSTRAINT "coupons_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "coupon_groups"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_orders" ADD CONSTRAINT "purchase_orders_coupon_group_id_fkey" FOREIGN KEY ("coupon_group_id") REFERENCES "coupon_groups"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
