-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Device" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "tunnelUrl" TEXT NOT NULL,
    "models" TEXT NOT NULL DEFAULT '[]',
    "hardware" TEXT NOT NULL DEFAULT '',
    "status" TEXT NOT NULL DEFAULT 'online',
    "lastSeen" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "latencyMs" INTEGER,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    "vramUsageMb" INTEGER,
    "activeModel" TEXT,
    "requestsServed" INTEGER NOT NULL DEFAULT 0,
    "uptimeSeconds" INTEGER NOT NULL DEFAULT 0,
    "savingsUsd" REAL NOT NULL DEFAULT 0,
    "marketplaceEnabled" BOOLEAN NOT NULL DEFAULT false,
    "marketplacePriceCents" INTEGER,
    CONSTRAINT "Device_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_Device" ("createdAt", "hardware", "id", "lastSeen", "latencyMs", "marketplaceEnabled", "marketplacePriceCents", "models", "name", "status", "tunnelUrl", "updatedAt", "userId") SELECT "createdAt", "hardware", "id", "lastSeen", "latencyMs", "marketplaceEnabled", "marketplacePriceCents", "models", "name", "status", "tunnelUrl", "updatedAt", "userId" FROM "Device";
DROP TABLE "Device";
ALTER TABLE "new_Device" RENAME TO "Device";
CREATE INDEX "Device_userId_idx" ON "Device"("userId");
CREATE INDEX "Device_status_idx" ON "Device"("status");
CREATE INDEX "Device_marketplaceEnabled_idx" ON "Device"("marketplaceEnabled");
CREATE UNIQUE INDEX "Device_userId_name_key" ON "Device"("userId", "name");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
