-- CreateEnum
CREATE TYPE "EVENT_TYPE" AS ENUM ('COURSE', 'OBLIGATED_COURSE', 'EVENT');

-- CreateEnum
CREATE TYPE "KEYWORD_CATEGORY" AS ENUM ('AB_ATMUNG', 'C_KREISLAUF', 'D_NEUROLOGISCH', 'E_TRAUMA_SONSTIGES', 'K_KIND', 'M_BERGRETTUNG', 'P_PSYCHIATRISCH', 'V_VERLETZUNG', 'W_WASSERRETTUNG', 'Z_SONSTIGES');

-- CreateEnum
CREATE TYPE "missionType" AS ENUM ('primär', 'sekundär');

-- CreateEnum
CREATE TYPE "MissionState" AS ENUM ('running', 'finished', 'draft');

-- CreateEnum
CREATE TYPE "HpgState" AS ENUM ('ready', 'arrived', 'onway');

-- CreateEnum
CREATE TYPE "GlobalColor" AS ENUM ('PRIMARY', 'SECONDARY', 'INFO', 'SUCCESS', 'WARNING', 'ERROR');

-- CreateEnum
CREATE TYPE "BosUse" AS ENUM ('PRIMARY', 'SECONDARY', 'DUAL_USE');

-- CreateEnum
CREATE TYPE "Country" AS ENUM ('DE', 'AT', 'CH');

-- CreateEnum
CREATE TYPE "BADGES" AS ENUM ('P1', 'P2', 'P3', 'D1', 'D2', 'D3', 'DAY1');

-- CreateEnum
CREATE TYPE "PERMISSION" AS ENUM ('ADMIN_EVENT', 'ADMIN_USER', 'AUDIO_ADMIN', 'ADMIN_STATION', 'ADMIN_KEYWORD', 'AUDIO', 'PILOT', 'DISPO');

