-- CreateTable
CREATE TABLE "connectedAircraft" (
    "id" SERIAL NOT NULL,
    "userId" TEXT NOT NULL,
    "publicUser" JSONB NOT NULL,
    "lastHeartbeat" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "stationId" INTEGER NOT NULL,
    "loginTime" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "esimatedLogoutTime" TIMESTAMP(3),
    "logoutTime" TIMESTAMP(3),
    "positionLogIds" INTEGER[] DEFAULT ARRAY[]::INTEGER[],

    CONSTRAINT "connectedAircraft_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ConnectedDispatch" (
    "id" SERIAL NOT NULL,
    "userId" TEXT NOT NULL,
    "publicUser" JSONB NOT NULL,
    "lastHeartbeat" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "loginTime" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "esimatedLogoutTime" TIMESTAMP(3),
    "logoutTime" TIMESTAMP(3),

    CONSTRAINT "ConnectedDispatch_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "PositionLog" (
    "id" SERIAL NOT NULL,
    "userId" TEXT NOT NULL,
    "simulator" TEXT NOT NULL,
    "lat" DOUBLE PRECISION NOT NULL,
    "lng" DOUBLE PRECISION NOT NULL,
    "alt" INTEGER NOT NULL,
    "speed" INTEGER NOT NULL,
    "heading" INTEGER NOT NULL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "PositionLog_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ReportMessage" (
    "id" SERIAL NOT NULL,
    "text" TEXT NOT NULL,
    "senderId" TEXT NOT NULL,
    "reportedId" TEXT NOT NULL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "erledigt" BOOLEAN NOT NULL DEFAULT false,

    CONSTRAINT "ReportMessage_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "connectedAircraft" ADD CONSTRAINT "connectedAircraft_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "connectedAircraft" ADD CONSTRAINT "connectedAircraft_stationId_fkey" FOREIGN KEY ("stationId") REFERENCES "Station"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ConnectedDispatch" ADD CONSTRAINT "ConnectedDispatch_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PositionLog" ADD CONSTRAINT "PositionLog_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ReportMessage" ADD CONSTRAINT "ReportMessage_senderId_fkey" FOREIGN KEY ("senderId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ReportMessage" ADD CONSTRAINT "ReportMessage_reportedId_fkey" FOREIGN KEY ("reportedId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
