-- CreateTable
CREATE TABLE "listings" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "slug" TEXT NOT NULL,
    "airbnb_url" TEXT NOT NULL,
    "normalized_url" TEXT NOT NULL,
    "external_id" TEXT,
    "title" TEXT NOT NULL,
    "location_text" TEXT,
    "latitude" REAL,
    "longitude" REAL,
    "nightly_price" REAL,
    "total_price" REAL,
    "currency" TEXT DEFAULT 'EUR',
    "rating" REAL,
    "review_count" INTEGER,
    "guest_count" INTEGER,
    "official_guest_count" INTEGER,
    "max_sleeping_places" INTEGER,
    "suitable_for_4" BOOLEAN,
    "extra_mattresses_needed_for_4" INTEGER,
    "bed_types_summary" TEXT,
    "bedrooms" INTEGER,
    "beds" INTEGER,
    "bathrooms" REAL,
    "description" TEXT,
    "host_name" TEXT,
    "cancellation_policy" TEXT,
    "amenities" TEXT,
    "is_favorite" BOOLEAN NOT NULL DEFAULT false,
    "status" TEXT NOT NULL DEFAULT 'NEW',
    "cover_image" TEXT,
    "raw_source_data" TEXT,
    "imported_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "listing_images" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "listing_id" TEXT NOT NULL,
    "url" TEXT NOT NULL,
    "alt" TEXT,
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "is_external" BOOLEAN NOT NULL DEFAULT true,
    "created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "listing_images_listing_id_fkey" FOREIGN KEY ("listing_id") REFERENCES "listings" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "admin_notes" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "listing_id" TEXT NOT NULL,
    "body" TEXT NOT NULL,
    "created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" DATETIME NOT NULL,
    CONSTRAINT "admin_notes_listing_id_fkey" FOREIGN KEY ("listing_id") REFERENCES "listings" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "tags" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "color" TEXT DEFAULT '#6366f1'
);

-- CreateTable
CREATE TABLE "listing_tags" (
    "listing_id" TEXT NOT NULL,
    "tag_id" TEXT NOT NULL,

    PRIMARY KEY ("listing_id", "tag_id"),
    CONSTRAINT "listing_tags_listing_id_fkey" FOREIGN KEY ("listing_id") REFERENCES "listings" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "listing_tags_tag_id_fkey" FOREIGN KEY ("tag_id") REFERENCES "tags" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "listing_sleeping_options" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "listing_id" TEXT NOT NULL,
    "bed_type" TEXT NOT NULL,
    "quantity" INTEGER NOT NULL DEFAULT 1,
    "spots_per_unit" INTEGER NOT NULL DEFAULT 1,
    "quality" TEXT NOT NULL DEFAULT 'FULL',
    "label" TEXT,
    "notes" TEXT,
    CONSTRAINT "listing_sleeping_options_listing_id_fkey" FOREIGN KEY ("listing_id") REFERENCES "listings" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "listings_slug_key" ON "listings"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "listings_airbnb_url_key" ON "listings"("airbnb_url");

-- CreateIndex
CREATE UNIQUE INDEX "listings_normalized_url_key" ON "listings"("normalized_url");

-- CreateIndex
CREATE UNIQUE INDEX "listings_external_id_key" ON "listings"("external_id");

-- CreateIndex
CREATE INDEX "listings_status_idx" ON "listings"("status");

-- CreateIndex
CREATE INDEX "listings_is_favorite_idx" ON "listings"("is_favorite");

-- CreateIndex
CREATE INDEX "listings_location_text_idx" ON "listings"("location_text");

-- CreateIndex
CREATE INDEX "listings_nightly_price_idx" ON "listings"("nightly_price");

-- CreateIndex
CREATE INDEX "listings_rating_idx" ON "listings"("rating");

-- CreateIndex
CREATE INDEX "listing_images_listing_id_sort_order_idx" ON "listing_images"("listing_id", "sort_order");

-- CreateIndex
CREATE INDEX "admin_notes_listing_id_created_at_idx" ON "admin_notes"("listing_id", "created_at");

-- CreateIndex
CREATE UNIQUE INDEX "tags_name_key" ON "tags"("name");

-- CreateIndex
CREATE UNIQUE INDEX "tags_slug_key" ON "tags"("slug");

-- CreateIndex
CREATE INDEX "listing_sleeping_options_listing_id_idx" ON "listing_sleeping_options"("listing_id");
