-- CreateTable
CREATE TABLE "evento" (
    "id" TEXT NOT NULL,
    "org_id" TEXT NOT NULL,
    "branca_id" TEXT NOT NULL,
    "parent_id" TEXT,
    "titolo" TEXT NOT NULL,
    "descrizione" TEXT,
    "data_inizio" TIMESTAMP(3) NOT NULL,
    "data_fine" TIMESTAMP(3) NOT NULL,
    "tipo" TEXT NOT NULL,
    "location" TEXT,
    "creato_da" TEXT NOT NULL,
    "creato_il" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "evento_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "risorsa_collegata" (
    "id" TEXT NOT NULL,
    "evento_id" TEXT NOT NULL,
    "tipo_risorsa" TEXT NOT NULL,
    "risorsa_id" TEXT NOT NULL,
    "servizio_origine" TEXT NOT NULL,
    "metadata" JSONB,
    "creato_il" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "risorsa_collegata_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "evento_org_id_branca_id_data_inizio_idx" ON "evento"("org_id", "branca_id", "data_inizio");

-- CreateIndex
CREATE INDEX "evento_parent_id_idx" ON "evento"("parent_id");

-- AddForeignKey
ALTER TABLE "evento" ADD CONSTRAINT "evento_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "evento"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "risorsa_collegata" ADD CONSTRAINT "risorsa_collegata_evento_id_fkey" FOREIGN KEY ("evento_id") REFERENCES "evento"("id") ON DELETE CASCADE ON UPDATE CASCADE;
