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

-- CreateTable
CREATE TABLE "public"."users" (
    "id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "firstName" TEXT NOT NULL,
    "lastName" TEXT NOT NULL,
    "role" "public"."UserRole" NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "branchId" 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 "public"."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,

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

-- CreateTable
CREATE TABLE "public"."orders" (
    "id" TEXT NOT NULL,
    "docNo" TEXT NOT NULL,
    "docDt" TIMESTAMP(3) NOT NULL,
    "branchCode" TEXT NOT NULL,
    "branchId" 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,
    "linkedInvoiceId" TEXT,
    "erpSyncedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."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,

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

-- CreateTable
CREATE TABLE "public"."order_products" (
    "id" TEXT NOT NULL,
    "orderId" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "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 "public"."invoice_line_items" (
    "id" TEXT NOT NULL,
    "docNo" TEXT NOT NULL,
    "slNo" INTEGER NOT NULL,
    "itemCode" TEXT NOT NULL,
    "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 "public"."invoice_products" (
    "id" TEXT NOT NULL,
    "invoiceId" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "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 "public"."product_shortfalls" (
    "id" TEXT NOT NULL,
    "orderId" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "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 "public"."invoices" (
    "id" TEXT NOT NULL,
    "docNo" TEXT NOT NULL,
    "docDt" TIMESTAMP(3) NOT NULL,
    "branchCode" TEXT NOT NULL,
    "branchId" 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 "public"."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,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."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,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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