-- CreateEnum
CREATE TYPE "StatoRichiesta" AS ENUM ('PENDING', 'APPROVATA', 'RIFIUTATA');

-- CreateEnum
CREATE TYPE "OrigineRichiesta" AS ENUM ('LINK', 'PROFILO');

-- CreateTable
CREATE TABLE "gruppo_scout" (
    "org_id" TEXT NOT NULL,
    "nome" TEXT NOT NULL,
    "regione" TEXT,
    "data_creazione" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "gruppo_scout_pkey" PRIMARY KEY ("org_id")
);

-- CreateTable
CREATE TABLE "invito" (
    "id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "org_id" TEXT NOT NULL,
    "ruolo" TEXT NOT NULL,
    "scadenza" TIMESTAMP(3) NOT NULL,
    "stato" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "richiesta_ingresso" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "org_id" TEXT NOT NULL,
    "stato" "StatoRichiesta" NOT NULL DEFAULT 'PENDING',
    "origine" "OrigineRichiesta" NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "link_ingresso" (
    "id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "org_id" TEXT NOT NULL,
    "attivo" BOOLEAN NOT NULL DEFAULT true,
    "creato_da" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "richiesta_creazione_gruppo" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "nome_proposto" TEXT NOT NULL,
    "regione" TEXT,
    "stato" "StatoRichiesta" NOT NULL DEFAULT 'PENDING',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "invito_token_key" ON "invito"("token");

-- CreateIndex
CREATE INDEX "richiesta_ingresso_email_idx" ON "richiesta_ingresso"("email");

-- CreateIndex
CREATE UNIQUE INDEX "link_ingresso_token_key" ON "link_ingresso"("token");

-- CreateIndex
CREATE INDEX "richiesta_creazione_gruppo_email_idx" ON "richiesta_creazione_gruppo"("email");

-- AddForeignKey
ALTER TABLE "invito" ADD CONSTRAINT "invito_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "gruppo_scout"("org_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "richiesta_ingresso" ADD CONSTRAINT "richiesta_ingresso_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "gruppo_scout"("org_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "link_ingresso" ADD CONSTRAINT "link_ingresso_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "gruppo_scout"("org_id") ON DELETE RESTRICT ON UPDATE CASCADE;

