-- AI Web Command customer identity is delegated to the WooCommerce store.
ALTER TABLE "User" ADD COLUMN "wooCustomerId" TEXT;
ALTER TABLE "User" ALTER COLUMN "passwordHash" DROP NOT NULL;

CREATE TABLE "SsoLoginNonce" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "nonceHash" TEXT NOT NULL,
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "usedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE UNIQUE INDEX "SsoLoginNonce_nonceHash_key" ON "SsoLoginNonce"("nonceHash");
CREATE INDEX "SsoLoginNonce_userId_expiresAt_idx" ON "SsoLoginNonce"("userId", "expiresAt");
CREATE UNIQUE INDEX "User_wooCustomerId_key" ON "User"("wooCustomerId");

ALTER TABLE "SsoLoginNonce" ADD CONSTRAINT "SsoLoginNonce_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
