-- AlterTable: add proxyKeyId to ApiKey for marketplace usage attribution (Phase 5, #305)
ALTER TABLE "ApiKey" ADD COLUMN "proxyKeyId" TEXT;
CREATE UNIQUE INDEX "ApiKey_proxyKeyId_key" ON "ApiKey"("proxyKeyId");

-- CreateTable: marketplace usage events (buyer dashboard, Phase 5 #305)
CREATE TABLE "MarketplaceUsageEvent" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "requestId" TEXT NOT NULL,
    "deviceId" TEXT,
    "model" TEXT NOT NULL,
    "promptTokens" INTEGER NOT NULL DEFAULT 0,
    "completionTokens" INTEGER NOT NULL DEFAULT 0,
    "priceCents" INTEGER,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "MarketplaceUsageEvent_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE INDEX "MarketplaceUsageEvent_userId_idx" ON "MarketplaceUsageEvent"("userId");
CREATE INDEX "MarketplaceUsageEvent_createdAt_idx" ON "MarketplaceUsageEvent"("createdAt");
