-- CreateEnum
CREATE TYPE "CouponStatus" AS ENUM ('DRAFT', 'ACTIVE', 'EXPIRED');

-- CreateEnum
CREATE TYPE "CouponValueType" AS ENUM ('FIXED', 'PERCENTAGE');

-- CreateEnum
CREATE TYPE "LuckyDrawStatus" AS ENUM ('DRAFT', 'ACTIVE', 'UPCOMING', 'ENDED');

-- CreateEnum
CREATE TYPE "DesignTemplateScope" AS ENUM ('COUPON', 'LUCKY_DRAW', 'ANY');

-- CreateTable
CREATE TABLE "design_templates" (
    "id" TEXT NOT NULL,
    "shopId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "scope" "DesignTemplateScope" NOT NULL DEFAULT 'ANY',
    "design" JSONB NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "coupons" (
    "id" TEXT NOT NULL,
    "shopId" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "shortDescription" TEXT,
    "longDescription" TEXT,
    "valueType" "CouponValueType" NOT NULL,
    "valueFixed" INTEGER,
    "valuePercent" DOUBLE PRECISION,
    "minOrderValue" INTEGER,
    "totalCoupons" INTEGER,
    "distributionBid" INTEGER,
    "status" "CouponStatus" NOT NULL DEFAULT 'DRAFT',
    "activeAt" TIMESTAMP(3),
    "expiresAt" TIMESTAMP(3),
    "imageUrl" TEXT,
    "design" JSONB,
    "issuedCount" INTEGER NOT NULL DEFAULT 0,
    "redeemedCount" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "lucky_draws" (
    "id" TEXT NOT NULL,
    "shopId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "prize" TEXT,
    "status" "LuckyDrawStatus" NOT NULL DEFAULT 'DRAFT',
    "startDate" TIMESTAMP(3) NOT NULL,
    "endDate" TIMESTAMP(3) NOT NULL,
    "imageUrl" TEXT,
    "design" JSONB,
    "participantsCount" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "design_templates_shopId_createdAt_idx" ON "design_templates"("shopId", "createdAt");

-- CreateIndex
CREATE INDEX "coupons_shopId_createdAt_idx" ON "coupons"("shopId", "createdAt");

-- CreateIndex
CREATE INDEX "lucky_draws_shopId_createdAt_idx" ON "lucky_draws"("shopId", "createdAt");

-- AddForeignKey
ALTER TABLE "design_templates" ADD CONSTRAINT "design_templates_shopId_fkey" FOREIGN KEY ("shopId") REFERENCES "shops"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "coupons" ADD CONSTRAINT "coupons_shopId_fkey" FOREIGN KEY ("shopId") REFERENCES "shops"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lucky_draws" ADD CONSTRAINT "lucky_draws_shopId_fkey" FOREIGN KEY ("shopId") REFERENCES "shops"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
