-- CreateTable
CREATE TABLE "Tenant" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "code" TEXT NOT NULL,
    "nom" TEXT NOT NULL,
    "statut" TEXT NOT NULL DEFAULT 'actif',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "User" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "email" TEXT NOT NULL,
    "passwordHash" TEXT NOT NULL,
    "nom" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "actif" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "UserTenant" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "tenantId" TEXT NOT NULL,
    CONSTRAINT "UserTenant_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "UserTenant_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Site" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "nom" TEXT NOT NULL,
    CONSTRAINT "Site_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Salle" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "siteId" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "nom" TEXT NOT NULL,
    CONSTRAINT "Salle_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Salle_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Baie" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "salleId" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "rangeeAllee" TEXT,
    "zoneCage" TEXT,
    "hauteurU" INTEGER,
    CONSTRAINT "Baie_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Baie_salleId_fkey" FOREIGN KEY ("salleId") REFERENCES "Salle" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Famille" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "code" TEXT NOT NULL,
    "ordre" INTEGER NOT NULL,
    "nom" TEXT NOT NULL,
    "valeurClient" TEXT,
    "valeurHebergeur" TEXT
);

-- CreateTable
CREATE TABLE "PrestationCatalogue" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "ref" TEXT NOT NULL,
    "familleId" TEXT NOT NULL,
    "libelle" TEXT NOT NULL,
    "contenu" TEXT NOT NULL,
    "uniteOeuvre" TEXT NOT NULL,
    "uniteOeuvreLabel" TEXT NOT NULL,
    "seuilTolerance" TEXT,
    "norme" TEXT,
    "tarifTexte" TEXT,
    "tarifMin" REAL,
    "tarifMax" REAL,
    "livrableType" TEXT,
    "livrableRequis" BOOLEAN NOT NULL DEFAULT false,
    "actif" BOOLEAN NOT NULL DEFAULT true,
    CONSTRAINT "PrestationCatalogue_familleId_fkey" FOREIGN KEY ("familleId") REFERENCES "Famille" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "PrestationTenant" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "prestationId" TEXT NOT NULL,
    "tenantId" TEXT NOT NULL,
    "actif" BOOLEAN NOT NULL DEFAULT true,
    CONSTRAINT "PrestationTenant_prestationId_fkey" FOREIGN KEY ("prestationId") REFERENCES "PrestationCatalogue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "PrestationTenant_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Equipement" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "baieId" TEXT,
    "nature" TEXT NOT NULL,
    "type" TEXT,
    "marqueModele" TEXT,
    "numeroSerie" TEXT,
    "hostnameAsset" TEXT,
    "ipIloIdrac" TEXT,
    "environnement" TEXT,
    "criticite" TEXT,
    "sousContratGarantie" TEXT,
    "positionU" TEXT,
    CONSTRAINT "Equipement_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Equipement_baieId_fkey" FOREIGN KEY ("baieId") REFERENCES "Baie" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "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,
    "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
);

-- CreateTable
CREATE TABLE "TicketEquipement" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "ticketId" TEXT NOT NULL,
    "equipementId" TEXT NOT NULL,
    CONSTRAINT "TicketEquipement_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "TicketEquipement_equipementId_fkey" FOREIGN KEY ("equipementId") REFERENCES "Equipement" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "LignePrestation" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "ticketId" TEXT NOT NULL,
    "prestationId" TEXT NOT NULL,
    "quantite" REAL NOT NULL DEFAULT 1,
    "statut" TEXT NOT NULL DEFAULT 'a_faire',
    "observations" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "LignePrestation_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "LignePrestation_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "LignePrestation_prestationId_fkey" FOREIGN KEY ("prestationId") REFERENCES "PrestationCatalogue" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "PieceRemplacee" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "ticketId" TEXT NOT NULL,
    "composant" TEXT NOT NULL,
    "serieRetiree" TEXT,
    "serieInstallee" TEXT,
    CONSTRAINT "PieceRemplacee_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "PieceRemplacee_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ModeleChecklist" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "code" TEXT NOT NULL,
    "nom" TEXT NOT NULL,
    "description" TEXT,
    "actif" BOOLEAN NOT NULL DEFAULT true
);

