-- CreateTable
CREATE TABLE "MarketplaceBillingEvent" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "deviceId" TEXT NOT NULL,
    "operatorId" TEXT NOT NULL,
    "buyerId" TEXT NOT NULL,
    "model" TEXT NOT NULL,
    "promptTokens" INTEGER NOT NULL,
    "completionTokens" INTEGER NOT NULL,
    "totalTokens" INTEGER NOT NULL,
    "priceCents" INTEGER NOT NULL,
    "platformFeeCents" INTEGER NOT NULL,
    "operatorShareCents" INTEGER NOT NULL,
    "paidOut" BOOLEAN NOT NULL DEFAULT false,
    "payoutTransferId" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "MarketplaceBillingEvent_deviceId_fkey" FOREIGN KEY ("deviceId") REFERENCES "Device" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "MarketplaceBillingEvent_operatorId_fkey" FOREIGN KEY ("operatorId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "MarketplaceBillingEvent_buyerId_fkey" FOREIGN KEY ("buyerId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE INDEX "MarketplaceBillingEvent_operatorId_idx" ON "MarketplaceBillingEvent"("operatorId");

-- CreateIndex
CREATE INDEX "MarketplaceBillingEvent_buyerId_idx" ON "MarketplaceBillingEvent"("buyerId");

-- CreateIndex
CREATE INDEX "MarketplaceBillingEvent_deviceId_idx" ON "MarketplaceBillingEvent"("deviceId");

-- CreateIndex
CREATE INDEX "MarketplaceBillingEvent_createdAt_idx" ON "MarketplaceBillingEvent"("createdAt");

-- CreateIndex
CREATE INDEX "MarketplaceBillingEvent_operatorId_paidOut_idx" ON "MarketplaceBillingEvent"("operatorId", "paidOut");
