-- CreateEnum
CREATE TYPE "public"."Status" AS ENUM ('ONLINE', 'RECENT', 'OFFLINE');

-- CreateEnum
CREATE TYPE "public"."LogLevel" AS ENUM ('LOW', 'INFO', 'SUCCESS', 'WARNING', 'ERROR');

-- CreateTable
CREATE TABLE "ResetToken" (
"id" SERIAL,
    "token" TEXT NOT NULL,
    "expiration" TIMESTAMP(3) NOT NULL,
    "usedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "userId" INTEGER NOT NULL,
    "creatorId" INTEGER NOT NULL,

    PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" SERIAL,
    "lastLogin" TIMESTAMP(3),
    "lastLoginPrior" TIMESTAMP(3),
    "roles" JSONB,
    "groups" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "accountExpires" TEXT,
    "badPasswordTime" TEXT,
    "badPwdCount" TEXT,
    "cn" TEXT,
    "department" TEXT,
    "description" TEXT,
    "displayName" TEXT,
    "distinguishedName" TEXT,
    "dn" TEXT,
    "extensionAttribute1" TEXT,
    "extensionAttribute10" TEXT,
    "extensionAttribute2" TEXT,
    "extensionAttribute6" TEXT,
    "extensionAttribute7" TEXT,
    "givenName" TEXT,
    "homeDirectory" TEXT,
    "homeDrive" TEXT,
    "lastLogoff" TEXT,
    "lastLogon" TEXT,
    "lastLogonTimestamp" TEXT,
    "lockoutTime" TEXT,
    "logonCount" TEXT,
    "mail" TEXT,
    "name" TEXT,
    "objectCategory" TEXT,
    "objectGUID" TEXT,
    "objectSid" TEXT,
    "primaryGroupID" TEXT,
    "pwdLastSet" TIMESTAMP(3),
    "sAMAccountName" TEXT NOT NULL,
    "sAMAccountType" TEXT,
    "sn" TEXT,
    "thumbnailPhoto" TEXT,
    "title" TEXT,
    "userAccountControl" TEXT,
    "userPrincipalName" TEXT,
    "whenChanged" TEXT,
    "whenCreated" TEXT,

    PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "WifiDevice" (
"id" SERIAL,
    "oui" TEXT,
    "mac" TEXT NOT NULL,
    "hostname" TEXT,
    "firstSeen" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "lastSeen" TIMESTAMP(3),
    "essid" TEXT,
    "ip" TEXT,
    "uptime" TEXT,
    "apName" TEXT,
    "status" "Status",
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "userId" INTEGER,
    "controller" TEXT NOT NULL DEFAULT E'unknown',

    PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Log" (
"id" SERIAL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "level" "LogLevel" NOT NULL DEFAULT E'LOW',
    "tags" JSONB,
    "message" TEXT NOT NULL,
    "data" JSONB,

    PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Statistic" (
"id" SERIAL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "onlineUsers" INTEGER NOT NULL,
    "offlineUsers" INTEGER NOT NULL,
    "totalUsers" INTEGER NOT NULL,
    "totalWifiDevices" INTEGER NOT NULL,
    "onlineWifiDevices" INTEGER NOT NULL,

    PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "ResetToken.token_unique" ON "ResetToken"("token");

-- CreateIndex
CREATE UNIQUE INDEX "User.sAMAccountName_unique" ON "User"("sAMAccountName");

-- CreateIndex
CREATE UNIQUE INDEX "WifiDevice.mac_unique" ON "WifiDevice"("mac");

-- AddForeignKey
ALTER TABLE "ResetToken" ADD FOREIGN KEY("creatorId")REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ResetToken" ADD FOREIGN KEY("userId")REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "WifiDevice" ADD FOREIGN KEY("userId")REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
