-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Assembly" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectId" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "qty" INTEGER NOT NULL DEFAULT 1,
    "notes" TEXT,
    "stepFileId" TEXT,
    "drawingFileId" TEXT,
    "cutFileId" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "Assembly_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Assembly_stepFileId_fkey" FOREIGN KEY ("stepFileId") REFERENCES "FileAsset" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "Assembly_drawingFileId_fkey" FOREIGN KEY ("drawingFileId") REFERENCES "FileAsset" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "Assembly_cutFileId_fkey" FOREIGN KEY ("cutFileId") REFERENCES "FileAsset" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Assembly" ("code", "createdAt", "id", "name", "notes", "projectId", "qty", "updatedAt") SELECT "code", "createdAt", "id", "name", "notes", "projectId", "qty", "updatedAt" FROM "Assembly";
DROP TABLE "Assembly";
ALTER TABLE "new_Assembly" RENAME TO "Assembly";
CREATE UNIQUE INDEX "Assembly_projectId_code_key" ON "Assembly"("projectId", "code");
CREATE TABLE "new_Operation" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "partId" TEXT NOT NULL,
    "sequence" INTEGER NOT NULL,
    "templateId" TEXT,
    "name" TEXT NOT NULL,
    "machineId" TEXT,
    "settings" TEXT,
    "materialNotes" TEXT,
    "instructions" TEXT,
    "qcRequired" BOOLEAN NOT NULL DEFAULT false,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "qrToken" TEXT NOT NULL,
    "claimedByUserId" TEXT,
    "claimedAt" DATETIME,
    "completedAt" DATETIME,
    "plannedMinutes" INTEGER,
    "plannedUnits" INTEGER,
    "unitsCompleted" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "Operation_partId_fkey" FOREIGN KEY ("partId") REFERENCES "Part" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Operation_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "OperationTemplate" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "Operation_machineId_fkey" FOREIGN KEY ("machineId") REFERENCES "Machine" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "Operation_claimedByUserId_fkey" FOREIGN KEY ("claimedByUserId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Operation" ("claimedAt", "claimedByUserId", "completedAt", "createdAt", "id", "instructions", "machineId", "materialNotes", "name", "partId", "plannedMinutes", "plannedUnits", "qcRequired", "qrToken", "sequence", "settings", "status", "templateId", "updatedAt") SELECT "claimedAt", "claimedByUserId", "completedAt", "createdAt", "id", "instructions", "machineId", "materialNotes", "name", "partId", "plannedMinutes", "plannedUnits", "qcRequired", "qrToken", "sequence", "settings", "status", "templateId", "updatedAt" FROM "Operation";
DROP TABLE "Operation";
ALTER TABLE "new_Operation" RENAME TO "Operation";
CREATE UNIQUE INDEX "Operation_qrToken_key" ON "Operation"("qrToken");
CREATE INDEX "Operation_status_idx" ON "Operation"("status");
CREATE INDEX "Operation_claimedByUserId_idx" ON "Operation"("claimedByUserId");
CREATE UNIQUE INDEX "Operation_partId_sequence_key" ON "Operation"("partId", "sequence");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
