CREATE TYPE "AssistantApprovalStatus" AS ENUM ('PENDING', 'APPROVED', 'CANCELLED', 'EXPIRED');

CREATE TABLE "assistant_approvals" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "conversationId" TEXT NOT NULL,
  "toolName" TEXT NOT NULL,
  "toolArguments" TEXT NOT NULL,
  "summary" TEXT NOT NULL,
  "status" "AssistantApprovalStatus" NOT NULL DEFAULT 'PENDING',
  "approvedAt" TIMESTAMP(3),
  "cancelledAt" TIMESTAMP(3),
  "expiresAt" TIMESTAMP(3) NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "assistant_approvals_userId_conversationId_status_expiresAt_idx"
  ON "assistant_approvals"("userId", "conversationId", "status", "expiresAt");

CREATE INDEX "assistant_approvals_status_expiresAt_idx"
  ON "assistant_approvals"("status", "expiresAt");

ALTER TABLE "assistant_approvals"
  ADD CONSTRAINT "assistant_approvals_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "users"("id")
  ON DELETE CASCADE ON UPDATE CASCADE;
