ALTER TABLE "Customer"
  ALTER COLUMN "phone" DROP NOT NULL;

CREATE TABLE "MiniProgramUser" (
  "id" TEXT NOT NULL,
  "appId" TEXT NOT NULL,
  "openId" TEXT NOT NULL,
  "unionId" TEXT,
  "nickname" TEXT,
  "avatarUrl" TEXT,
  "phone" TEXT,
  "phoneAuthorizedAt" TIMESTAMP(3),
  "note" TEXT,
  "source" TEXT NOT NULL DEFAULT 'mini_program',
  "status" TEXT NOT NULL DEFAULT 'active',
  "customerId" TEXT,
  "firstSeenAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "lastLoginAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE TABLE "MiniProgramFavorite" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "productId" TEXT NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE TABLE "MiniProgramHistory" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "productId" TEXT NOT NULL,
  "viewCount" INTEGER NOT NULL DEFAULT 1,
  "lastViewedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

ALTER TABLE "Lead"
  ADD COLUMN "userId" TEXT;

CREATE UNIQUE INDEX "MiniProgramUser_appId_openId_key" ON "MiniProgramUser"("appId", "openId");
CREATE UNIQUE INDEX "MiniProgramUser_customerId_key" ON "MiniProgramUser"("customerId");
CREATE INDEX "MiniProgramUser_phone_idx" ON "MiniProgramUser"("phone");
CREATE INDEX "MiniProgramUser_status_lastLoginAt_idx" ON "MiniProgramUser"("status", "lastLoginAt");
CREATE UNIQUE INDEX "MiniProgramFavorite_userId_productId_key" ON "MiniProgramFavorite"("userId", "productId");
CREATE INDEX "MiniProgramFavorite_productId_idx" ON "MiniProgramFavorite"("productId");
CREATE UNIQUE INDEX "MiniProgramHistory_userId_productId_key" ON "MiniProgramHistory"("userId", "productId");
CREATE INDEX "MiniProgramHistory_userId_lastViewedAt_idx" ON "MiniProgramHistory"("userId", "lastViewedAt");
CREATE INDEX "Lead_userId_createdAt_idx" ON "Lead"("userId", "createdAt");
CREATE INDEX "Lead_phone_idx" ON "Lead"("phone");

ALTER TABLE "MiniProgramUser"
  ADD CONSTRAINT "MiniProgramUser_customerId_fkey"
  FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE "MiniProgramFavorite"
  ADD CONSTRAINT "MiniProgramFavorite_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "MiniProgramUser"("id") ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT "MiniProgramFavorite_productId_fkey"
  FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "MiniProgramHistory"
  ADD CONSTRAINT "MiniProgramHistory_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "MiniProgramUser"("id") ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT "MiniProgramHistory_productId_fkey"
  FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "Lead"
  ADD CONSTRAINT "Lead_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "MiniProgramUser"("id") ON DELETE SET NULL ON UPDATE CASCADE;
