PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;

-- =====================
-- Channel
-- =====================
CREATE TABLE "new_Channel" (
                               "id" TEXT NOT NULL PRIMARY KEY,
                               "ownerId" TEXT,
                               "ownerUsername" TEXT,
                               "name" TEXT NOT NULL,
                               "persistent" BOOLEAN NOT NULL,
                               CONSTRAINT "Channel_ownerUsername_fkey" FOREIGN KEY ("ownerUsername") REFERENCES "User" ("username") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_Channel" ("id", "name", "ownerId", "ownerUsername", "persistent")
SELECT c."id", c."name", c."ownerId",
       (SELECT u."username" FROM "User" u WHERE u."id" = c."ownerId"),
       c."persistent"
FROM "Channel" c;
DROP TABLE "Channel";
ALTER TABLE "new_Channel" RENAME TO "Channel";

-- =====================
-- Message
-- =====================
CREATE TABLE "new_Message" (
                               "id" TEXT NOT NULL PRIMARY KEY,
                               "text" TEXT NOT NULL,
                               "senderId" TEXT,
                               "senderUsername" TEXT,
                               "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
                               "updatedAt" DATETIME NOT NULL,
                               CONSTRAINT "Message_senderUsername_fkey" FOREIGN KEY ("senderUsername") REFERENCES "User" ("username") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Message" ("id", "text", "senderId", "senderUsername", "createdAt", "updatedAt")
SELECT m."id", m."text", m."senderId",
       (SELECT u."username" FROM "User" u WHERE u."id" = m."senderId"),
       m."createdAt", m."updatedAt"
FROM "Message" m;
DROP TABLE "Message";
ALTER TABLE "new_Message" RENAME TO "Message";

-- =====================
-- Session
-- =====================
CREATE TABLE "new_Session" (
                               "id" TEXT NOT NULL PRIMARY KEY,
                               "userId" TEXT NOT NULL,
                               "username" TEXT NOT NULL,
                               "expiresAt" DATETIME NOT NULL,
                               CONSTRAINT "Session_username_fkey" FOREIGN KEY ("username") REFERENCES "User" ("username") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_Session" ("id", "userId", "username", "expiresAt")
SELECT s."id", s."userId",
       (SELECT u."username" FROM "User" u WHERE u."id" = s."userId"),
       s."expiresAt"
FROM "Session" s;
DROP TABLE "Session";
ALTER TABLE "new_Session" RENAME TO "Session";
CREATE INDEX "Session_userId_idx" ON "Session"("userId");

-- =====================
-- User
-- =====================
CREATE TABLE "new_User" (
                            "id" TEXT NOT NULL,
                            "username" TEXT NOT NULL PRIMARY KEY,
                            "password" TEXT NOT NULL,
                            "displayName" TEXT NOT NULL,
                            "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
                            "updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_User" ("createdAt", "displayName", "id", "password", "updatedAt", "username")
SELECT "createdAt", "displayName", "id", "password", "updatedAt", "username"
FROM "User";
DROP TABLE "User";
ALTER TABLE "new_User" RENAME TO "User";
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");

-- =====================
-- UserPreferences
-- =====================
CREATE TABLE "new_UserPreferences" (
                                       "userId" TEXT NOT NULL,
                                       "username" TEXT NOT NULL PRIMARY KEY,
                                       "toggleInputHotkey" TEXT DEFAULT '',
                                       "toggleOutputHotkey" TEXT DEFAULT '',
                                       CONSTRAINT "UserPreferences_username_fkey" FOREIGN KEY ("username") REFERENCES "User" ("username") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_UserPreferences" ("userId", "username", "toggleInputHotkey", "toggleOutputHotkey")
SELECT up."userId",
       (SELECT u."username" FROM "User" u WHERE u."id" = up."userId"),
       up."toggleInputHotkey", up."toggleOutputHotkey"
FROM "UserPreferences" up;
DROP TABLE "UserPreferences";
ALTER TABLE "new_UserPreferences" RENAME TO "UserPreferences";
CREATE UNIQUE INDEX "UserPreferences_userId_key" ON "UserPreferences"("userId");
CREATE UNIQUE INDEX "UserPreferences_username_key" ON "UserPreferences"("username");

PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;