-- CreateEnum
CREATE TYPE "UserRole" AS ENUM ('ADMIN', 'DISPATCH_MANAGER', 'DISTRIBUTOR');

-- CreateTable
CREATE TABLE "units" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "units_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "users" (
    "id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "firstName" TEXT NOT NULL,
    "lastName" TEXT NOT NULL,
    "role" "UserRole" NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "branchId" TEXT,
    "unitId" TEXT,
    "passwordResetToken" TEXT,
    "passwordResetExpiresAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "branches" (
    "id" TEXT NOT NULL,
    "hoCode" TEXT NOT NULL,
    "branchDesc" TEXT NOT NULL,
    "location" TEXT NOT NULL,
    "address" TEXT NOT NULL,
    "pinCode" TEXT NOT NULL,
    "delvAddress" TEXT,
    "delvPinCode" TEXT,
    "stateCode" TEXT NOT NULL,
    "contactNo" TEXT NOT NULL,
    "emailId" TEXT NOT NULL,
    "panNo" TEXT NOT NULL,
    "drugLicNo" TEXT,
    "drugLicDate" TEXT,
    "saleTaxNo" TEXT,
    "cstNo" TEXT,
    "vatNo" TEXT,
    "gstNo" TEXT NOT NULL,
    "gstDate" TEXT,
    "distance" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "unitId" TEXT,

    CONSTRAINT "branches_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "orders" (
    "id" TEXT NOT NULL,
    "docNo" TEXT,
    "docDt" TIMESTAMP(3) NOT NULL,
    "branchCode" TEXT NOT NULL,
    "branchId" TEXT,
    "unitId" TEXT,
    "createdBy" TEXT,
    "totPackage" INTEGER NOT NULL DEFAULT 0,
    "totWeight" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totAmount" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totPDiscAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totDiscountedAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totSDiscAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totTaxableAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totCGSTAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totSGSTAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totIGSTAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totGrossAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "freightAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "specDiscAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "roundOff" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totNetAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "remarks" TEXT,
    "linkedInvoiceId" TEXT,
    "erpSyncedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "orderDate" TIMESTAMP(3),

    CONSTRAINT "orders_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "products" (
    "id" TEXT NOT NULL,
    "hoCode" TEXT NOT NULL,
    "itemDesc" TEXT NOT NULL,
    "unitCode" TEXT NOT NULL,
    "hsn" TEXT NOT NULL,
    "igstRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "cgstRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "sgstRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "packQty" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "tertiaryQty" TEXT,
    "secondaryQty" TEXT,
    "primaryQty" TEXT,
    "weight" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "unitId" TEXT,

    CONSTRAINT "products_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "order_products" (
    "id" TEXT NOT NULL,
    "orderId" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "unitId" TEXT,
    "quantity" DECIMAL(65,30) NOT NULL,
    "unitPrice" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totalAmount" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "order_products_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "invoice_line_items" (
    "id" TEXT NOT NULL,
    "docNo" TEXT NOT NULL,
    "slNo" INTEGER NOT NULL,
    "itemCode" TEXT NOT NULL,
    "unitId" TEXT,
    "shipper" INTEGER NOT NULL DEFAULT 1,
    "piece" INTEGER NOT NULL DEFAULT 0,
    "qty" DECIMAL(65,30) NOT NULL,
    "rate" DECIMAL(65,30) NOT NULL,
    "amount" DECIMAL(65,30) NOT NULL,
    "pDiscRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "pDiscAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "discountedAmt" DECIMAL(65,30) NOT NULL,
    "sDiscRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "sDiscAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "taxableAmt" DECIMAL(65,30) NOT NULL,
    "cgstRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "cgstAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "sgstRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "sgstAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "igstRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "igstAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "netAmt" DECIMAL(65,30) NOT NULL,
    "mrp" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "weightRate" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "netWeight" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "remarks" TEXT,
    "invoiceId" TEXT,
    "productId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "invoice_line_items_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "invoice_products" (
    "id" TEXT NOT NULL,
    "invoiceId" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "unitId" TEXT,
    "quantity" DECIMAL(65,30) NOT NULL,
    "unitPrice" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totalAmount" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "invoice_products_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "product_shortfalls" (
    "id" TEXT NOT NULL,
    "orderId" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "unitId" TEXT,
    "orderedQty" DECIMAL(65,30) NOT NULL,
    "invoicedQty" DECIMAL(65,30) NOT NULL,
    "shortfallQty" DECIMAL(65,30) NOT NULL,
    "emailSent" BOOLEAN NOT NULL DEFAULT false,
    "emailSentAt" TIMESTAMP(3),
    "resolvedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "product_shortfalls_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "shortfall_analytics_daily" (
    "id" TEXT NOT NULL,
    "date" TIMESTAMP(3) NOT NULL,
    "branchId" TEXT,
    "unitId" TEXT,
    "branchKey" TEXT NOT NULL DEFAULT 'GLOBAL',
    "totalShortfallItems" INTEGER NOT NULL DEFAULT 0,
    "totalShortfallQty" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totalOrderedQty" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totalOrdersWithShortfall" INTEGER NOT NULL DEFAULT 0,
    "totalOrders" INTEGER NOT NULL DEFAULT 0,
    "totalShortfallAmount" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "shortfall_analytics_daily_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "invoices" (
    "id" TEXT NOT NULL,
    "docNo" TEXT NOT NULL,
    "docDt" TIMESTAMP(3) NOT NULL,
    "branchCode" TEXT NOT NULL,
    "branchId" TEXT,
    "unitId" TEXT,
    "totPackage" INTEGER NOT NULL DEFAULT 0,
    "totWeight" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totAmount" DECIMAL(65,30) NOT NULL,
    "totPDiscAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totDiscountedAmt" DECIMAL(65,30) NOT NULL,
    "totSDiscAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totTaxableAmt" DECIMAL(65,30) NOT NULL,
    "totCGSTAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totSGSTAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totIGSTAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totGrossAmt" DECIMAL(65,30) NOT NULL,
    "freightAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "specDiscAmt" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "roundOff" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "totNetAmt" DECIMAL(65,30) NOT NULL,
    "remarks" TEXT,
    "erpSyncedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "invoices_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "order_invoices" (
    "id" TEXT NOT NULL,
    "orderId" TEXT NOT NULL,
    "invoiceId" TEXT NOT NULL,
    "unitId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "order_invoices_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "erp_syncs" (
    "id" TEXT NOT NULL,
    "entityType" TEXT NOT NULL,
    "syncType" TEXT NOT NULL,
    "lastSyncAt" TIMESTAMP(3) NOT NULL,
    "nextSyncAt" TIMESTAMP(3),
    "syncStatus" TEXT NOT NULL,
    "recordsCount" INTEGER NOT NULL DEFAULT 0,
    "errorMessage" TEXT,
    "syncDuration" INTEGER,
    "unitId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "erp_syncs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "sync_logs" (
    "id" TEXT NOT NULL,
    "syncId" TEXT NOT NULL,
    "entityType" TEXT NOT NULL,
    "entityId" TEXT NOT NULL,
    "operation" TEXT NOT NULL,
    "status" TEXT NOT NULL,
    "errorMessage" TEXT,
    "unitId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "sync_logs_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "units_name_key" ON "units"("name");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE INDEX "users_unitId_idx" ON "users"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "branches_hoCode_key" ON "branches"("hoCode");

-- CreateIndex
CREATE INDEX "branches_unitId_idx" ON "branches"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "orders_docNo_key" ON "orders"("docNo");

-- CreateIndex
CREATE INDEX "orders_unitId_idx" ON "orders"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "products_hoCode_key" ON "products"("hoCode");

-- CreateIndex
CREATE INDEX "products_unitId_idx" ON "products"("unitId");

-- CreateIndex
CREATE INDEX "order_products_unitId_idx" ON "order_products"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "order_products_orderId_productId_key" ON "order_products"("orderId", "productId");

-- CreateIndex
CREATE INDEX "invoice_line_items_unitId_idx" ON "invoice_line_items"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "invoice_line_items_docNo_slNo_key" ON "invoice_line_items"("docNo", "slNo");

-- CreateIndex
CREATE INDEX "invoice_products_unitId_idx" ON "invoice_products"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "invoice_products_invoiceId_productId_key" ON "invoice_products"("invoiceId", "productId");

-- CreateIndex
CREATE INDEX "product_shortfalls_unitId_idx" ON "product_shortfalls"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "product_shortfalls_orderId_productId_key" ON "product_shortfalls"("orderId", "productId");

-- CreateIndex
CREATE INDEX "shortfall_analytics_daily_unitId_idx" ON "shortfall_analytics_daily"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "shortfall_analytics_daily_date_branchKey_key" ON "shortfall_analytics_daily"("date", "branchKey");

-- CreateIndex
CREATE UNIQUE INDEX "invoices_docNo_key" ON "invoices"("docNo");

-- CreateIndex
CREATE INDEX "invoices_unitId_idx" ON "invoices"("unitId");

-- CreateIndex
CREATE INDEX "order_invoices_unitId_idx" ON "order_invoices"("unitId");

-- CreateIndex
CREATE UNIQUE INDEX "order_invoices_orderId_invoiceId_key" ON "order_invoices"("orderId", "invoiceId");

-- CreateIndex
CREATE INDEX "erp_syncs_unitId_idx" ON "erp_syncs"("unitId");

-- CreateIndex
CREATE INDEX "sync_logs_unitId_idx" ON "sync_logs"("unitId");

-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_branchId_fkey" FOREIGN KEY ("branchId") REFERENCES "branches"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_unitId_fkey" FOREIGN KEY ("unitId") REFERENCES "units"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "branches" ADD CONSTRAINT "branches_unitId_fkey" FOREIGN KEY ("unitId") REFERENCES "units"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "orders" ADD CONSTRAINT "orders_branchId_fkey" FOREIGN KEY ("branchId") REFERENCES "branches"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "orders" ADD CONSTRAINT "orders_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "orders" ADD CONSTRAINT "orders_linkedInvoiceId_fkey" FOREIGN KEY ("linkedInvoiceId") REFERENCES "invoices"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "products" ADD CONSTRAINT "products_unitId_fkey" FOREIGN KEY ("unitId") REFERENCES "units"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "order_products" ADD CONSTRAINT "order_products_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "orders"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "order_products" ADD CONSTRAINT "order_products_productId_fkey" FOREIGN KEY ("productId") REFERENCES "products"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoice_line_items" ADD CONSTRAINT "invoice_line_items_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "invoices"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoice_line_items" ADD CONSTRAINT "invoice_line_items_productId_fkey" FOREIGN KEY ("productId") REFERENCES "products"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoice_products" ADD CONSTRAINT "invoice_products_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "invoices"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoice_products" ADD CONSTRAINT "invoice_products_productId_fkey" FOREIGN KEY ("productId") REFERENCES "products"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "product_shortfalls" ADD CONSTRAINT "product_shortfalls_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "orders"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "product_shortfalls" ADD CONSTRAINT "product_shortfalls_productId_fkey" FOREIGN KEY ("productId") REFERENCES "products"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shortfall_analytics_daily" ADD CONSTRAINT "shortfall_analytics_daily_branchId_fkey" FOREIGN KEY ("branchId") REFERENCES "branches"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_branchId_fkey" FOREIGN KEY ("branchId") REFERENCES "branches"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_unitId_fkey" FOREIGN KEY ("unitId") REFERENCES "units"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "order_invoices" ADD CONSTRAINT "order_invoices_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "orders"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "order_invoices" ADD CONSTRAINT "order_invoices_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "invoices"("id") ON DELETE CASCADE ON UPDATE CASCADE;
