-- CreateTable
CREATE TABLE "CentralStock" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "productId" TEXT NOT NULL,
    "quantityAvailable" INTEGER NOT NULL DEFAULT 0,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "CentralStock_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Procurement" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "supplierName" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "quantity" INTEGER NOT NULL,
    "pricePerUnit" REAL NOT NULL,
    "totalCost" REAL NOT NULL,
    "notes" TEXT,
    "createdBy" TEXT NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "Procurement_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Distribution" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "branchId" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "quantity" INTEGER NOT NULL,
    "notes" TEXT,
    "createdBy" TEXT NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "Distribution_branchId_fkey" FOREIGN KEY ("branchId") REFERENCES "Branch" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "Distribution_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ScheduledPrice" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "productId" TEXT NOT NULL,
    "price" REAL NOT NULL,
    "effectiveDate" DATETIME NOT NULL,
    "createdBy" TEXT NOT NULL,
    "appliedAt" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "ScheduledPrice_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "CentralStock_productId_key" ON "CentralStock"("productId");

-- CreateIndex
CREATE INDEX "Procurement_productId_idx" ON "Procurement"("productId");

-- CreateIndex
CREATE INDEX "Procurement_createdAt_idx" ON "Procurement"("createdAt");

-- CreateIndex
CREATE INDEX "Distribution_branchId_idx" ON "Distribution"("branchId");

-- CreateIndex
CREATE INDEX "Distribution_productId_idx" ON "Distribution"("productId");

-- CreateIndex
CREATE INDEX "Distribution_createdAt_idx" ON "Distribution"("createdAt");

-- CreateIndex
CREATE INDEX "ScheduledPrice_productId_idx" ON "ScheduledPrice"("productId");

-- CreateIndex
CREATE INDEX "ScheduledPrice_effectiveDate_idx" ON "ScheduledPrice"("effectiveDate");

-- CreateIndex
CREATE INDEX "ScheduledPrice_appliedAt_idx" ON "ScheduledPrice"("appliedAt");
