-- CreateTable
CREATE TABLE "SystemSetting" (
    "hashKey" TEXT NOT NULL PRIMARY KEY,
    "hashValueType" TEXT NOT NULL,
    "hashValue" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "SystemPostMigration" (
    "name" TEXT NOT NULL PRIMARY KEY,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "AuthRealm" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "name" TEXT NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "AuthOauth2Client" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "realmId" INTEGER NOT NULL,
    "clientId" TEXT NOT NULL,
    "clientSecret" TEXT,
    "consentRequired" BOOLEAN NOT NULL DEFAULT false,
    "authorizationCodeFlowEnabled" BOOLEAN NOT NULL DEFAULT false,
    "resourceOwnerPasswordCredentialsFlowEnabled" BOOLEAN NOT NULL DEFAULT false,
    "clientCredentialsFlowEnabled" BOOLEAN NOT NULL DEFAULT false,
    "idTokenEnabled" BOOLEAN NOT NULL DEFAULT false,
    "refreshTokenEnabled" BOOLEAN NOT NULL DEFAULT false,
    CONSTRAINT "AuthOauth2Client_realmId_fkey" FOREIGN KEY ("realmId") REFERENCES "AuthRealm" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "AuthOauth2Scope" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "realmId" INTEGER NOT NULL,
    "scope" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "AuthOauth2ClientToAuthOauth2Scope" (
    "clientId" INTEGER NOT NULL,
    "scopeId" INTEGER NOT NULL,

    PRIMARY KEY ("clientId", "scopeId"),
    CONSTRAINT "AuthOauth2ClientToAuthOauth2Scope_clientId_fkey" FOREIGN KEY ("clientId") REFERENCES "AuthOauth2Client" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "AuthOauth2ClientToAuthOauth2Scope_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "AuthOauth2Scope" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "AuthOauth2ScopeToIdentityProfileAttributeName" (
    "scopeId" INTEGER NOT NULL,
    "claimName" TEXT NOT NULL,
    "attributeId" INTEGER NOT NULL,

    PRIMARY KEY ("scopeId", "attributeId"),
    CONSTRAINT "AuthOauth2ScopeToIdentityProfileAttributeName_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "AuthOauth2Scope" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "AuthOauth2ScopeToIdentityProfileAttributeName_attributeId_fkey" FOREIGN KEY ("attributeId") REFERENCES "IdentityProfileAttributeName" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "AuthRole" (
    "realmId" INTEGER NOT NULL,
    "roleName" TEXT NOT NULL,

    PRIMARY KEY ("realmId", "roleName"),
    CONSTRAINT "AuthRole_realmId_fkey" FOREIGN KEY ("realmId") REFERENCES "AuthRealm" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "AuthAccessAttempt" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "username" TEXT NOT NULL,
    "ip" TEXT NOT NULL,
    "userAgent" TEXT NOT NULL,
    "requestPath" TEXT NOT NULL,
    "valid" BOOLEAN NOT NULL,
    "attemptedOn" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "EnumIdentityGroupRole" (
    "enumValue" TEXT NOT NULL PRIMARY KEY
);

-- CreateTable
CREATE TABLE "IdentityGroup" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "realmId" INTEGER NOT NULL,
    "role" TEXT NOT NULL,
    "name" TEXT,
    CONSTRAINT "IdentityGroup_realmId_fkey" FOREIGN KEY ("realmId") REFERENCES "AuthRealm" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "IdentityGroup_role_fkey" FOREIGN KEY ("role") REFERENCES "EnumIdentityGroupRole" ("enumValue") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "IdentityGroupToIdentityUser" (
    "groupId" INTEGER NOT NULL,
    "userId" INTEGER NOT NULL,
    "userIsGroupAdmin" BOOLEAN NOT NULL DEFAULT false,

    PRIMARY KEY ("groupId", "userId"),
    CONSTRAINT "IdentityGroupToIdentityUser_groupId_fkey" FOREIGN KEY ("groupId") REFERENCES "IdentityGroup" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "IdentityGroupToIdentityUser_userId_fkey" FOREIGN KEY ("userId") REFERENCES "IdentityUser" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "IdentityUser" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "externalId" TEXT NOT NULL,
    "username" TEXT NOT NULL,
    "realmId" INTEGER NOT NULL,
    CONSTRAINT "IdentityUser_realmId_fkey" FOREIGN KEY ("realmId") REFERENCES "AuthRealm" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "IdentityProfileAttributeName" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "realmId" INTEGER NOT NULL,
    "name" TEXT NOT NULL,
    CONSTRAINT "IdentityProfileAttributeName_realmId_fkey" FOREIGN KEY ("realmId") REFERENCES "AuthRealm" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "IdentityProfileNonNormalized" (
    "userId" INTEGER NOT NULL,
    "attributeNameId" INTEGER NOT NULL,
    "attributeValue" TEXT NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,

    PRIMARY KEY ("userId", "attributeNameId"),
    CONSTRAINT "IdentityProfileNonNormalized_userId_fkey" FOREIGN KEY ("userId") REFERENCES "IdentityUser" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "IdentityProfileNonNormalized_attributeNameId_fkey" FOREIGN KEY ("attributeNameId") REFERENCES "IdentityProfileAttributeName" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "IdentityUserEmails" (
    "email" TEXT NOT NULL PRIMARY KEY,
    "userId" INTEGER NOT NULL,
    "verified" BOOLEAN NOT NULL DEFAULT false,
    "default" BOOLEAN NOT NULL DEFAULT false,
    CONSTRAINT "IdentityUserEmails_userId_fkey" FOREIGN KEY ("userId") REFERENCES "IdentityUser" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "EnumIdentityAuthDeviceType" (
    "enumValue" TEXT NOT NULL PRIMARY KEY
);

-- CreateTable
CREATE TABLE "IdentityAuthDevice" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" INTEGER NOT NULL,
    "deviceType" TEXT NOT NULL,
    "attributes" TEXT NOT NULL,
    "preferred" BOOLEAN NOT NULL,
    "twoFactorPreferred" BOOLEAN NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "IdentityAuthDevice_userId_fkey" FOREIGN KEY ("userId") REFERENCES "IdentityUser" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "IdentityAuthDevice_deviceType_fkey" FOREIGN KEY ("deviceType") REFERENCES "EnumIdentityAuthDeviceType" ("enumValue") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "EnumCloudDavResourceType" (
    "enumValue" TEXT NOT NULL PRIMARY KEY
);

-- CreateTable
CREATE TABLE "CloudDavResource" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "identityGroupId" INTEGER NOT NULL,
    "resourceType" TEXT NOT NULL,
    "attributes" TEXT NOT NULL,
    CONSTRAINT "CloudDavResource_identityGroupId_fkey" FOREIGN KEY ("identityGroupId") REFERENCES "IdentityGroup" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "CloudDavResource_resourceType_fkey" FOREIGN KEY ("resourceType") REFERENCES "EnumCloudDavResourceType" ("enumValue") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "AuthRealm_name_key" ON "AuthRealm"("name");

-- CreateIndex
CREATE UNIQUE INDEX "AuthOauth2Client_realmId_clientId_key" ON "AuthOauth2Client"("realmId", "clientId");

-- CreateIndex
CREATE UNIQUE INDEX "AuthOauth2Scope_realmId_scope_key" ON "AuthOauth2Scope"("realmId", "scope");

-- CreateIndex
CREATE UNIQUE INDEX "AuthOauth2ScopeToIdentityProfileAttributeName_scopeId_claimName_key" ON "AuthOauth2ScopeToIdentityProfileAttributeName"("scopeId", "claimName");

-- CreateIndex
CREATE UNIQUE INDEX "IdentityUser_externalId_key" ON "IdentityUser"("externalId");

-- CreateIndex
CREATE UNIQUE INDEX "IdentityUser_username_key" ON "IdentityUser"("username");

-- CreateIndex
CREATE INDEX "IdentityAuthDevice_userId_idx" ON "IdentityAuthDevice"("userId");

-- CreateIndex
CREATE INDEX "IdentityAuthDevice_userId_deviceType_idx" ON "IdentityAuthDevice"("userId", "deviceType");

-- CreateIndex
CREATE INDEX "CloudDavResource_identityGroupId_idx" ON "CloudDavResource"("identityGroupId");
