-- CreateExtension
CREATE EXTENSION IF NOT EXISTS "pgcrypto";

-- CreateEnum
CREATE TYPE "TicketState" AS ENUM ('CREATED', 'APPROVAL_REQUIRED', 'READY', 'DONE', 'BILLABLE', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "TicketValidationState" AS ENUM ('CREATED', 'USER_INTERACTION_REQUIRED', 'TD_UNCERTAIN', 'DOCUMENTS_MISSING', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "ApprovalState" AS ENUM ('UNKNOWN', 'FREE', 'REQUIRED', 'REQUESTED', 'APPROVED');

-- CreateTable
CREATE TABLE "TourSource" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "insertedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "jobId" TEXT NOT NULL,
    "id" TEXT,
    "check" TEXT,
    "createTime" TEXT,
    "empfangenVonId" TEXT,
    "endgen" TEXT,
    "serienId" TEXT,
    "serienKategorName" TEXT,
    "serienKategorie" TEXT,
    "zadStatus" TEXT,
    "abrDate" TEXT,
    "abrNum" TEXT,
    "abrTime" TEXT,
    "ausrueckZeit" TEXT,
    "startTime" TEXT,
    "startDate" TEXT,
    "date" TEXT,
    "date_iso" TEXT,
    "dauergenehmigung" TEXT,
    "direction" TEXT,
    "distanceValue" TEXT,
    "besetztKm" TEXT,
    "gefahren" TEXT,
    "gesamtPreis" TEXT,
    "infektion" TEXT,
    "itDesc" TEXT,
    "itKrz" TEXT,
    "itName" TEXT,
    "kkId" TEXT,
    "krankenkasse" TEXT,
    "patId" TEXT,
    "patName" TEXT,
    "patSurname" TEXT,
    "patStreet" TEXT,
    "patZip" TEXT,
    "patCity" TEXT,
    "startInstitution" TEXT,
    "startStreet" TEXT,
    "startZip" TEXT,
    "startCity" TEXT,
    "targetInstitution" TEXT,
    "targetStreet" TEXT,
    "targetZip" TEXT,
    "targetCity" TEXT,
    "transportKrz" TEXT,
    "transportName" TEXT,
    "transportart" TEXT,
    "verordnungsId" TEXT,
    "verordnungsName" TEXT,
    "type" TEXT,
    "startBegin" TEXT,
    "startEnd" TEXT,
    "frei" TEXT,
    "ziel" TEXT,
    "zielBegin" TEXT,
    "endTime" TEXT,
    "endDate" TEXT,
    "einsatzNummer" TEXT,
    "carName" TEXT,
    "carNo" TEXT,
    "beifahrerName" TEXT,
    "beifahrer" TEXT,
    "fahrerName" TEXT,
    "fahrer" TEXT,
    "color" TEXT,

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

-- CreateTable
CREATE TABLE "EmployeeDispoLiveSource" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "insertedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "jobId" TEXT NOT NULL,
    "id" TEXT,
    "pScheinAblaufDatum" TEXT,
    "name" TEXT,
    "surname" TEXT,
    "persId" TEXT,
    "importPersId" TEXT,
    "qualifikation" TEXT,

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

-- CreateTable
CREATE TABLE "AttendanceRegistrationSource" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "insertedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "jobId" TEXT NOT NULL,
    "attendanceRegistrationId" TEXT,
    "employeeId" TEXT,
    "personnelNumber" TEXT,
    "dateTime" TEXT,
    "event" TEXT,

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

-- CreateTable
CREATE TABLE "PlannedTimeSource" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "insertedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "jobId" TEXT NOT NULL,
    "id" TEXT,
    "user_id" TEXT,
    "firstname" TEXT,
    "surname" TEXT,
    "department_id" TEXT,
    "department_name" TEXT,
    "start_date" TEXT,
    "end_date" TEXT,
    "pauze" TEXT,
    "duration" TEXT,
    "deleted" TEXT,
    "mark" TEXT,

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

-- CreateTable
CREATE TABLE "WorkTimeSource" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "insertedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "jobId" TEXT NOT NULL,
    "id" TEXT,
    "user_id" TEXT,
    "firstname" TEXT,
    "surname" TEXT,
    "department_id" TEXT,
    "department_name" TEXT,
    "start_date" TEXT,
    "end_date" TEXT,
    "pauze" TEXT,
    "duration" TEXT,

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

-- CreateTable
CREATE TABLE "EmployeeDyflexisSource" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "insertedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "jobId" TEXT NOT NULL,
    "id" TEXT,
    "firstname" TEXT,
    "surname" TEXT,
    "contract_id" TEXT,
    "contract_start" TEXT,
    "contract_end" TEXT,
    "contract_hours_week" TEXT,
    "contract_salary_hour" TEXT,

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

-- CreateTable
CREATE TABLE "PatientSource" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "insertedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "jobId" TEXT NOT NULL,
    "id" TEXT,
    "surname" TEXT,
    "name" TEXT,
    "street" TEXT,
    "zip" TEXT,
    "city" TEXT,
    "birthday" TEXT,
    "kkId" TEXT,
    "krankenkasse" TEXT,
    "pflegestufe" TEXT,
    "behinderung1" TEXT,
    "behinderung2" TEXT,

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

-- CreateTable
CREATE TABLE "TourStaging" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "id" TEXT,
    "done" BOOLEAN,
    "direction" TEXT,
    "carName" TEXT,
    "operationId" TEXT,
    "driverId" TEXT,
    "driverName" TEXT,
    "codriverId" TEXT,
    "codriverName" TEXT,
    "patientId" TEXT,
    "patientName" TEXT,
    "patientSurname" TEXT,
    "patientStreet" TEXT,
    "patientZip" TEXT,
    "patientCity" TEXT,
    "healthInsurance" TEXT,
    "healthInsuranceNumber" TEXT,
    "type" TEXT,
    "category" TEXT,
    "transportType" TEXT,
    "ordinanceType" TEXT,
    "rangeEndDate" TIMESTAMP(3),
    "startInstitution" TEXT,
    "startStreet" TEXT,
    "startZip" TEXT,
    "startCity" TEXT,
    "targetInstitution" TEXT,
    "targetStreet" TEXT,
    "targetZip" TEXT,
    "targetCity" TEXT,
    "startDate" TIMESTAMP(3),
    "check" INTEGER NOT NULL DEFAULT 0,
    "occupiedKm" DOUBLE PRECISION,
    "totalKm" DOUBLE PRECISION,
    "startBegin" TEXT,
    "startEnd" TEXT,
    "target" TEXT,
    "targetBegin" TEXT,
    "empty" TEXT,
    "hasInfection" BOOLEAN NOT NULL DEFAULT false,
    "infectionName" TEXT,
    "revenueDispoLive" DOUBLE PRECISION,
    "billDate" TIMESTAMP(3),
    "billNumber" TEXT,
    "revenue" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "revenueDeviation" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "consumptionCosts" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "PatientStaging" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "id" TEXT,
    "surname" TEXT,
    "name" TEXT,
    "street" TEXT,
    "zip" TEXT,
    "city" TEXT,
    "birthday" TIMESTAMP(3),
    "healthinsuranceId" TEXT,
    "healthinsurance" TEXT,
    "careDegree" TEXT,
    "disabilityMark1" TEXT,
    "disabilityMark2" TEXT,

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

-- CreateTable
CREATE TABLE "AttendanceRegistrationStaging" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "attendanceRegistrationId" INTEGER NOT NULL,
    "employeeId" INTEGER NOT NULL,
    "dateTime" TIMESTAMP(3) NOT NULL,
    "event" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "PlannedTimeStaging" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "id" INTEGER NOT NULL,
    "employeeId" INTEGER NOT NULL,
    "firstname" TEXT,
    "surname" TEXT,
    "departmentName" TEXT,
    "startDate" TIMESTAMP(3) NOT NULL,
    "endDate" TIMESTAMP(3) NOT NULL,
    "deleted" BOOLEAN NOT NULL,
    "mark" TEXT,

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

-- CreateTable
CREATE TABLE "WorkTimeStaging" (
    "internalId" UUID NOT NULL DEFAULT gen_random_uuid(),
    "id" INTEGER NOT NULL,
    "employeeId" INTEGER NOT NULL,
    "firstname" TEXT NOT NULL,
    "surname" TEXT NOT NULL,
    "departmentName" TEXT,
    "startDate" TIMESTAMP(3) NOT NULL,
    "endDate" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "EmployeeStaging" (
    "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,
    "personTransportCertificate" TIMESTAMP(3),
    "qualification" TEXT,

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

-- CreateTable
CREATE TABLE "Tour" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "done" BOOLEAN NOT NULL,
    "direction" TEXT NOT NULL,
    "carName" TEXT,
    "operationId" TEXT NOT NULL,
    "driverId" TEXT,
    "driverName" TEXT,
    "codriverId" TEXT,
    "codriverName" TEXT,
    "patientId" TEXT,
    "patientName" TEXT,
    "patientSurname" TEXT,
    "patientStreet" TEXT,
    "patientZip" TEXT,
    "patientCity" TEXT,
    "healthInsurance" TEXT,
    "healthInsuranceNumber" TEXT,
    "type" TEXT,
    "category" TEXT,
    "transportType" TEXT,
    "ordinanceType" TEXT,
    "rangeEndDate" TIMESTAMP(3),
    "startInstitution" TEXT,
    "startStreet" TEXT,
    "startZip" TEXT,
    "startCity" TEXT,
    "targetInstitution" TEXT,
    "targetStreet" TEXT,
    "targetZip" TEXT,
    "targetCity" TEXT,
    "startDate" TIMESTAMP(3) NOT NULL,
    "check" INTEGER NOT NULL DEFAULT 0,
    "occupiedKm" DOUBLE PRECISION,
    "totalKm" DOUBLE PRECISION,
    "startBegin" TEXT,
    "startEnd" TEXT,
    "target" TEXT,
    "targetBegin" TEXT,
    "empty" TEXT,
    "hasInfection" BOOLEAN NOT NULL DEFAULT false,
    "infectionName" TEXT,
    "revenueDispoLive" DOUBLE PRECISION,
    "billDate" TIMESTAMP(3),
    "billNumber" TEXT,
    "revenue" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "consumptionCosts" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "revenueDeviation" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3),
    "deletedAt" TIMESTAMP(3),
    "ticketId" UUID,

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

-- CreateTable
CREATE TABLE "Patient" (
    "id" TEXT NOT NULL,
    "surname" TEXT,
    "name" TEXT,
    "street" TEXT,
    "zip" TEXT,
    "city" TEXT,
    "birthday" TIMESTAMP(3),
    "healthinsuranceId" TEXT,
    "healthinsurance" TEXT,
    "careDegree" TEXT,
    "disabilityMark1" TEXT,
    "disabilityMark2" TEXT,

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

-- CreateTable
CREATE TABLE "Ticket" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "currentState" "TicketValidationState" NOT NULL,
    "notes" TEXT[],
    "errors" TEXT[],
    "isUrgent" BOOLEAN NOT NULL DEFAULT false,
    "urgency" TIMESTAMP(3) NOT NULL DEFAULT '2099-12-31 23:59:59.999 +00:00',
    "approvalState" "ApprovalState",
    "documentInfo" JSONB DEFAULT '{"approval": {"isNeeded": null, "whoRequested": "","answeredFrom": ""},"tdLocation": ""}',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "TicketStateHistory" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "state" "TicketValidationState" NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "ticketId" UUID NOT NULL,

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

-- CreateTable
CREATE TABLE "AttendanceRegistration" (
    "attendanceRegistrationId" INTEGER NOT NULL,
    "employeeId" INTEGER NOT NULL,
    "personnelNumber" TEXT NOT NULL,
    "dateTime" TIMESTAMP(3) NOT NULL,
    "event" TEXT NOT NULL,

    CONSTRAINT "AttendanceRegistration_pkey" PRIMARY KEY ("attendanceRegistrationId")
);

-- CreateTable
CREATE TABLE "WorkTime" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "firstname" TEXT NOT NULL,
    "surname" TEXT NOT NULL,
    "startDate" TIMESTAMP(3),
    "endDate" TIMESTAMP(3),
    "department" TEXT,
    "pause" INTEGER NOT NULL,
    "duration" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "PlannedTime" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "firstname" TEXT,
    "surname" TEXT,
    "startDate" TIMESTAMP(3),
    "endDate" TIMESTAMP(3),
    "department" TEXT,
    "pause" INTEGER NOT NULL,
    "duration" INTEGER NOT NULL,
    "note" TEXT,

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

-- CreateIndex
CREATE UNIQUE INDEX "Tour_operationId_key" ON "Tour"("operationId");

-- AddForeignKey
ALTER TABLE "Tour" ADD CONSTRAINT "Tour_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TicketStateHistory" ADD CONSTRAINT "TicketStateHistory_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
