-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Ticket" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "numero" TEXT NOT NULL,
    "baieId" TEXT,
    "rangeeAllee" TEXT,
    "zoneCage" TEXT,
    "contactSurSite" TEXT,
    "demandeurId" TEXT NOT NULL,
    "technicienId" TEXT,
    "fournisseurId" TEXT,
    "priorite" TEXT NOT NULL DEFAULT 'standard',
    "typeIntervention" TEXT NOT NULL DEFAULT 'corrective',
    "statut" TEXT NOT NULL DEFAULT 'nouveau',
    "description" TEXT NOT NULL,
    "diagnosticMotif" TEXT,
    "actionsCorrectives" TEXT,
    "heureArrivee" DATETIME,
    "heureDepart" DATETIME,
    "dureeTotale" TEXT,
    "resultat" TEXT,
    "equipementRemisProd" BOOLEAN NOT NULL DEFAULT false,
    "interruptionService" BOOLEAN NOT NULL DEFAULT false,
    "dureeIndisponibilite" TEXT,
    "prochaineEcheance" DATETIME,
    "validationTechnicien" TEXT,
    "validationTechnicienDate" DATETIME,
    "validationClient" TEXT,
    "validationClientDate" DATETIME,
    "dateCreation" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "datePriseEnCompte" DATETIME,
    "dateIntervention" DATETIME,
    "dateCloture" DATETIME,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "Ticket_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Ticket_baieId_fkey" FOREIGN KEY ("baieId") REFERENCES "Baie" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "Ticket_demandeurId_fkey" FOREIGN KEY ("demandeurId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "Ticket_technicienId_fkey" FOREIGN KEY ("technicienId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "Ticket_fournisseurId_fkey" FOREIGN KEY ("fournisseurId") REFERENCES "Fournisseur" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Ticket" ("actionsCorrectives", "baieId", "contactSurSite", "dateCloture", "dateCreation", "dateIntervention", "datePriseEnCompte", "demandeurId", "description", "diagnosticMotif", "dureeIndisponibilite", "dureeTotale", "equipementRemisProd", "heureArrivee", "heureDepart", "id", "interruptionService", "numero", "priorite", "prochaineEcheance", "rangeeAllee", "resultat", "statut", "technicienId", "tenantId", "typeIntervention", "updatedAt", "validationClient", "validationClientDate", "validationTechnicien", "validationTechnicienDate", "zoneCage") SELECT "actionsCorrectives", "baieId", "contactSurSite", "dateCloture", "dateCreation", "dateIntervention", "datePriseEnCompte", "demandeurId", "description", "diagnosticMotif", "dureeIndisponibilite", "dureeTotale", "equipementRemisProd", "heureArrivee", "heureDepart", "id", "interruptionService", "numero", "priorite", "prochaineEcheance", "rangeeAllee", "resultat", "statut", "technicienId", "tenantId", "typeIntervention", "updatedAt", "validationClient", "validationClientDate", "validationTechnicien", "validationTechnicienDate", "zoneCage" FROM "Ticket";
DROP TABLE "Ticket";
ALTER TABLE "new_Ticket" RENAME TO "Ticket";
CREATE INDEX "Ticket_tenantId_idx" ON "Ticket"("tenantId");
CREATE INDEX "Ticket_tenantId_statut_idx" ON "Ticket"("tenantId", "statut");
CREATE UNIQUE INDEX "Ticket_tenantId_numero_key" ON "Ticket"("tenantId", "numero");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
