-- CreateEnum
CREATE TYPE "TransferCategory" AS ENUM ('FURNITURE', 'APPLIANCE', 'OFFICE_EQUIPMENT', 'KITCHEN', 'PREMISES', 'FULL_UNIT');

-- CreateEnum
CREATE TYPE "TransferCondition" AS ENUM ('NEW', 'LIKE_NEW', 'GOOD', 'FAIR', 'WORN');

-- CreateEnum
CREATE TYPE "TransferListingStatus" AS ENUM ('DRAFT', 'PENDING_REVIEW', 'ACTIVE', 'RESERVED', 'SOLD', 'EXPIRED', 'REJECTED');

-- CreateEnum
CREATE TYPE "TransferPricingSource" AS ENUM ('MANUAL', 'AI_ESTIMATED', 'NEGOTIABLE');

-- CreateTable
CREATE TABLE "TransferListing" (
    "id" TEXT NOT NULL,
    "sellerId" TEXT NOT NULL,
    "category" "TransferCategory" NOT NULL,
    "status" "TransferListingStatus" NOT NULL DEFAULT 'DRAFT',
    "title" TEXT NOT NULL,
    "description" TEXT,
    "address" TEXT NOT NULL,
    "ward" TEXT,
    "district" TEXT NOT NULL,
    "city" TEXT NOT NULL,
    "location" geometry(Point, 4326) NOT NULL,
    "askingPriceVND" BIGINT NOT NULL,
    "aiEstimatePriceVND" BIGINT,
    "aiConfidence" DOUBLE PRECISION,
    "pricingSource" "TransferPricingSource" NOT NULL DEFAULT 'MANUAL',
    "isNegotiable" BOOLEAN NOT NULL DEFAULT true,
    "areaM2" DOUBLE PRECISION,
    "monthlyRentVND" BIGINT,
    "depositMonths" INTEGER,
    "remainingLeaseMo" INTEGER,
    "businessType" TEXT,
    "footTraffic" TEXT,
    "media" JSONB,
    "moderationScore" DOUBLE PRECISION,
    "moderationNotes" TEXT,
    "viewCount" INTEGER NOT NULL DEFAULT 0,
    "saveCount" INTEGER NOT NULL DEFAULT 0,
    "inquiryCount" INTEGER NOT NULL DEFAULT 0,
    "contactPhone" TEXT,
    "contactName" TEXT,
    "featuredUntil" TIMESTAMP(3),
    "expiresAt" TIMESTAMP(3),
    "publishedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "TransferItem" (
    "id" TEXT NOT NULL,
    "transferListingId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "brand" TEXT,
    "modelName" TEXT,
    "category" "TransferCategory" NOT NULL,
    "condition" "TransferCondition" NOT NULL,
    "purchaseYear" INTEGER,
    "originalPriceVND" BIGINT,
    "askingPriceVND" BIGINT NOT NULL,
    "aiEstimatePriceVND" BIGINT,
    "aiConfidence" DOUBLE PRECISION,
    "quantity" INTEGER NOT NULL DEFAULT 1,
    "dimensions" JSONB,
    "media" JSONB,
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex: TransferListing
CREATE INDEX "TransferListing_sellerId_idx" ON "TransferListing"("sellerId");
CREATE INDEX "TransferListing_category_idx" ON "TransferListing"("category");
CREATE INDEX "TransferListing_status_idx" ON "TransferListing"("status");
CREATE INDEX "TransferListing_district_city_idx" ON "TransferListing"("district", "city");
CREATE INDEX "TransferListing_askingPriceVND_idx" ON "TransferListing"("askingPriceVND");
CREATE INDEX "TransferListing_location_idx" ON "TransferListing" USING GIST ("location");
CREATE INDEX "TransferListing_publishedAt_idx" ON "TransferListing"("publishedAt");
CREATE INDEX "TransferListing_createdAt_idx" ON "TransferListing"("createdAt");
CREATE INDEX "TransferListing_featuredUntil_idx" ON "TransferListing"("featuredUntil");
CREATE INDEX "TransferListing_expiresAt_idx" ON "TransferListing"("expiresAt");
CREATE INDEX "TransferListing_category_status_publishedAt_idx" ON "TransferListing"("category", "status", "publishedAt" DESC);
CREATE INDEX "TransferListing_district_city_category_status_idx" ON "TransferListing"("district", "city", "category", "status");
CREATE INDEX "TransferListing_status_createdAt_idx" ON "TransferListing"("status", "createdAt" DESC);

-- CreateIndex: TransferItem
CREATE INDEX "TransferItem_transferListingId_idx" ON "TransferItem"("transferListingId");
CREATE INDEX "TransferItem_category_idx" ON "TransferItem"("category");
CREATE INDEX "TransferItem_condition_idx" ON "TransferItem"("condition");
CREATE INDEX "TransferItem_brand_idx" ON "TransferItem"("brand");
CREATE INDEX "TransferItem_askingPriceVND_idx" ON "TransferItem"("askingPriceVND");
CREATE INDEX "TransferItem_transferListingId_category_idx" ON "TransferItem"("transferListingId", "category");

-- AddForeignKey
ALTER TABLE "TransferListing" ADD CONSTRAINT "TransferListing_sellerId_fkey" FOREIGN KEY ("sellerId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TransferItem" ADD CONSTRAINT "TransferItem_transferListingId_fkey" FOREIGN KEY ("transferListingId") REFERENCES "TransferListing"("id") ON DELETE CASCADE ON UPDATE CASCADE;
