-- CreateTable
CREATE TABLE "payment_compensations" (
    "compensation_id" BIGSERIAL NOT NULL,
    "order_no" VARCHAR(50) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "compensation_type" VARCHAR(50) NOT NULL,
    "amount" DECIMAL(20,8) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "failure_reason" TEXT,
    "failure_stage" VARCHAR(50),
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "max_retries" INTEGER NOT NULL DEFAULT 5,
    "next_retry_at" TIMESTAMP(3),
    "last_error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "processed_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),

    CONSTRAINT "payment_compensations_pkey" PRIMARY KEY ("compensation_id")
);

-- CreateIndex
CREATE INDEX "payment_compensations_status_next_retry_at_idx" ON "payment_compensations"("status", "next_retry_at");

-- CreateIndex
CREATE INDEX "payment_compensations_user_id_idx" ON "payment_compensations"("user_id");

-- CreateIndex
CREATE INDEX "payment_compensations_order_no_idx" ON "payment_compensations"("order_no");

-- CreateIndex
CREATE INDEX "payment_compensations_created_at_idx" ON "payment_compensations"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "payment_compensations_order_no_compensation_type_key" ON "payment_compensations"("order_no", "compensation_type");
