-- CreateTable
CREATE TABLE "SfgCostingRecord" (
    "id" TEXT NOT NULL,
    "sfgItemId" TEXT,
    "itemCode" TEXT,
    "itemName" TEXT NOT NULL,
    "lookupKey" TEXT NOT NULL,
    "sfgType" TEXT NOT NULL,
    "inch" TEXT,
    "finishVariant" TEXT NOT NULL DEFAULT 'DEFAULT',
    "sourceKind" TEXT NOT NULL,
    "plateCodes" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "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,
    "sourceFile" TEXT,
    "sourceData" JSONB NOT NULL DEFAULT '{}',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SfgCostingDetail" (
    "id" TEXT NOT NULL,
    "costingRecordId" TEXT NOT NULL,
    "lineType" TEXT NOT NULL,
    "sequence" INTEGER NOT NULL DEFAULT 0,
    "lineCode" TEXT,
    "lineName" TEXT NOT NULL,
    "machineName" TEXT,
    "quantity" DOUBLE PRECISION,
    "unitRate" DOUBLE PRECISION,
    "length" DOUBLE PRECISION,
    "width" DOUBLE PRECISION,
    "thickness" DOUBLE PRECISION,
    "weightGram" DOUBLE PRECISION,
    "cavityCount" DOUBLE PRECISION,
    "noOfSetsPerSheet" DOUBLE PRECISION,
    "noOfPcsPerStrip" DOUBLE PRECISION,
    "oneSheetWeight" DOUBLE PRECISION,
    "stdPrice" DOUBLE PRECISION,
    "oneSheetRate" DOUBLE PRECISION,
    "piecesPerKgOrMeter" DOUBLE PRECISION,
    "runningCostPerHour" DOUBLE PRECISION,
    "labourCostPerHour" DOUBLE PRECISION,
    "productionPerHour" DOUBLE PRECISION,
    "productionCostPerPiece" DOUBLE PRECISION,
    "oneStripCuttingCharge" DOUBLE PRECISION,
    "onePieceCuttingCharge" DOUBLE PRECISION,
    "galvanizingPerKg" DOUBLE PRECISION,
    "galvanizingPerPiece" DOUBLE PRECISION,
    "totalCost" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "sourceData" JSONB NOT NULL DEFAULT '{}',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "SfgCostingRecord_sfgItemId_idx" ON "SfgCostingRecord"("sfgItemId");

-- CreateIndex
CREATE INDEX "SfgCostingRecord_itemCode_idx" ON "SfgCostingRecord"("itemCode");

-- CreateIndex
CREATE INDEX "SfgCostingRecord_lookupKey_idx" ON "SfgCostingRecord"("lookupKey");

-- CreateIndex
CREATE INDEX "SfgCostingRecord_finishVariant_idx" ON "SfgCostingRecord"("finishVariant");

-- CreateIndex
CREATE INDEX "SfgCostingRecord_sourceKind_idx" ON "SfgCostingRecord"("sourceKind");

-- CreateIndex
CREATE INDEX "SfgCostingRecord_sfgType_idx" ON "SfgCostingRecord"("sfgType");

-- CreateIndex
CREATE INDEX "SfgCostingRecord_inch_idx" ON "SfgCostingRecord"("inch");

-- CreateIndex
CREATE INDEX "SfgCostingDetail_costingRecordId_idx" ON "SfgCostingDetail"("costingRecordId");

-- CreateIndex
CREATE INDEX "SfgCostingDetail_lineType_idx" ON "SfgCostingDetail"("lineType");

-- AddForeignKey
ALTER TABLE "SfgCostingRecord" ADD CONSTRAINT "SfgCostingRecord_sfgItemId_fkey" FOREIGN KEY ("sfgItemId") REFERENCES "SfgItem"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SfgCostingDetail" ADD CONSTRAINT "SfgCostingDetail_costingRecordId_fkey" FOREIGN KEY ("costingRecordId") REFERENCES "SfgCostingRecord"("id") ON DELETE CASCADE ON UPDATE CASCADE;
