-- CreateEnum
CREATE TYPE "StopStatus" AS ENUM ('PENDING', 'EN_ROUTE', 'ARRIVED', 'SETUP_STARTED', 'SETUP_COMPLETE', 'PICKUP_STARTED', 'PICKUP_COMPLETE', 'SKIPPED', 'ISSUE');

-- CreateEnum
CREATE TYPE "PhotoType" AS ENUM ('SETUP_BEFORE', 'SETUP_AFTER', 'PICKUP_BEFORE', 'PICKUP_AFTER', 'ISSUE', 'OTHER');

-- CreateTable
CREATE TABLE "TourStop" (
    "id" TEXT NOT NULL,
    "tourId" TEXT NOT NULL,
    "bookingId" TEXT NOT NULL,
    "stopOrder" INTEGER NOT NULL,
    "stopType" TEXT NOT NULL DEFAULT 'DELIVERY',
    "status" "StopStatus" NOT NULL DEFAULT 'PENDING',
    "arrivedAt" TIMESTAMP(3),
    "setupStartedAt" TIMESTAMP(3),
    "setupCompleteAt" TIMESTAMP(3),
    "pickupStartedAt" TIMESTAMP(3),
    "pickupCompleteAt" TIMESTAMP(3),
    "arrivalLatitude" DOUBLE PRECISION,
    "arrivalLongitude" DOUBLE PRECISION,
    "notes" TEXT,
    "issueDescription" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "DriverLocation" (
    "id" TEXT NOT NULL,
    "driverId" TEXT NOT NULL,
    "latitude" DOUBLE PRECISION NOT NULL,
    "longitude" DOUBLE PRECISION NOT NULL,
    "accuracy" DOUBLE PRECISION,
    "heading" DOUBLE PRECISION,
    "speed" DOUBLE PRECISION,
    "tourId" TEXT,
    "currentStopId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "DeliveryPhoto" (
    "id" TEXT NOT NULL,
    "tourStopId" TEXT NOT NULL,
    "photoType" "PhotoType" NOT NULL,
    "fileName" TEXT NOT NULL,
    "filePath" TEXT NOT NULL,
    "fileSize" INTEGER,
    "mimeType" TEXT,
    "latitude" DOUBLE PRECISION,
    "longitude" DOUBLE PRECISION,
    "caption" TEXT,
    "uploadedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "TourStop_tourId_stopOrder_idx" ON "TourStop"("tourId", "stopOrder");

-- CreateIndex
CREATE INDEX "TourStop_status_idx" ON "TourStop"("status");

-- CreateIndex
CREATE UNIQUE INDEX "TourStop_tourId_bookingId_key" ON "TourStop"("tourId", "bookingId");

-- CreateIndex
CREATE INDEX "DriverLocation_driverId_createdAt_idx" ON "DriverLocation"("driverId", "createdAt");

-- CreateIndex
CREATE INDEX "DriverLocation_tourId_idx" ON "DriverLocation"("tourId");

-- CreateIndex
CREATE INDEX "DeliveryPhoto_tourStopId_idx" ON "DeliveryPhoto"("tourStopId");

-- CreateIndex
CREATE INDEX "DeliveryPhoto_photoType_idx" ON "DeliveryPhoto"("photoType");

-- AddForeignKey
ALTER TABLE "TourStop" ADD CONSTRAINT "TourStop_tourId_fkey" FOREIGN KEY ("tourId") REFERENCES "Tour"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TourStop" ADD CONSTRAINT "TourStop_bookingId_fkey" FOREIGN KEY ("bookingId") REFERENCES "Booking"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DriverLocation" ADD CONSTRAINT "DriverLocation_driverId_fkey" FOREIGN KEY ("driverId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DeliveryPhoto" ADD CONSTRAINT "DeliveryPhoto_tourStopId_fkey" FOREIGN KEY ("tourStopId") REFERENCES "TourStop"("id") ON DELETE CASCADE ON UPDATE CASCADE;
