-- CreateTable
CREATE TABLE "TicketTechnicien" (
    "ticketId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,

    PRIMARY KEY ("ticketId", "userId"),
    CONSTRAINT "TicketTechnicien_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "TicketTechnicien_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE INDEX "TicketTechnicien_userId_idx" ON "TicketTechnicien"("userId");

-- CreateIndex
CREATE INDEX "TicketTechnicien_ticketId_idx" ON "TicketTechnicien"("ticketId");

-- Backfill : le technicien référent existant devient membre de l'équipe affectée.
INSERT INTO "TicketTechnicien" ("ticketId", "userId")
SELECT "id", "technicienId" FROM "Ticket" WHERE "technicienId" IS NOT NULL;
