-- CreateTable
CREATE TABLE "Session" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "shop" TEXT NOT NULL,
    "state" TEXT NOT NULL,
    "isOnline" BOOLEAN NOT NULL DEFAULT false,
    "scope" TEXT,
    "expires" DATETIME,
    "accessToken" TEXT NOT NULL,
    "userId" BIGINT,
    "firstName" TEXT,
    "lastName" TEXT,
    "email" TEXT,
    "accountOwner" BOOLEAN NOT NULL DEFAULT false,
    "locale" TEXT,
    "collaborator" BOOLEAN DEFAULT false,
    "emailVerified" BOOLEAN DEFAULT false
);

-- CreateTable
CREATE TABLE "Dashboard" (
    "id" TEXT NOT NULL PRIMARY KEY DEFAULT '1',
    "appEnabled" BOOLEAN NOT NULL DEFAULT true,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "Campaign" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "shop" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "status" BOOLEAN NOT NULL DEFAULT true,
    "type" TEXT NOT NULL,
    "action" JSONB NOT NULL,
    "schedule" JSONB NOT NULL,
    "transform" JSONB NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "OrderAppliedOffer" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "lineItemId" BIGINT NOT NULL,
    "lineItemPrice" REAL NOT NULL,
    "orderId" BIGINT NOT NULL,
    "customerId" BIGINT,
    "campaignId" TEXT NOT NULL,
    "campaignName" TEXT NOT NULL,
    "campaignType" TEXT NOT NULL,
    "appliedTag" TEXT,
    "triggerProducts" JSONB NOT NULL,
    "offerProducts" JSONB NOT NULL,
    "discountType" TEXT NOT NULL,
    "discountValue" REAL,
    "freeQty" INTEGER,
    "freeProduct" TEXT,
    "thresholdRule" JSONB,
    "stackTiers" BOOLEAN NOT NULL DEFAULT false,
    "autoAdded" BOOLEAN NOT NULL DEFAULT false,
    "orderSubtotal" REAL NOT NULL,
    "orderTotal" REAL NOT NULL,
    "customerTags" TEXT NOT NULL,
    "currency" TEXT,
    "storeId" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "AutomaticDiscount" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "discountID" TEXT NOT NULL,
    "shop" TEXT
);

-- CreateTable
CREATE TABLE "Target" (
    "id" TEXT NOT NULL PRIMARY KEY DEFAULT '1',
    "customerTags" JSONB,
    "excludeCustomerTags" JSONB,
    "countriesAllow" JSONB,
    "countriesBlock" JSONB,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "SubscriptionPlan" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "shop" TEXT NOT NULL,
    "planName" TEXT NOT NULL,
    "planPrice" REAL NOT NULL,
    "orderLimit" INTEGER NOT NULL,
    "subscriptionId" TEXT,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "currentPeriodStart" DATETIME,
    "currentPeriodEnd" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "BillingUsage" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "shop" TEXT NOT NULL,
    "month" TEXT NOT NULL,
    "orderCount" INTEGER NOT NULL DEFAULT 0,
    "lastCalculatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "UsageAlertStatus" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "shop" TEXT NOT NULL,
    "month" TEXT NOT NULL,
    "warning90Sent" BOOLEAN NOT NULL DEFAULT false,
    "limitReachedSent" BOOLEAN NOT NULL DEFAULT false
);

-- CreateIndex
CREATE INDEX "Campaign_shop_idx" ON "Campaign"("shop");

-- CreateIndex
CREATE INDEX "Campaign_shop_status_idx" ON "Campaign"("shop", "status");

-- CreateIndex
CREATE INDEX "OrderAppliedOffer_orderId_idx" ON "OrderAppliedOffer"("orderId");

-- CreateIndex
CREATE INDEX "OrderAppliedOffer_campaignId_idx" ON "OrderAppliedOffer"("campaignId");

-- CreateIndex
CREATE INDEX "OrderAppliedOffer_customerId_idx" ON "OrderAppliedOffer"("customerId");

-- CreateIndex
CREATE UNIQUE INDEX "SubscriptionPlan_shop_key" ON "SubscriptionPlan"("shop");

-- CreateIndex
CREATE UNIQUE INDEX "BillingUsage_shop_month_key" ON "BillingUsage"("shop", "month");

-- CreateIndex
CREATE UNIQUE INDEX "UsageAlertStatus_shop_month_key" ON "UsageAlertStatus"("shop", "month");
