/*
  Warnings:

  - The values [Delivered] on the enum `OrderStatus` will be removed. If these variants are still used in the database, this will fail.
  - You are about to drop the column `totalQuantity` on the `Order` table. All the data in the column will be lost.
  - You are about to drop the column `createdAt` on the `Rider` table. All the data in the column will be lost.
  - You are about to drop the column `driversLicence` on the `Rider` table. All the data in the column will be lost.
  - You are about to drop the column `updatedAt` on the `Rider` table. All the data in the column will be lost.
  - The `vehicleType` column on the `Rider` table would be dropped and recreated. This will lead to data loss if there is data in the column.
  - A unique constraint covering the columns `[email]` on the table `User` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateEnum
CREATE TYPE "VoucherStatus" AS ENUM ('ACTIVE', 'PENDING', 'EXPIRED');

-- CreateEnum
CREATE TYPE "ServiceType" AS ENUM ('Fire', 'Police', 'Medical', 'MentalHealth', 'Disaster', 'Other');

-- AlterEnum
BEGIN;
CREATE TYPE "OrderStatus_new" AS ENUM ('Pending', 'Preparing', 'Ready', 'Incoming', 'Completed', 'Cancelled');
ALTER TABLE "Order" ALTER COLUMN "status" DROP DEFAULT;
ALTER TABLE "Order" ALTER COLUMN "status" TYPE "OrderStatus_new" USING ("status"::text::"OrderStatus_new");
ALTER TYPE "OrderStatus" RENAME TO "OrderStatus_old";
ALTER TYPE "OrderStatus_new" RENAME TO "OrderStatus";
DROP TYPE "OrderStatus_old";
ALTER TABLE "Order" ALTER COLUMN "status" SET DEFAULT 'Preparing';
COMMIT;

-- DropForeignKey
ALTER TABLE "Item" DROP CONSTRAINT "Item_cartId_fkey";

-- DropForeignKey
ALTER TABLE "Order" DROP CONSTRAINT "Order_riderId_fkey";

-- DropForeignKey
ALTER TABLE "Rider" DROP CONSTRAINT "Rider_userId_fkey";

-- DropIndex
DROP INDEX "Category_productId_key";

-- DropIndex
DROP INDEX "Order_customerId_key";

-- DropIndex
DROP INDEX "Order_riderId_key";

-- AlterTable
ALTER TABLE "Item" ADD COLUMN     "orderId" INTEGER,
ALTER COLUMN "cartId" DROP NOT NULL;

-- AlterTable
ALTER TABLE "Order" DROP COLUMN "totalQuantity",
ADD COLUMN     "deliveryFee" DOUBLE PRECISION NOT NULL DEFAULT 0,
ADD COLUMN     "discount" DOUBLE PRECISION NOT NULL DEFAULT 0,
ALTER COLUMN "riderId" DROP NOT NULL;

-- AlterTable
ALTER TABLE "Rider" DROP COLUMN "createdAt",
DROP COLUMN "driversLicence",
DROP COLUMN "updatedAt",
ADD COLUMN     "brand" TEXT,
ADD COLUMN     "clearanceUrl" TEXT,
ADD COLUMN     "coins" INTEGER NOT NULL DEFAULT 0,
ADD COLUMN     "color" TEXT,
ADD COLUMN     "driversLicenceUrl" TEXT,
ADD COLUMN     "isRegisteredOwner" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "motorType" TEXT,
ADD COLUMN     "plateNumber" TEXT,
ADD COLUMN     "supportingDocumentUrl" TEXT,
ADD COLUMN     "vaccinationCardUrl" TEXT,
ADD COLUMN     "vehicle_document" TEXT,
ADD COLUMN     "year" TEXT,
ADD COLUMN     "yearModelDisplacement" TEXT,
ALTER COLUMN "userId" DROP NOT NULL,
DROP COLUMN "vehicleType",
ADD COLUMN     "vehicleType" TEXT;

-- AlterTable
ALTER TABLE "User" ADD COLUMN     "address" TEXT,
ADD COLUMN     "dateOfBirth" TEXT,
ALTER COLUMN "role" DROP NOT NULL;

-- CreateTable
CREATE TABLE "Address" (
    "id" SERIAL NOT NULL,
    "addressLineOne" TEXT NOT NULL,
    "addressLineTwo" TEXT,
    "street" TEXT NOT NULL,
    "barangay" TEXT NOT NULL,
    "city" TEXT NOT NULL,
    "latitude" DOUBLE PRECISION NOT NULL,
    "longitude" DOUBLE PRECISION NOT NULL,
    "customerId" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "PaymentMethod" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "accountNumber" TEXT NOT NULL,
    "accountName" TEXT NOT NULL,
    "expiration" TEXT,
    "cvv" INTEGER,
    "customerId" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "Vouchers" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "status" "VoucherStatus" NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "description" TEXT NOT NULL,
    "isUsed" BOOLEAN NOT NULL,
    "duration" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "Report" (
    "id" SERIAL NOT NULL,
    "reportIssues" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "reportNotes" TEXT,
    "reportImageURL" TEXT,
    "orderId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "EmergencyContact" (
    "id" SERIAL NOT NULL,
    "stationName" TEXT NOT NULL,
    "serviceType" "ServiceType" NOT NULL,
    "phoneNumbers" TEXT[],
    "address" TEXT NOT NULL,
    "email" TEXT,
    "contactPerson" TEXT,
    "latitude" DECIMAL(65,30) NOT NULL,
    "longitude" DECIMAL(65,30) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "Vouchers_name_key" ON "Vouchers"("name");

-- CreateIndex
CREATE INDEX "station_name_idx" ON "EmergencyContact"("stationName");

-- CreateIndex
CREATE INDEX "service_type_idx" ON "EmergencyContact"("serviceType");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- AddForeignKey
ALTER TABLE "Rider" ADD CONSTRAINT "Rider_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Address" ADD CONSTRAINT "Address_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PaymentMethod" ADD CONSTRAINT "PaymentMethod_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Order" ADD CONSTRAINT "Order_riderId_fkey" FOREIGN KEY ("riderId") REFERENCES "Rider"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Report" ADD CONSTRAINT "Report_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "Order"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Item" ADD CONSTRAINT "Item_cartId_fkey" FOREIGN KEY ("cartId") REFERENCES "Cart"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Item" ADD CONSTRAINT "Item_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "Order"("id") ON DELETE SET NULL ON UPDATE CASCADE;
