-- CreateTable
CREATE TABLE "User" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "email" TEXT NOT NULL,
    "passwordHash" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "company" TEXT,
    "role" TEXT NOT NULL DEFAULT 'SELLER',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "Session" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "token" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "expiresAt" DATETIME NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Listing" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "assetType" TEXT,
    "stage" TEXT,
    "country" TEXT NOT NULL,
    "region" TEXT,
    "city" TEXT,
    "lat" REAL,
    "lng" REAL,
    "priceUsd" INTEGER,
    "surfaceHa" REAL,
    "concessionCount" INTEGER,
    "verified" BOOLEAN NOT NULL DEFAULT false,
    "imageSeed" TEXT,
    "publishedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "status" TEXT NOT NULL DEFAULT 'PUBLISHED',
    "ownerId" TEXT,
    "offerType" TEXT,
    "headline" TEXT,
    "sellReason" TEXT,
    "sellerCompany" TEXT,
    "sellerWebsite" TEXT,
    "sellerType" TEXT,
    "mineralizationType" TEXT,
    "works" TEXT,
    "waterAccess" TEXT,
    "energyAccess" TEXT,
    "esgEnvStatus" TEXT,
    "esgClosurePlan" TEXT,
    "esgCommunity" TEXT,
    "esgLiabilities" TEXT,
    "contactName" TEXT,
    "contactRole" TEXT,
    "contactEmail" TEXT,
    "contactPhone" TEXT,
    "contactPreference" TEXT,
    "hasAgent" BOOLEAN NOT NULL DEFAULT false,
    CONSTRAINT "Listing_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Listing" ("assetType", "category", "city", "concessionCount", "country", "description", "id", "imageSeed", "lat", "lng", "priceUsd", "publishedAt", "region", "slug", "stage", "surfaceHa", "title", "verified") SELECT "assetType", "category", "city", "concessionCount", "country", "description", "id", "imageSeed", "lat", "lng", "priceUsd", "publishedAt", "region", "slug", "stage", "surfaceHa", "title", "verified" FROM "Listing";
DROP TABLE "Listing";
ALTER TABLE "new_Listing" RENAME TO "Listing";
CREATE UNIQUE INDEX "Listing_slug_key" ON "Listing"("slug");
CREATE INDEX "Listing_category_idx" ON "Listing"("category");
CREATE INDEX "Listing_country_idx" ON "Listing"("country");
CREATE INDEX "Listing_status_idx" ON "Listing"("status");
CREATE INDEX "Listing_ownerId_idx" ON "Listing"("ownerId");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

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

-- CreateIndex
CREATE UNIQUE INDEX "Session_token_key" ON "Session"("token");

-- CreateIndex
CREATE INDEX "Session_userId_idx" ON "Session"("userId");
