-- Create Schemas
CREATE SCHEMA IF NOT EXISTS "xplr";

-- CreateEnum
CREATE TYPE "xplr"."LocationType" AS ENUM ('RETAIL', 'RESTAURANT', 'BAR', 'OTHER');

-- CreateEnum
CREATE TYPE "xplr"."AllocationDistributionMethod" AS ENUM ('RAFFLE_LOTTERY', 'INCLUDED_IN_BUNDLE', 'HOLD_FOR_REGULARS', 'MUSEUM_PRICING', 'SCHEDULED_RELEASE', 'RANDOM_DROPS', 'REWARD_PROGRAM', 'HELD_IN_BACK', 'OTHER');

-- CreateEnum
CREATE TYPE "xplr"."PricingTier" AS ENUM ('RETAIL', 'RETAIL_PLUS', 'ABOVE_MARKET', 'SECONDARY', 'MUSEUM');

-- CreateTable
CREATE TABLE "xplr"."user" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "firstName" TEXT,
    "lastName" TEXT,
    "email" TEXT NOT NULL,
    "emailVerified" BOOLEAN NOT NULL,
    "image" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "role" TEXT,
    "avatar" TEXT,

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

-- CreateTable
CREATE TABLE "xplr"."session" (
    "id" TEXT NOT NULL,
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "token" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "userId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "xplr"."account" (
    "id" TEXT NOT NULL,
    "accountId" TEXT NOT NULL,
    "providerId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "accessToken" TEXT,
    "refreshToken" TEXT,
    "idToken" TEXT,
    "accessTokenExpiresAt" TIMESTAMP(3),
    "refreshTokenExpiresAt" TIMESTAMP(3),
    "scope" TEXT,
    "password" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "xplr"."verification" (
    "id" TEXT NOT NULL,
    "identifier" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3),
    "updatedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "xplr"."store_brands" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "xplr"."locations" (
    "id" SERIAL NOT NULL,
    "brand_id" INTEGER,
    "name" TEXT,
    "address" TEXT NOT NULL,
    "city" TEXT NOT NULL,
    "state" TEXT,
    "country" TEXT,
    "postal_code" TEXT,
    "latitude" DECIMAL(9,6),
    "longitude" DECIMAL(9,6),
    "google_maps_url" TEXT,
    "type" "xplr"."LocationType" NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "xplr"."store_attributes" (
    "id" SERIAL NOT NULL,
    "location_id" INTEGER NOT NULL,
    "submitted_by" TEXT,
    "has_store_picks" BOOLEAN,
    "offers_samples" BOOLEAN,
    "pricing_tier" "xplr"."PricingTier",
    "control_product_price" DECIMAL(8,2),
    "is_state_controlled" BOOLEAN,
    "is_correction" BOOLEAN,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "xplr"."store_allocated_methods" (
    "store_attribute_id" INTEGER NOT NULL,
    "method" "xplr"."AllocationDistributionMethod" NOT NULL,

    CONSTRAINT "store_allocated_methods_pkey" PRIMARY KEY ("store_attribute_id","method")
);

-- CreateTable
CREATE TABLE "xplr"."store_attribute_votes" (
    "id" SERIAL NOT NULL,
    "attribute_id" INTEGER NOT NULL,
    "user_id" TEXT NOT NULL,
    "vote" INTEGER NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "user_email_key" ON "xplr"."user"("email");

-- CreateIndex
CREATE UNIQUE INDEX "session_token_key" ON "xplr"."session"("token");

-- CreateIndex
CREATE UNIQUE INDEX "store_brands_name_key" ON "xplr"."store_brands"("name");

-- CreateIndex
CREATE INDEX "idx_locations_city_state" ON "xplr"."locations"("city", "state");

-- CreateIndex
CREATE INDEX "idx_store_attr_location" ON "xplr"."store_attributes"("location_id", "created_at");

-- CreateIndex
CREATE UNIQUE INDEX "uq_attr_vote_user" ON "xplr"."store_attribute_votes"("attribute_id", "user_id");

-- AddForeignKey
ALTER TABLE "xplr"."session" ADD CONSTRAINT "session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "xplr"."user"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "xplr"."account" ADD CONSTRAINT "account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "xplr"."user"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "xplr"."locations" ADD CONSTRAINT "locations_brand_id_fkey" FOREIGN KEY ("brand_id") REFERENCES "xplr"."store_brands"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "xplr"."store_attributes" ADD CONSTRAINT "store_attributes_location_id_fkey" FOREIGN KEY ("location_id") REFERENCES "xplr"."locations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "xplr"."store_attributes" ADD CONSTRAINT "store_attributes_submitted_by_fkey" FOREIGN KEY ("submitted_by") REFERENCES "xplr"."user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "xplr"."store_allocated_methods" ADD CONSTRAINT "store_allocated_methods_store_attribute_id_fkey" FOREIGN KEY ("store_attribute_id") REFERENCES "xplr"."store_attributes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "xplr"."store_attribute_votes" ADD CONSTRAINT "store_attribute_votes_attribute_id_fkey" FOREIGN KEY ("attribute_id") REFERENCES "xplr"."store_attributes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "xplr"."store_attribute_votes" ADD CONSTRAINT "store_attribute_votes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "xplr"."user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
