-- CreateTable
CREATE TABLE "LienOptique" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "ticketId" TEXT,
    "lignePrestationId" TEXT,
    "reference" TEXT NOT NULL,
    "baieDepart" TEXT,
    "uDepart" TEXT,
    "tiroirDepart" TEXT,
    "positionDepart" TEXT,
    "baieArrivee" TEXT,
    "uArrivee" TEXT,
    "tiroirArrivee" TEXT,
    "positionArrivee" TEXT,
    "typeFibre" TEXT NOT NULL,
    "connectique" TEXT NOT NULL,
    "polissage" TEXT NOT NULL,
    "longueurM" REAL NOT NULL,
    "nbConnecteurs" INTEGER NOT NULL DEFAULT 0,
    "nbEpissures" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "LienOptique_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "LienOptique_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "Ticket" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "MesureOptique" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "lienId" TEXT NOT NULL,
    "longueurOnde" INTEGER NOT NULL,
    "sens" TEXT NOT NULL,
    "attenuationTotaleDb" REAL NOT NULL,
    "appareil" TEXT,
    "operateur" TEXT,
    "dateMesure" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "MesureOptique_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "MesureOptique_lienId_fkey" FOREIGN KEY ("lienId") REFERENCES "LienOptique" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "EvenementOTDR" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "mesureId" TEXT NOT NULL,
    "ordre" INTEGER NOT NULL,
    "distanceM" REAL NOT NULL,
    "type" TEXT NOT NULL,
    "perteDb" REAL,
    "returnLossDb" REAL,
    "verdictEvenement" TEXT,
    CONSTRAINT "EvenementOTDR_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "EvenementOTDR_mesureId_fkey" FOREIGN KEY ("mesureId") REFERENCES "MesureOptique" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "RecetteResultat" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "tenantId" TEXT NOT NULL,
    "lienId" TEXT NOT NULL,
    "mesureId" TEXT,
    "verdictGlobal" TEXT NOT NULL,
    "verdictObjectifInterne" TEXT NOT NULL,
    "perteMaxConnecteur" REAL,
    "rlMin" REAL,
    "attenuationMesuree" REAL,
    "budgetTheorique" REAL,
    "margeBudgetDb" REAL,
    "evenementsFautifs" TEXT,
    "dateCalcul" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "RecetteResultat_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "RecetteResultat_lienId_fkey" FOREIGN KEY ("lienId") REFERENCES "LienOptique" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE INDEX "LienOptique_tenantId_idx" ON "LienOptique"("tenantId");

-- CreateIndex
CREATE INDEX "LienOptique_ticketId_idx" ON "LienOptique"("ticketId");

-- CreateIndex
CREATE INDEX "MesureOptique_tenantId_idx" ON "MesureOptique"("tenantId");

-- CreateIndex
CREATE INDEX "MesureOptique_lienId_idx" ON "MesureOptique"("lienId");

-- CreateIndex
CREATE INDEX "EvenementOTDR_tenantId_idx" ON "EvenementOTDR"("tenantId");

-- CreateIndex
CREATE INDEX "EvenementOTDR_mesureId_idx" ON "EvenementOTDR"("mesureId");

-- CreateIndex
CREATE INDEX "RecetteResultat_tenantId_idx" ON "RecetteResultat"("tenantId");

-- CreateIndex
CREATE INDEX "RecetteResultat_lienId_idx" ON "RecetteResultat"("lienId");
