-- CreateEnum
CREATE TYPE "CheckoutSessionStatus" AS ENUM ('pending', 'paid', 'retrieved', 'expired', 'failed');

-- CreateTable
CREATE TABLE "checkout_sessions" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "user_id" UUID NOT NULL,
    "plugin" VARCHAR(30) NOT NULL,
    "site_url" TEXT NOT NULL,
    "plan_tier" VARCHAR(50) NOT NULL,
    "billing_cycle" "BillingCycle" NOT NULL,
    "paypal_subscription_id" TEXT,
    "status" "CheckoutSessionStatus" NOT NULL DEFAULT 'pending',
    "retrieved_at" TIMESTAMP(3),
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "checkout_sessions_paypal_subscription_id_key" ON "checkout_sessions"("paypal_subscription_id");

-- CreateIndex
CREATE INDEX "checkout_sessions_user_id_idx" ON "checkout_sessions"("user_id");

-- CreateIndex
CREATE INDEX "checkout_sessions_status_expires_at_idx" ON "checkout_sessions"("status", "expires_at");

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