-- CreateTable: log polymorphic untuk admin actions lintas entity. Append-only,
-- never update/delete. Dipakai untuk compliance & investigasi (siapa
-- approve/reject/cancel/suspend, kapan, dengan payload apa).
--
-- `adminId` nullable + `adminEmail` snapshot supaya kalau admin dihapus,
-- log entry tetap auditable (siapa via email, kapan, payload apa). FK
-- ON DELETE SET NULL menjamin adminId di-clear tanpa cascade ke baris log.
CREATE TABLE "AdminActionLog" (
    "id" TEXT NOT NULL,
    "adminId" TEXT,
    "adminEmail" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "entityType" TEXT NOT NULL,
    "entityId" TEXT NOT NULL,
    "payload" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- Index: filter "all actions by admin X" + "all actions on entity Y".
CREATE INDEX "AdminActionLog_adminId_createdAt_idx" ON "AdminActionLog"("adminId", "createdAt" DESC);
CREATE INDEX "AdminActionLog_entityType_entityId_idx" ON "AdminActionLog"("entityType", "entityId");
CREATE INDEX "AdminActionLog_createdAt_idx" ON "AdminActionLog"("createdAt" DESC);

-- FK: admin user — SET NULL kalau admin dihapus supaya log tidak hilang.
ALTER TABLE "AdminActionLog" ADD CONSTRAINT "AdminActionLog_adminId_fkey"
  FOREIGN KEY ("adminId") REFERENCES "User"("id")
  ON DELETE SET NULL ON UPDATE CASCADE;