-- CreateTable
CREATE TABLE "accounts" (
    "id" SERIAL NOT NULL,
    "compound_id" TEXT NOT NULL,
    "user_id" INTEGER NOT NULL,
    "provider_type" TEXT NOT NULL,
    "provider_id" TEXT NOT NULL,
    "provider_account_id" TEXT NOT NULL,
    "refresh_token" TEXT,
    "access_token" TEXT,
    "access_token_expires" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "sessions" (
    "id" SERIAL NOT NULL,
    "user_id" INTEGER NOT NULL,
    "expires" TIMESTAMP(3) NOT NULL,
    "session_token" TEXT NOT NULL,
    "access_token" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "verification_requests" (
    "id" SERIAL NOT NULL,
    "identifier" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expires" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "oauth_tokens" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "client_id" TEXT NOT NULL,
    "access_token" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "ChatMessage" (
    "id" SERIAL NOT NULL,
    "text" TEXT NOT NULL,
    "senderId" TEXT NOT NULL,
    "receiverId" TEXT NOT NULL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "receiverName" TEXT NOT NULL,
    "senderName" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "EventAppointment" (
    "id" SERIAL NOT NULL,
    "eventId" INTEGER NOT NULL,
    "appointmentDate" TIMESTAMP(3) NOT NULL,
    "presenterId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "Participant" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "finisherMoodleCurseCompleted" BOOLEAN NOT NULL DEFAULT false,
    "attended" BOOLEAN NOT NULL DEFAULT false,
    "appointmentCancelled" BOOLEAN NOT NULL DEFAULT false,
    "completetionWorkflowFinished" BOOLEAN NOT NULL DEFAULT false,
    "eventAppointmentId" INTEGER,
    "enscriptionDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "statusLog" JSONB[] DEFAULT ARRAY[]::JSONB[],
    "eventId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "Event" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "type" "EVENT_TYPE" NOT NULL DEFAULT 'EVENT',
    "discordRoleId" TEXT DEFAULT '',
    "hasPresenceEvents" BOOLEAN NOT NULL DEFAULT false,
    "maxParticipants" INTEGER DEFAULT 0,
    "finisherMoodleCourseId" TEXT DEFAULT '',
    "finishedBadges" "BADGES"[] DEFAULT ARRAY[]::"BADGES"[],
    "requiredBadges" "BADGES"[] DEFAULT ARRAY[]::"BADGES"[],
    "finishedPermissions" "PERMISSION"[] DEFAULT ARRAY[]::"PERMISSION"[],
    "hidden" BOOLEAN NOT NULL DEFAULT true,

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

-- CreateTable
CREATE TABLE "File" (
    "id" SERIAL NOT NULL,

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

-- CreateTable
CREATE TABLE "Keyword" (
    "id" SERIAL NOT NULL,
    "category" "KEYWORD_CATEGORY" NOT NULL,
    "abreviation" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "hpgMissionTypes" TEXT[],

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

-- CreateTable
CREATE TABLE "Mission" (
    "id" SERIAL NOT NULL,
    "type" "missionType" NOT NULL DEFAULT 'primär',
    "state" "MissionState" NOT NULL DEFAULT 'draft',
    "addressLat" DOUBLE PRECISION NOT NULL,
    "addressLng" DOUBLE PRECISION NOT NULL,
    "addressStreet" TEXT,
    "addressCity" TEXT,
    "addressZip" TEXT,
    "addressOSMways" JSONB[] DEFAULT ARRAY[]::JSONB[],
    "missionKeywordCategory" TEXT,
    "missionKeywordName" TEXT,
    "missionKeywordAbbreviation" TEXT,
    "missionPatientInfo" TEXT NOT NULL,
    "missionAdditionalInfo" TEXT NOT NULL,
    "missionStationIds" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "missionStationUserIds" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "missionLog" JSONB[] DEFAULT ARRAY[]::JSONB[],
    "hpgMissionString" TEXT,
    "hpgAmbulanceState" "HpgState",
    "hpgFireEngineState" "HpgState",
    "hpgPoliceState" "HpgState",
    "hpgLocationLat" DOUBLE PRECISION DEFAULT 0,
    "hpgLocationLng" DOUBLE PRECISION DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "createdUserId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "MissionOnStationUsers" (
    "userId" TEXT NOT NULL,
    "missionId" INTEGER NOT NULL,
    "stationId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "MissionsOnStations" (
    "missionId" INTEGER NOT NULL,
    "stationId" INTEGER NOT NULL,

    CONSTRAINT "MissionsOnStations_pkey" PRIMARY KEY ("missionId","stationId")
);

-- CreateTable
CREATE TABLE "Notam" (
    "id" SERIAL NOT NULL,
    "color" "GlobalColor" NOT NULL,
    "message" TEXT NOT NULL,
    "showUntil" TIMESTAMP(3) NOT NULL,
    "isMainMsg" BOOLEAN NOT NULL,
    "active" BOOLEAN NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Station" (
    "id" SERIAL NOT NULL,
    "bosUse" "BosUse" NOT NULL,
    "bosCallsign" TEXT NOT NULL,
    "bosCallsignShort" TEXT NOT NULL,
    "bosRadioArea" TEXT NOT NULL,
    "country" "Country" NOT NULL,
    "operator" TEXT NOT NULL,
    "aircraft" TEXT NOT NULL,
    "aircraftRegistration" TEXT NOT NULL,
    "aircraftSpeed" INTEGER NOT NULL,
    "hasWinch" BOOLEAN NOT NULL,
    "is24h" BOOLEAN NOT NULL,
    "hasNvg" BOOLEAN NOT NULL,
    "locationState" TEXT NOT NULL,
    "locationStateShort" TEXT NOT NULL,
    "hasRope" BOOLEAN NOT NULL,
    "fir" TEXT NOT NULL,
    "latitude" DOUBLE PRECISION NOT NULL,
    "longitude" DOUBLE PRECISION NOT NULL,
    "atcCallsign" TEXT NOT NULL,
    "hideRangeRings" BOOLEAN NOT NULL,

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

-- CreateTable
CREATE TABLE "users" (
    "id" TEXT NOT NULL,
    "publicId" TEXT NOT NULL,
    "firstname" TEXT NOT NULL,
    "lastname" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "vatsim_cid" INTEGER,
    "moodle_id" INTEGER,
    "email_verified" TIMESTAMP(3),
    "image" TEXT,
    "badges" "BADGES"[] DEFAULT ARRAY[]::"BADGES"[],
    "permissions" "PERMISSION"[] DEFAULT ARRAY[]::"PERMISSION"[],
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "is_banned" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "discord_accounts" (
    "id" SERIAL NOT NULL,
    "discord_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "username" TEXT NOT NULL,
    "avatar" TEXT,
    "global_name" TEXT NOT NULL,
    "verified" BOOLEAN NOT NULL DEFAULT false,
    "access_token" TEXT NOT NULL,
    "refresh_token" TEXT NOT NULL,
    "token_type" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "_EventAppointmentUser" (
    "A" INTEGER NOT NULL,
    "B" TEXT NOT NULL,

    CONSTRAINT "_EventAppointmentUser_AB_pkey" PRIMARY KEY ("A","B")
);

-- CreateIndex
CREATE UNIQUE INDEX "accounts_compound_id_key" ON "accounts"("compound_id");

-- CreateIndex
CREATE INDEX "providerAccountId" ON "accounts"("provider_account_id");

-- CreateIndex
CREATE INDEX "providerId" ON "accounts"("provider_id");

-- CreateIndex
CREATE INDEX "userId" ON "accounts"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "sessions_session_token_key" ON "sessions"("session_token");

-- CreateIndex
CREATE UNIQUE INDEX "sessions_access_token_key" ON "sessions"("access_token");

-- CreateIndex
CREATE UNIQUE INDEX "verification_requests_token_key" ON "verification_requests"("token");

-- CreateIndex
CREATE UNIQUE INDEX "oauth_tokens_access_token_key" ON "oauth_tokens"("access_token");

-- CreateIndex
CREATE UNIQUE INDEX "MissionOnStationUsers_userId_missionId_stationId_key" ON "MissionOnStationUsers"("userId", "missionId", "stationId");

-- CreateIndex
CREATE UNIQUE INDEX "users_publicId_key" ON "users"("publicId");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "discord_accounts_discord_id_key" ON "discord_accounts"("discord_id");

-- CreateIndex
CREATE INDEX "_EventAppointmentUser_B_index" ON "_EventAppointmentUser"("B");

-- AddForeignKey
ALTER TABLE "oauth_tokens" ADD CONSTRAINT "oauth_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "ChatMessage" ADD CONSTRAINT "ChatMessage_receiverId_fkey" FOREIGN KEY ("receiverId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EventAppointment" ADD CONSTRAINT "EventAppointment_eventId_fkey" FOREIGN KEY ("eventId") REFERENCES "Event"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EventAppointment" ADD CONSTRAINT "EventAppointment_presenterId_fkey" FOREIGN KEY ("presenterId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Participant" ADD CONSTRAINT "Participant_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Participant" ADD CONSTRAINT "Participant_eventId_fkey" FOREIGN KEY ("eventId") REFERENCES "Event"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Participant" ADD CONSTRAINT "Participant_eventAppointmentId_fkey" FOREIGN KEY ("eventAppointmentId") REFERENCES "EventAppointment"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Mission" ADD CONSTRAINT "Mission_createdUserId_fkey" FOREIGN KEY ("createdUserId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "MissionOnStationUsers" ADD CONSTRAINT "MissionOnStationUsers_missionId_fkey" FOREIGN KEY ("missionId") REFERENCES "Mission"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "MissionsOnStations" ADD CONSTRAINT "MissionsOnStations_missionId_fkey" FOREIGN KEY ("missionId") REFERENCES "Mission"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "discord_accounts" ADD CONSTRAINT "discord_accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_EventAppointmentUser" ADD CONSTRAINT "_EventAppointmentUser_A_fkey" FOREIGN KEY ("A") REFERENCES "EventAppointment"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_EventAppointmentUser" ADD CONSTRAINT "_EventAppointmentUser_B_fkey" FOREIGN KEY ("B") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
