-- CreateEnum
CREATE TYPE "Frequency" AS ENUM ('PUNCTUAL', 'RECURRENT');

-- CreateEnum
CREATE TYPE "AddressType" AS ENUM ('HOUSE_NUMBER', 'STREET_ADDRESS', 'LOCALITY', 'VENUE', 'OTHER');

-- CreateTable
CREATE TABLE "ad" (
    "uuid" UUID NOT NULL,
    "userUuid" UUID NOT NULL,
    "driver" BOOLEAN,
    "passenger" BOOLEAN,
    "frequency" "Frequency" NOT NULL DEFAULT 'RECURRENT',
    "fromDate" DATE NOT NULL,
    "toDate" DATE,
    "monTime" TIMESTAMPTZ,
    "tueTime" TIMESTAMPTZ,
    "wedTime" TIMESTAMPTZ,
    "thuTime" TIMESTAMPTZ,
    "friTime" TIMESTAMPTZ,
    "satTime" TIMESTAMPTZ,
    "sunTime" TIMESTAMPTZ,
    "monMargin" INTEGER,
    "tueMargin" INTEGER,
    "wedMargin" INTEGER,
    "thuMargin" INTEGER,
    "friMargin" INTEGER,
    "satMargin" INTEGER,
    "sunMargin" INTEGER,
    "seatsDriver" SMALLINT,
    "seatsPassenger" SMALLINT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "ad_pkey" PRIMARY KEY ("uuid")
);

-- CreateTable
CREATE TABLE "address" (
    "uuid" UUID NOT NULL,
    "adUuid" UUID NOT NULL,
    "position" SMALLINT NOT NULL,
    "lon" DOUBLE PRECISION NOT NULL,
    "lat" DOUBLE PRECISION NOT NULL,
    "houseNumber" TEXT,
    "street" TEXT,
    "locality" TEXT,
    "postalCode" TEXT,
    "country" TEXT,
    "type" "AddressType" DEFAULT 'OTHER',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "address_pkey" PRIMARY KEY ("uuid")
);

-- CreateIndex
CREATE INDEX "ad_driver_idx" ON "ad"("driver");

-- CreateIndex
CREATE INDEX "ad_passenger_idx" ON "ad"("passenger");

-- CreateIndex
CREATE INDEX "ad_fromDate_idx" ON "ad"("fromDate");

-- CreateIndex
CREATE INDEX "ad_toDate_idx" ON "ad"("toDate");

-- AddForeignKey
ALTER TABLE "address" ADD CONSTRAINT "address_adUuid_fkey" FOREIGN KEY ("adUuid") REFERENCES "ad"("uuid") ON DELETE CASCADE ON UPDATE CASCADE;
