-- CreateEnum
CREATE TYPE "UserRole" AS ENUM ('ADMIN', 'MANAGER', 'STAFF', 'EMPLOYEE');

-- CreateEnum
CREATE TYPE "UserStatus" AS ENUM ('ACTIVE', 'INACTIVE');

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

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

-- CreateTable
CREATE TABLE "User" (
    "id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "passwordHash" TEXT NOT NULL,
    "role" "UserRole" NOT NULL DEFAULT 'EMPLOYEE',
    "status" "UserStatus" NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HolderMaster" (
    "id" TEXT NOT NULL,
    "sourceId" INTEGER,
    "holderInchValue" DOUBLE PRECISION NOT NULL DEFAULT 1,
    "holderInch" TEXT NOT NULL,
    "holderName" TEXT NOT NULL,
    "holderCode" TEXT NOT NULL,
    "bodyName" TEXT NOT NULL,
    "bodyCode" TEXT NOT NULL,
    "yorkeName" TEXT,
    "yorkeCode" TEXT,
    "yorkeLoad" DOUBLE PRECISION,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "PlateMaster" (
    "id" TEXT NOT NULL,
    "sourceId" INTEGER,
    "holderCode" TEXT NOT NULL,
    "plateName" TEXT NOT NULL,
    "brakeName" TEXT NOT NULL,
    "plateCode" TEXT NOT NULL,
    "brakeType" TEXT NOT NULL,
    "brakeTypeCode" TEXT NOT NULL,
    "loadCarryingType" TEXT NOT NULL,
    "loadCarryingCode" TEXT NOT NULL,
    "mountingType" TEXT NOT NULL,
    "mountingCode" TEXT NOT NULL,
    "fittingSize" TEXT NOT NULL,
    "fittingSizeCode" TEXT NOT NULL,
    "bearingType" TEXT NOT NULL,
    "bearingCode" TEXT NOT NULL,
    "fittingType" TEXT NOT NULL,
    "fittingCode" TEXT NOT NULL,
    "yokeName" TEXT,
    "yokeCode" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "WheelMaster" (
    "id" TEXT NOT NULL,
    "sourceId" INTEGER,
    "wheelInchValue" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "wheelName" TEXT NOT NULL,
    "wheelCode" TEXT NOT NULL,
    "wheelSize" TEXT NOT NULL,
    "wheelSizeCode" TEXT NOT NULL,
    "wheelType" TEXT NOT NULL,
    "wheelTypeCode" TEXT NOT NULL,
    "bearingType" TEXT NOT NULL,
    "bearingCode" TEXT NOT NULL,
    "dustCoverName" TEXT NOT NULL,
    "dustCoverCode" TEXT NOT NULL,
    "wheelColor" TEXT NOT NULL,
    "wheelColorCode" TEXT NOT NULL,
    "dynamicLoadCarryCapacity" TEXT NOT NULL,
    "dynamicLoadCarryCapacityCode" TEXT NOT NULL,
    "wheelTypeSubName" TEXT NOT NULL,
    "wheelTypeSubNameCode" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SetCreator" (
    "id" TEXT NOT NULL,
    "setNumber" INTEGER NOT NULL,
    "productInch" DOUBLE PRECISION NOT NULL,
    "holderCode" TEXT NOT NULL,
    "holderName" TEXT NOT NULL,
    "plateCount" INTEGER NOT NULL DEFAULT 0,
    "finishCount" INTEGER NOT NULL DEFAULT 0,
    "wheelCount" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SetCreatorPlate" (
    "id" TEXT NOT NULL,
    "setId" TEXT NOT NULL,
    "plateCode" TEXT NOT NULL,
    "plateName" TEXT NOT NULL,
    "brakeName" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "SetCreatorFinish" (
    "id" TEXT NOT NULL,
    "setId" TEXT NOT NULL,
    "finishType" TEXT NOT NULL,
    "finishLabel" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "SetCreatorWheel" (
    "id" TEXT NOT NULL,
    "setId" TEXT NOT NULL,
    "wheelCode" TEXT NOT NULL,
    "wheelName" TEXT NOT NULL,
    "wheelType" TEXT NOT NULL,
    "bearingType" TEXT NOT NULL,
    "wheelInchValue" DOUBLE PRECISION NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "SetProduct" (
    "id" TEXT NOT NULL,
    "setId" TEXT NOT NULL,
    "holderCode" TEXT NOT NULL,
    "holderName" TEXT NOT NULL,
    "plateCode" TEXT NOT NULL,
    "plateName" TEXT NOT NULL,
    "brakeName" TEXT NOT NULL,
    "finishType" TEXT NOT NULL,
    "finishLabel" TEXT NOT NULL,
    "wheelCode" TEXT NOT NULL,
    "wheelName" TEXT NOT NULL,
    "productName" TEXT NOT NULL,
    "productCode" TEXT NOT NULL,
    "productInch" DOUBLE PRECISION NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "CodeGeneratorEmailSetting" (
    "key" TEXT NOT NULL DEFAULT 'code_generator_delete',
    "recipientEmail" TEXT NOT NULL DEFAULT 'cmt@starline.in',
    "ccEmail" TEXT DEFAULT 'accounts@starlinecastors.com',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "CodeGeneratorEmailSetting_pkey" PRIMARY KEY ("key")
);

-- CreateTable
CREATE TABLE "CodeGeneratorDeleteOtp" (
    "id" TEXT NOT NULL,
    "startCode" TEXT NOT NULL,
    "endCode" TEXT NOT NULL,
    "productIds" TEXT[],
    "productCount" INTEGER NOT NULL,
    "recipientEmail" TEXT NOT NULL,
    "ccEmail" TEXT,
    "otpHash" TEXT NOT NULL,
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "usedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "RawMaterialCategory" (
    "id" TEXT NOT NULL,
    "categoryCode" TEXT,
    "categoryName" TEXT NOT NULL,
    "description" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "UnitOfMeasure" (
    "id" TEXT NOT NULL,
    "uomName" TEXT NOT NULL,
    "uomCode" TEXT NOT NULL,
    "measureType" TEXT NOT NULL,
    "conversionFactor" DOUBLE PRECISION NOT NULL DEFAULT 1,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "RawMaterialVendor" (
    "id" TEXT NOT NULL,
    "vendorCode" TEXT NOT NULL,
    "vendorName" TEXT NOT NULL,
    "address" TEXT,
    "phoneNumber" TEXT,
    "whatsappNumber" TEXT,
    "email" TEXT,
    "gstNumber" TEXT,
    "mapLocationUrl" TEXT,
    "locationPlace" TEXT,
    "locationPincode" TEXT,
    "latitude" DOUBLE PRECISION,
    "longitude" DOUBLE PRECISION,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "RawMaterial" (
    "id" TEXT NOT NULL,
    "materialCode" TEXT NOT NULL,
    "materialName" TEXT NOT NULL,
    "categoryId" TEXT,
    "subType" TEXT,
    "description" TEXT,
    "uomId" TEXT,
    "hsnCode" TEXT,
    "taxCategory" TEXT,
    "gstPercent" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "vendorId" TEXT,
    "costPerUnit" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "currency" TEXT NOT NULL DEFAULT 'INR',
    "batchNumber" TEXT,
    "qualityGrade" TEXT,
    "specificationStandard" TEXT,
    "storageConditions" TEXT,
    "currentStock" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "reorderLevel" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "reorderQuantity" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "stockType" TEXT NOT NULL DEFAULT 'Central Store',
    "lossWastePercent" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "accountLevelLink" TEXT,
    "imagePath" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "RawMaterialPriceHistory" (
    "id" TEXT NOT NULL,
    "rawMaterialId" TEXT NOT NULL,
    "vendorId" TEXT,
    "oldPrice" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "newPrice" DOUBLE PRECISION NOT NULL,
    "currency" TEXT NOT NULL DEFAULT 'INR',
    "effectiveDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedBy" TEXT,
    "remarks" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "PurchaseOrder" (
    "id" TEXT NOT NULL,
    "poNumber" TEXT NOT NULL,
    "vendorId" TEXT,
    "orderDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expectedDate" TIMESTAMP(3),
    "status" TEXT NOT NULL DEFAULT 'Draft',
    "subtotal" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "gstTotal" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "transportationCost" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "totalAmount" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "transportDetails" TEXT,
    "epassRequired" BOOLEAN NOT NULL DEFAULT false,
    "epassFilePath" TEXT,
    "notes" TEXT,
    "generatedAt" TIMESTAMP(3),
    "emailSubject" TEXT,
    "emailTo" TEXT,
    "emailCc" TEXT,
    "emailMessage" TEXT,
    "emailSentAt" TIMESTAMP(3),
    "stockConvertedAt" TIMESTAMP(3),
    "stockConvertedBy" TEXT,
    "createdBy" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "PurchaseOrderItem" (
    "id" TEXT NOT NULL,
    "purchaseOrderId" TEXT NOT NULL,
    "rawMaterialId" TEXT,
    "uomId" TEXT,
    "description" TEXT,
    "quantity" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "rate" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "gstPercent" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "lineSubtotal" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "lineGst" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "lineTotal" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "RawMaterialSettings" (
    "key" TEXT NOT NULL DEFAULT 'raw_material',
    "smtpHost" TEXT,
    "smtpPort" INTEGER NOT NULL DEFAULT 465,
    "smtpSecure" BOOLEAN NOT NULL DEFAULT true,
    "smtpUsername" TEXT,
    "smtpPassword" TEXT,
    "defaultSenderEmail" TEXT,
    "defaultSenderName" TEXT DEFAULT 'Starline Purchase',
    "defaultCc" TEXT,
    "billHeader" TEXT,
    "billFooter" TEXT,
    "logoPath" TEXT,
    "processCodeAutoGenerate" BOOLEAN NOT NULL DEFAULT true,
    "processCodePrefix" TEXT NOT NULL DEFAULT 'PRC',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "RawMaterialSettings_pkey" PRIMARY KEY ("key")
);

-- CreateTable
CREATE TABLE "Machinery" (
    "id" TEXT NOT NULL,
    "moduleType" TEXT NOT NULL DEFAULT 'General',
    "machineryName" TEXT NOT NULL,
    "machineryNumber" TEXT NOT NULL,
    "eachHp" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "hourRate" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "name" TEXT,
    "code" TEXT,
    "tonnage" DOUBLE PRECISION,
    "noOfPlatens" INTEGER,
    "platenSize" TEXT,
    "powerPackHp" DOUBLE PRECISION,
    "heaterCapacity" DOUBLE PRECISION,
    "heaterPerPlaten" DOUBLE PRECISION,
    "noOfPlates" INTEGER,
    "coolingTower" DOUBLE PRECISION,
    "powerPack" DOUBLE PRECISION,
    "heaterPack" DOUBLE PRECISION,
    "coolingTowerCost" DOUBLE PRECISION,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "CostingProcess" (
    "id" TEXT NOT NULL,
    "processCode" TEXT NOT NULL,
    "processName" TEXT NOT NULL,
    "processType" TEXT NOT NULL,
    "inch" TEXT,
    "selectedItems" JSONB NOT NULL DEFAULT '[]',
    "materialInput" JSONB NOT NULL DEFAULT '{}',
    "machineInput" JSONB NOT NULL DEFAULT '{}',
    "coatingInput" JSONB NOT NULL DEFAULT '{}',
    "materialCost" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "machineCost" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "coatingCost" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "totalCost" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdBy" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SfgItem" (
    "id" TEXT NOT NULL,
    "itemCode" TEXT NOT NULL,
    "itemName" TEXT NOT NULL,
    "itemGroup" TEXT,
    "sfgType" TEXT NOT NULL DEFAULT 'HOLDER',
    "purchaseUom" TEXT,
    "stockUom" TEXT,
    "conversionRatio" DOUBLE PRECISION NOT NULL DEFAULT 1,
    "billingItem" BOOLEAN NOT NULL DEFAULT false,
    "bom" BOOLEAN NOT NULL DEFAULT false,
    "stockMaintain" BOOLEAN NOT NULL DEFAULT false,
    "minStockLevel" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "maxStockLevel" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "rate" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "costPrice" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "inch" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "sourceData" JSONB NOT NULL DEFAULT '{}',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BomHeader" (
    "id" TEXT NOT NULL,
    "bomType" TEXT NOT NULL,
    "parentCode" TEXT NOT NULL,
    "parentName" TEXT NOT NULL,
    "parentItemType" TEXT NOT NULL,
    "parentSfgItemId" TEXT,
    "parentSetProductId" TEXT,
    "sourceSheet" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BomItem" (
    "id" TEXT NOT NULL,
    "bomId" TEXT NOT NULL,
    "childCode" TEXT NOT NULL,
    "childName" TEXT NOT NULL,
    "childItemType" TEXT NOT NULL,
    "childSfgItemId" TEXT,
    "rawMaterialId" TEXT,
    "quantity" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "newQuantity" DOUBLE PRECISION,
    "uom" TEXT,
    "wastagePercent" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "costPerUnit" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "totalMaterialCost" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "sourceSheet" TEXT,
    "sourceData" JSONB NOT NULL DEFAULT '{}',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SfgItemProcess" (
    "id" TEXT NOT NULL,
    "processCode" TEXT NOT NULL,
    "sfgItemId" TEXT,
    "itemCode" TEXT NOT NULL,
    "itemName" TEXT NOT NULL,
    "processName" TEXT NOT NULL,
    "processType" TEXT NOT NULL,
    "inch" TEXT,
    "setupTime" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "processTime" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "inspectionTime" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "inspectionRequired" BOOLEAN NOT NULL DEFAULT false,
    "toolsDie" TEXT,
    "machineryId" TEXT,
    "machineName" TEXT,
    "description" TEXT,
    "reqMaterialCode" TEXT,
    "reqMaterialQty" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "hourRate" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "machineCost" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "costingProcessId" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "sourceData" JSONB NOT NULL DEFAULT '{}',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "Company_name_key" ON "Company"("name");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "HolderMaster_holderCode_key" ON "HolderMaster"("holderCode");

-- CreateIndex
CREATE INDEX "HolderMaster_holderInch_idx" ON "HolderMaster"("holderInch");

-- CreateIndex
CREATE INDEX "HolderMaster_holderInchValue_idx" ON "HolderMaster"("holderInchValue");

-- CreateIndex
CREATE INDEX "HolderMaster_bodyCode_idx" ON "HolderMaster"("bodyCode");

-- CreateIndex
CREATE INDEX "HolderMaster_status_idx" ON "HolderMaster"("status");

-- CreateIndex
CREATE INDEX "HolderMaster_holderName_idx" ON "HolderMaster"("holderName");

-- CreateIndex
CREATE UNIQUE INDEX "PlateMaster_plateCode_key" ON "PlateMaster"("plateCode");

-- CreateIndex
CREATE INDEX "PlateMaster_holderCode_idx" ON "PlateMaster"("holderCode");

-- CreateIndex
CREATE INDEX "PlateMaster_status_idx" ON "PlateMaster"("status");

-- CreateIndex
CREATE UNIQUE INDEX "WheelMaster_wheelCode_key" ON "WheelMaster"("wheelCode");

-- CreateIndex
CREATE INDEX "WheelMaster_wheelCode_idx" ON "WheelMaster"("wheelCode");

-- CreateIndex
CREATE INDEX "WheelMaster_wheelInchValue_idx" ON "WheelMaster"("wheelInchValue");

-- CreateIndex
CREATE INDEX "WheelMaster_status_idx" ON "WheelMaster"("status");

-- CreateIndex
CREATE UNIQUE INDEX "SetCreator_setNumber_key" ON "SetCreator"("setNumber");

-- CreateIndex
CREATE INDEX "SetCreator_productInch_idx" ON "SetCreator"("productInch");

-- CreateIndex
CREATE INDEX "SetCreator_holderCode_idx" ON "SetCreator"("holderCode");

-- CreateIndex
CREATE UNIQUE INDEX "SetCreatorPlate_setId_plateCode_key" ON "SetCreatorPlate"("setId", "plateCode");

-- CreateIndex
CREATE UNIQUE INDEX "SetCreatorFinish_setId_finishType_key" ON "SetCreatorFinish"("setId", "finishType");

-- CreateIndex
CREATE UNIQUE INDEX "SetCreatorWheel_setId_wheelCode_key" ON "SetCreatorWheel"("setId", "wheelCode");

-- CreateIndex
CREATE UNIQUE INDEX "SetProduct_productCode_key" ON "SetProduct"("productCode");

-- CreateIndex
CREATE INDEX "SetProduct_setId_idx" ON "SetProduct"("setId");

-- CreateIndex
CREATE INDEX "SetProduct_productInch_idx" ON "SetProduct"("productInch");

-- CreateIndex
CREATE UNIQUE INDEX "SetProduct_holderCode_plateCode_finishType_wheelCode_key" ON "SetProduct"("holderCode", "plateCode", "finishType", "wheelCode");

-- CreateIndex
CREATE INDEX "CodeGeneratorDeleteOtp_expiresAt_idx" ON "CodeGeneratorDeleteOtp"("expiresAt");

-- CreateIndex
CREATE INDEX "CodeGeneratorDeleteOtp_usedAt_idx" ON "CodeGeneratorDeleteOtp"("usedAt");

-- CreateIndex
CREATE UNIQUE INDEX "RawMaterialCategory_categoryCode_key" ON "RawMaterialCategory"("categoryCode");

-- CreateIndex
CREATE UNIQUE INDEX "RawMaterialCategory_categoryName_key" ON "RawMaterialCategory"("categoryName");

-- CreateIndex
CREATE INDEX "RawMaterialCategory_status_idx" ON "RawMaterialCategory"("status");

-- CreateIndex
CREATE UNIQUE INDEX "UnitOfMeasure_uomName_key" ON "UnitOfMeasure"("uomName");

-- CreateIndex
CREATE UNIQUE INDEX "UnitOfMeasure_uomCode_key" ON "UnitOfMeasure"("uomCode");

-- CreateIndex
CREATE INDEX "UnitOfMeasure_measureType_idx" ON "UnitOfMeasure"("measureType");

-- CreateIndex
CREATE INDEX "UnitOfMeasure_status_idx" ON "UnitOfMeasure"("status");

-- CreateIndex
CREATE UNIQUE INDEX "RawMaterialVendor_vendorCode_key" ON "RawMaterialVendor"("vendorCode");

-- CreateIndex
CREATE INDEX "RawMaterialVendor_vendorName_idx" ON "RawMaterialVendor"("vendorName");

-- CreateIndex
CREATE INDEX "RawMaterialVendor_status_idx" ON "RawMaterialVendor"("status");

-- CreateIndex
CREATE UNIQUE INDEX "RawMaterial_materialCode_key" ON "RawMaterial"("materialCode");

-- CreateIndex
CREATE INDEX "RawMaterial_categoryId_idx" ON "RawMaterial"("categoryId");

-- CreateIndex
CREATE INDEX "RawMaterial_uomId_idx" ON "RawMaterial"("uomId");

-- CreateIndex
CREATE INDEX "RawMaterial_vendorId_idx" ON "RawMaterial"("vendorId");

-- CreateIndex
CREATE INDEX "RawMaterial_stockType_idx" ON "RawMaterial"("stockType");

-- CreateIndex
CREATE INDEX "RawMaterial_status_idx" ON "RawMaterial"("status");

-- CreateIndex
CREATE INDEX "RawMaterialPriceHistory_rawMaterialId_idx" ON "RawMaterialPriceHistory"("rawMaterialId");

-- CreateIndex
CREATE INDEX "RawMaterialPriceHistory_vendorId_idx" ON "RawMaterialPriceHistory"("vendorId");

-- CreateIndex
CREATE INDEX "RawMaterialPriceHistory_effectiveDate_idx" ON "RawMaterialPriceHistory"("effectiveDate");

-- CreateIndex
CREATE UNIQUE INDEX "PurchaseOrder_poNumber_key" ON "PurchaseOrder"("poNumber");

-- CreateIndex
CREATE INDEX "PurchaseOrder_vendorId_idx" ON "PurchaseOrder"("vendorId");

-- CreateIndex
CREATE INDEX "PurchaseOrder_orderDate_idx" ON "PurchaseOrder"("orderDate");

-- CreateIndex
CREATE INDEX "PurchaseOrder_status_idx" ON "PurchaseOrder"("status");

-- CreateIndex
CREATE INDEX "PurchaseOrder_stockConvertedAt_idx" ON "PurchaseOrder"("stockConvertedAt");

-- CreateIndex
CREATE INDEX "PurchaseOrderItem_purchaseOrderId_idx" ON "PurchaseOrderItem"("purchaseOrderId");

-- CreateIndex
CREATE INDEX "PurchaseOrderItem_rawMaterialId_idx" ON "PurchaseOrderItem"("rawMaterialId");

-- CreateIndex
CREATE INDEX "PurchaseOrderItem_uomId_idx" ON "PurchaseOrderItem"("uomId");

-- CreateIndex
CREATE UNIQUE INDEX "Machinery_machineryNumber_key" ON "Machinery"("machineryNumber");

-- CreateIndex
CREATE UNIQUE INDEX "Machinery_code_key" ON "Machinery"("code");

-- CreateIndex
CREATE INDEX "Machinery_moduleType_idx" ON "Machinery"("moduleType");

-- CreateIndex
CREATE INDEX "Machinery_machineryName_idx" ON "Machinery"("machineryName");

-- CreateIndex
CREATE INDEX "Machinery_status_idx" ON "Machinery"("status");

-- CreateIndex
CREATE UNIQUE INDEX "CostingProcess_processCode_key" ON "CostingProcess"("processCode");

-- CreateIndex
CREATE INDEX "CostingProcess_processType_idx" ON "CostingProcess"("processType");

-- CreateIndex
CREATE INDEX "CostingProcess_inch_idx" ON "CostingProcess"("inch");

-- CreateIndex
CREATE INDEX "CostingProcess_status_idx" ON "CostingProcess"("status");

-- CreateIndex
CREATE UNIQUE INDEX "SfgItem_itemCode_key" ON "SfgItem"("itemCode");

-- CreateIndex
CREATE INDEX "SfgItem_sfgType_idx" ON "SfgItem"("sfgType");

-- CreateIndex
CREATE INDEX "SfgItem_itemGroup_idx" ON "SfgItem"("itemGroup");

-- CreateIndex
CREATE INDEX "SfgItem_inch_idx" ON "SfgItem"("inch");

-- CreateIndex
CREATE INDEX "SfgItem_status_idx" ON "SfgItem"("status");

-- CreateIndex
CREATE INDEX "BomHeader_bomType_idx" ON "BomHeader"("bomType");

-- CreateIndex
CREATE INDEX "BomHeader_parentItemType_idx" ON "BomHeader"("parentItemType");

-- CreateIndex
CREATE INDEX "BomHeader_parentSfgItemId_idx" ON "BomHeader"("parentSfgItemId");

-- CreateIndex
CREATE INDEX "BomHeader_parentSetProductId_idx" ON "BomHeader"("parentSetProductId");

-- CreateIndex
CREATE INDEX "BomHeader_status_idx" ON "BomHeader"("status");

-- CreateIndex
CREATE UNIQUE INDEX "BomHeader_bomType_parentCode_key" ON "BomHeader"("bomType", "parentCode");

-- CreateIndex
CREATE INDEX "BomItem_childCode_idx" ON "BomItem"("childCode");

-- CreateIndex
CREATE INDEX "BomItem_childItemType_idx" ON "BomItem"("childItemType");

-- CreateIndex
CREATE INDEX "BomItem_childSfgItemId_idx" ON "BomItem"("childSfgItemId");

-- CreateIndex
CREATE INDEX "BomItem_rawMaterialId_idx" ON "BomItem"("rawMaterialId");

-- CreateIndex
CREATE UNIQUE INDEX "BomItem_bomId_childCode_key" ON "BomItem"("bomId", "childCode");

-- CreateIndex
CREATE UNIQUE INDEX "SfgItemProcess_processCode_key" ON "SfgItemProcess"("processCode");

-- CreateIndex
CREATE INDEX "SfgItemProcess_sfgItemId_idx" ON "SfgItemProcess"("sfgItemId");

-- CreateIndex
CREATE INDEX "SfgItemProcess_itemCode_idx" ON "SfgItemProcess"("itemCode");

-- CreateIndex
CREATE INDEX "SfgItemProcess_processType_idx" ON "SfgItemProcess"("processType");

-- CreateIndex
CREATE INDEX "SfgItemProcess_inch_idx" ON "SfgItemProcess"("inch");

-- CreateIndex
CREATE INDEX "SfgItemProcess_machineryId_idx" ON "SfgItemProcess"("machineryId");

-- CreateIndex
CREATE INDEX "SfgItemProcess_status_idx" ON "SfgItemProcess"("status");

-- AddForeignKey
ALTER TABLE "SetCreatorPlate" ADD CONSTRAINT "SetCreatorPlate_setId_fkey" FOREIGN KEY ("setId") REFERENCES "SetCreator"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SetCreatorFinish" ADD CONSTRAINT "SetCreatorFinish_setId_fkey" FOREIGN KEY ("setId") REFERENCES "SetCreator"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SetCreatorWheel" ADD CONSTRAINT "SetCreatorWheel_setId_fkey" FOREIGN KEY ("setId") REFERENCES "SetCreator"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SetProduct" ADD CONSTRAINT "SetProduct_setId_fkey" FOREIGN KEY ("setId") REFERENCES "SetCreator"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RawMaterial" ADD CONSTRAINT "RawMaterial_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "RawMaterialCategory"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RawMaterial" ADD CONSTRAINT "RawMaterial_uomId_fkey" FOREIGN KEY ("uomId") REFERENCES "UnitOfMeasure"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RawMaterial" ADD CONSTRAINT "RawMaterial_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "RawMaterialVendor"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RawMaterialPriceHistory" ADD CONSTRAINT "RawMaterialPriceHistory_rawMaterialId_fkey" FOREIGN KEY ("rawMaterialId") REFERENCES "RawMaterial"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RawMaterialPriceHistory" ADD CONSTRAINT "RawMaterialPriceHistory_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "RawMaterialVendor"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PurchaseOrder" ADD CONSTRAINT "PurchaseOrder_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "RawMaterialVendor"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PurchaseOrderItem" ADD CONSTRAINT "PurchaseOrderItem_purchaseOrderId_fkey" FOREIGN KEY ("purchaseOrderId") REFERENCES "PurchaseOrder"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PurchaseOrderItem" ADD CONSTRAINT "PurchaseOrderItem_rawMaterialId_fkey" FOREIGN KEY ("rawMaterialId") REFERENCES "RawMaterial"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PurchaseOrderItem" ADD CONSTRAINT "PurchaseOrderItem_uomId_fkey" FOREIGN KEY ("uomId") REFERENCES "UnitOfMeasure"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BomItem" ADD CONSTRAINT "BomItem_bomId_fkey" FOREIGN KEY ("bomId") REFERENCES "BomHeader"("id") ON DELETE CASCADE ON UPDATE CASCADE;
