-- CreateEnum
CREATE TYPE "obm_core"."OrderStatus" AS ENUM ('PENDING', 'APPROVED', 'NOT_APPROVED', 'CANCELLED', 'COMPLETED');

-- CreateEnum
CREATE TYPE "obm_core"."PaymentType" AS ENUM ('CASH', 'CHEQUE', 'BANK_TRANSFER');

-- CreateTable
CREATE TABLE "obm_core"."Order" (
    "id" TEXT NOT NULL,
    "orderCode" TEXT NOT NULL,
    "shopId" TEXT NOT NULL,
    "status" "obm_core"."OrderStatus" NOT NULL DEFAULT 'PENDING',
    "remarks" TEXT,
    "totalAmount" DECIMAL(10,2) NOT NULL,
    "placedById" TEXT NOT NULL,
    "paidAmount" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "balanceAmount" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "companyId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "obm_core"."OrderItem" (
    "id" TEXT NOT NULL,
    "orderId" TEXT NOT NULL,
    "productId" TEXT NOT NULL,
    "quantity" INTEGER NOT NULL,
    "bonus" INTEGER NOT NULL DEFAULT 0,
    "batchNumber" TEXT,
    "unitPrice" DECIMAL(10,2) NOT NULL,
    "discountPercent" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "discountAmount" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "gstPercent" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "gstAmount" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "whTaxPercent" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "whTaxAmount" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "netAmount" DECIMAL(10,2) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "obm_core"."OrderAssignment" (
    "id" TEXT NOT NULL,
    "orderId" TEXT NOT NULL,
    "employeeId" TEXT NOT NULL,
    "assignmentType" TEXT NOT NULL,
    "assignmentDate" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "obm_core"."OrderPayment" (
    "id" TEXT NOT NULL,
    "orderId" TEXT NOT NULL,
    "employeeId" TEXT NOT NULL,
    "receivedAmount" DECIMAL(10,2) NOT NULL,
    "paymentType" "obm_core"."PaymentType" NOT NULL,
    "bankName" TEXT,
    "chequeNumber" TEXT,
    "chequeDate" TIMESTAMP(3),
    "image" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "Order_orderCode_key" ON "obm_core"."Order"("orderCode");

-- AddForeignKey
ALTER TABLE "obm_core"."Order" ADD CONSTRAINT "Order_shopId_fkey" FOREIGN KEY ("shopId") REFERENCES "obm_core"."Shop"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."Order" ADD CONSTRAINT "Order_placedById_fkey" FOREIGN KEY ("placedById") REFERENCES "obm_core"."Staff"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."Order" ADD CONSTRAINT "Order_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "obm_core"."Company"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."OrderItem" ADD CONSTRAINT "OrderItem_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "obm_core"."Order"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."OrderItem" ADD CONSTRAINT "OrderItem_productId_fkey" FOREIGN KEY ("productId") REFERENCES "obm_core"."Product"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."OrderAssignment" ADD CONSTRAINT "OrderAssignment_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "obm_core"."Order"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."OrderAssignment" ADD CONSTRAINT "OrderAssignment_employeeId_fkey" FOREIGN KEY ("employeeId") REFERENCES "obm_core"."Staff"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."OrderPayment" ADD CONSTRAINT "OrderPayment_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "obm_core"."Order"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."OrderPayment" ADD CONSTRAINT "OrderPayment_employeeId_fkey" FOREIGN KEY ("employeeId") REFERENCES "obm_core"."Staff"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
