-- CreateTable
CREATE TABLE "payments" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "referenceId" TEXT NOT NULL,
    "amount" INTEGER NOT NULL,
    "currency" TEXT NOT NULL DEFAULT 'ARS',
    "provider" TEXT NOT NULL DEFAULT 'MERCADOPAGO',
    "providerPaymentId" TEXT,
    "providerPreferenceId" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'PENDING',
    "paymentMethod" TEXT,
    "installments" INTEGER,
    "metadata" TEXT,
    "paidAt" DATETIME,
    "refundedAt" DATETIME,
    "refundAmount" INTEGER,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "payments_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "bonus_packs" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "numberOfBookings" INTEGER NOT NULL,
    "price" INTEGER NOT NULL,
    "validityDays" INTEGER NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "user_bonuses" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "bonusPackId" TEXT NOT NULL,
    "totalBookings" INTEGER NOT NULL,
    "usedBookings" INTEGER NOT NULL DEFAULT 0,
    "remainingBookings" INTEGER NOT NULL,
    "purchaseDate" DATETIME NOT NULL,
    "expirationDate" DATETIME NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "paymentId" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "user_bonuses_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "user_bonuses_bonusPackId_fkey" FOREIGN KEY ("bonusPackId") REFERENCES "bonus_packs" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "bonus_usages" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userBonusId" TEXT NOT NULL,
    "bookingId" TEXT NOT NULL,
    "usedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "bonus_usages_userBonusId_fkey" FOREIGN KEY ("userBonusId") REFERENCES "user_bonuses" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "bonus_usages_bookingId_fkey" FOREIGN KEY ("bookingId") REFERENCES "bookings" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "subscription_plans" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "type" TEXT NOT NULL,
    "price" INTEGER NOT NULL,
    "features" TEXT,
    "benefits" TEXT NOT NULL,
    "mercadoPagoPlanId" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "user_subscriptions" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "planId" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'PENDING',
    "startDate" DATETIME,
    "endDate" DATETIME,
    "currentPeriodStart" DATETIME,
    "currentPeriodEnd" DATETIME,
    "cancelAtPeriodEnd" BOOLEAN NOT NULL DEFAULT false,
    "mercadoPagoSubscriptionId" TEXT,
    "paymentMethodId" TEXT,
    "lastPaymentDate" DATETIME,
    "nextPaymentDate" DATETIME,
    "freeBookingsUsed" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "user_subscriptions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "user_subscriptions_planId_fkey" FOREIGN KEY ("planId") REFERENCES "subscription_plans" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "payments_providerPreferenceId_key" ON "payments"("providerPreferenceId");

-- CreateIndex
CREATE INDEX "payments_userId_idx" ON "payments"("userId");

-- CreateIndex
CREATE INDEX "payments_status_idx" ON "payments"("status");

-- CreateIndex
CREATE INDEX "payments_type_referenceId_idx" ON "payments"("type", "referenceId");

-- CreateIndex
CREATE INDEX "payments_providerPaymentId_idx" ON "payments"("providerPaymentId");

-- CreateIndex
CREATE INDEX "payments_providerPreferenceId_idx" ON "payments"("providerPreferenceId");

-- CreateIndex
CREATE INDEX "payments_createdAt_idx" ON "payments"("createdAt");

-- CreateIndex
CREATE INDEX "bonus_packs_isActive_idx" ON "bonus_packs"("isActive");

-- CreateIndex
CREATE INDEX "user_bonuses_userId_idx" ON "user_bonuses"("userId");

-- CreateIndex
CREATE INDEX "user_bonuses_status_idx" ON "user_bonuses"("status");

-- CreateIndex
CREATE INDEX "user_bonuses_expirationDate_idx" ON "user_bonuses"("expirationDate");

-- CreateIndex
CREATE INDEX "user_bonuses_userId_status_idx" ON "user_bonuses"("userId", "status");

-- CreateIndex
CREATE INDEX "bonus_usages_userBonusId_idx" ON "bonus_usages"("userBonusId");

-- CreateIndex
CREATE INDEX "bonus_usages_usedAt_idx" ON "bonus_usages"("usedAt");

-- CreateIndex
CREATE UNIQUE INDEX "bonus_usages_bookingId_key" ON "bonus_usages"("bookingId");

-- CreateIndex
CREATE INDEX "subscription_plans_type_idx" ON "subscription_plans"("type");

-- CreateIndex
CREATE INDEX "subscription_plans_isActive_idx" ON "subscription_plans"("isActive");

-- CreateIndex
CREATE INDEX "user_subscriptions_userId_idx" ON "user_subscriptions"("userId");

-- CreateIndex
CREATE INDEX "user_subscriptions_planId_idx" ON "user_subscriptions"("planId");

-- CreateIndex
CREATE INDEX "user_subscriptions_status_idx" ON "user_subscriptions"("status");

-- CreateIndex
CREATE INDEX "user_subscriptions_mercadoPagoSubscriptionId_idx" ON "user_subscriptions"("mercadoPagoSubscriptionId");

-- CreateIndex
CREATE UNIQUE INDEX "user_subscriptions_userId_status_key" ON "user_subscriptions"("userId", "status");