-- CreateTable
CREATE TABLE "PointControleModele" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "modeleId" TEXT NOT NULL,
    "ordre" INTEGER NOT NULL,
    "libelle" TEXT NOT NULL,
    CONSTRAINT "PointControleModele_modeleId_fkey" FOREIGN KEY ("modeleId") REFERENCES "ModeleChecklist" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ChecklistIntervention" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "ticketId" TEXT NOT NULL,
    "modeleId" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "ChecklistIntervention_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "ChecklistIntervention_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "ChecklistIntervention_modeleId_fkey" FOREIGN KEY ("modeleId") REFERENCES "ModeleChecklist" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "PointControle" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "checklistId" TEXT NOT NULL,
    "ordre" INTEGER NOT NULL,
    "libelle" TEXT NOT NULL,
    "statut" TEXT NOT NULL DEFAULT 'non_verifie',
    "observations" TEXT,
    CONSTRAINT "PointControle_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "PointControle_checklistId_fkey" FOREIGN KEY ("checklistId") REFERENCES "ChecklistIntervention" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Livrable" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "ticketId" TEXT,
    "lignePrestationId" TEXT,
    "type" TEXT NOT NULL,
    "nomFichier" TEXT NOT NULL,
    "cheminStockage" TEXT NOT NULL,
    "taille" INTEGER,
    "mime" TEXT,
    "auteurId" TEXT,
    "horodatage" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "Livrable_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Livrable_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Livrable_lignePrestationId_fkey" FOREIGN KEY ("lignePrestationId") REFERENCES "LignePrestation" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "Livrable_auteurId_fkey" FOREIGN KEY ("auteurId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "JournalEvenement" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "entite" TEXT NOT NULL,
    "entiteId" TEXT NOT NULL,
    "typeAction" TEXT NOT NULL,
    "auteurId" TEXT,
    "detail" TEXT,
    "horodatage" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "JournalEvenement_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "JournalEvenement_auteurId_fkey" FOREIGN KEY ("auteurId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "Tenant_code_key" ON "Tenant"("code");

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

-- CreateIndex
CREATE INDEX "UserTenant_tenantId_idx" ON "UserTenant"("tenantId");

-- CreateIndex
CREATE UNIQUE INDEX "UserTenant_userId_tenantId_key" ON "UserTenant"("userId", "tenantId");

-- CreateIndex
CREATE INDEX "Site_tenantId_idx" ON "Site"("tenantId");

-- CreateIndex
CREATE UNIQUE INDEX "Site_tenantId_code_key" ON "Site"("tenantId", "code");

-- CreateIndex
CREATE INDEX "Salle_tenantId_idx" ON "Salle"("tenantId");

-- CreateIndex
CREATE UNIQUE INDEX "Salle_tenantId_siteId_code_key" ON "Salle"("tenantId", "siteId", "code");

-- CreateIndex
CREATE INDEX "Baie_tenantId_idx" ON "Baie"("tenantId");

-- CreateIndex
CREATE UNIQUE INDEX "Baie_tenantId_salleId_code_key" ON "Baie"("tenantId", "salleId", "code");

-- CreateIndex
CREATE UNIQUE INDEX "Famille_code_key" ON "Famille"("code");

-- CreateIndex
CREATE UNIQUE INDEX "PrestationCatalogue_ref_key" ON "PrestationCatalogue"("ref");

-- CreateIndex
CREATE INDEX "PrestationCatalogue_familleId_idx" ON "PrestationCatalogue"("familleId");

-- CreateIndex
CREATE INDEX "PrestationTenant_tenantId_idx" ON "PrestationTenant"("tenantId");

-- CreateIndex
CREATE UNIQUE INDEX "PrestationTenant_prestationId_tenantId_key" ON "PrestationTenant"("prestationId", "tenantId");

-- CreateIndex
CREATE INDEX "Equipement_tenantId_idx" ON "Equipement"("tenantId");

-- CreateIndex
CREATE INDEX "Ticket_tenantId_idx" ON "Ticket"("tenantId");

-- CreateIndex
CREATE INDEX "Ticket_tenantId_statut_idx" ON "Ticket"("tenantId", "statut");

-- CreateIndex
CREATE UNIQUE INDEX "Ticket_tenantId_numero_key" ON "Ticket"("tenantId", "numero");

-- CreateIndex
CREATE UNIQUE INDEX "TicketEquipement_ticketId_equipementId_key" ON "TicketEquipement"("ticketId", "equipementId");

-- CreateIndex
CREATE INDEX "LignePrestation_tenantId_idx" ON "LignePrestation"("tenantId");

-- CreateIndex
CREATE INDEX "LignePrestation_ticketId_idx" ON "LignePrestation"("ticketId");

-- CreateIndex
CREATE INDEX "PieceRemplacee_tenantId_idx" ON "PieceRemplacee"("tenantId");

-- CreateIndex
CREATE UNIQUE INDEX "ModeleChecklist_code_key" ON "ModeleChecklist"("code");

-- CreateIndex
CREATE INDEX "PointControleModele_modeleId_idx" ON "PointControleModele"("modeleId");

-- CreateIndex
CREATE INDEX "ChecklistIntervention_tenantId_idx" ON "ChecklistIntervention"("tenantId");

-- CreateIndex
CREATE INDEX "ChecklistIntervention_ticketId_idx" ON "ChecklistIntervention"("ticketId");

-- CreateIndex
CREATE INDEX "PointControle_tenantId_idx" ON "PointControle"("tenantId");

-- CreateIndex
CREATE INDEX "PointControle_checklistId_idx" ON "PointControle"("checklistId");

-- CreateIndex
CREATE INDEX "Livrable_tenantId_idx" ON "Livrable"("tenantId");

-- CreateIndex
CREATE INDEX "Livrable_ticketId_idx" ON "Livrable"("ticketId");

-- CreateIndex
CREATE INDEX "JournalEvenement_tenantId_idx" ON "JournalEvenement"("tenantId");

-- CreateIndex
CREATE INDEX "JournalEvenement_entite_entiteId_idx" ON "JournalEvenement"("entite", "entiteId");
