/*
  Warnings:

  - You are about to drop the `EmployeeStaging` table. If the table is not empty, all the data it contains will be lost.

*/
-- CreateEnum
CREATE TYPE "CostTypeVariant" AS ENUM ('NACHTZUSCHLAG', 'WOCHENENDZUSCHLAG', 'FEIERTAGSZUSCHLAG', 'DESINFEKTIONSZUSCHLAG', 'KILOMETERZUSCHLAG', 'FAHRZEUGZUSCHLAG', 'TSWDIFFERENZIERUNG', 'GRUNDPREIS');

-- DropTable
DROP TABLE "EmployeeStaging";

-- CreateTable
CREATE TABLE "EmployeeDyflexisStaging" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "id" INTEGER NOT NULL,
    "firstname" TEXT NOT NULL,
    "surname" TEXT NOT NULL,
    "contractStart" TIMESTAMP(3) NOT NULL,
    "contractEnd" TIMESTAMP(3) NOT NULL,
    "contractHoursWeek" DOUBLE PRECISION NOT NULL,
    "contractSalaryHour" DOUBLE PRECISION NOT NULL,

    CONSTRAINT "EmployeeDyflexisStaging_pkey" PRIMARY KEY ("internalId")
);

-- CreateTable
CREATE TABLE "EmployeeDispoLiveStaging" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "id" TEXT NOT NULL,
    "name" TEXT,
    "surname" TEXT,
    "personnelNumber" TEXT,
    "qualification" TEXT,
    "personTransportCertificate" TIMESTAMP(3),

    CONSTRAINT "EmployeeDispoLiveStaging_pkey" PRIMARY KEY ("internalId")
);

-- CreateTable
CREATE TABLE "Employee" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "surname" TEXT NOT NULL,
    "personnelNumber" TEXT,
    "mobile" TEXT,
    "qualification" TEXT,
    "occuptationalHealthExamination" TIMESTAMP(3),
    "personTransportCertificate" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "DyflexisEmployee" (
    "id" INTEGER NOT NULL,
    "firstname" TEXT NOT NULL,
    "surname" TEXT NOT NULL,
    "contractStart" TIMESTAMP(3) NOT NULL,
    "contractEnd" TIMESTAMP(3),
    "contractHoursWeek" DOUBLE PRECISION NOT NULL,
    "contractSalaryHour" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "AvicennaTenant" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "healthInsuranceNumber" TEXT NOT NULL,
    "name" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "TariffGroup" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "description" TEXT NOT NULL,
    "ordinanceType" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "Tariff" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "validFrom" TIMESTAMP(3),
    "validTo" TIMESTAMP(3),
    "tariffGroupId" UUID,

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

-- CreateTable
CREATE TABLE "CostType" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "description" TEXT NOT NULL,
    "variant" "CostTypeVariant" NOT NULL,
    "costAmount" INTEGER NOT NULL,
    "timeRange" TEXT,
    "kmInclusive" INTEGER,
    "excludeCarNames" TEXT[],
    "infectionType" TEXT,
    "tariffId" UUID
);

-- CreateTable
CREATE TABLE "Trace" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "type" TEXT NOT NULL,
    "entityKey" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "Anomaly" (
    "id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "groupKey" TEXT NOT NULL,
    "groupDescription" TEXT NOT NULL,
    "workedOnBy" TEXT NOT NULL,
    "sleepTimer" INTEGER NOT NULL,
    "sleepSince" TIMESTAMP(3),
    "resolvedAt" TIMESTAMP(3),
    "manuallyResolved" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "SolutionTarget" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "name" TEXT NOT NULL,
    "anomalyId" TEXT,

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

-- CreateTable
CREATE TABLE "SolutionTargetStep" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "description" TEXT NOT NULL,
    "solutionTargetId" UUID,

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

-- CreateTable
CREATE TABLE "_AvicennaTenantToTariffGroup" (
    "A" UUID NOT NULL,
    "B" UUID NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "CostType_id_key" ON "CostType"("id");

-- CreateIndex
CREATE UNIQUE INDEX "_AvicennaTenantToTariffGroup_AB_unique" ON "_AvicennaTenantToTariffGroup"("A", "B");

-- CreateIndex
CREATE INDEX "_AvicennaTenantToTariffGroup_B_index" ON "_AvicennaTenantToTariffGroup"("B");

-- AddForeignKey
ALTER TABLE "Tariff" ADD CONSTRAINT "Tariff_tariffGroupId_fkey" FOREIGN KEY ("tariffGroupId") REFERENCES "TariffGroup"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CostType" ADD CONSTRAINT "CostType_tariffId_fkey" FOREIGN KEY ("tariffId") REFERENCES "Tariff"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SolutionTarget" ADD CONSTRAINT "SolutionTarget_anomalyId_fkey" FOREIGN KEY ("anomalyId") REFERENCES "Anomaly"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SolutionTargetStep" ADD CONSTRAINT "SolutionTargetStep_solutionTargetId_fkey" FOREIGN KEY ("solutionTargetId") REFERENCES "SolutionTarget"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_AvicennaTenantToTariffGroup" ADD CONSTRAINT "_AvicennaTenantToTariffGroup_A_fkey" FOREIGN KEY ("A") REFERENCES "AvicennaTenant"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_AvicennaTenantToTariffGroup" ADD CONSTRAINT "_AvicennaTenantToTariffGroup_B_fkey" FOREIGN KEY ("B") REFERENCES "TariffGroup"("id") ON DELETE CASCADE ON UPDATE CASCADE;
