/*
  Warnings:

  - You are about to drop the `Contact` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropTable
DROP TABLE "Contact";

-- CreateTable
CREATE TABLE "contact" (
    "id" SERIAL NOT NULL,
    "nombre" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "mensaje" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "formularios" (
    "id" UUID NOT NULL,
    "numero_voluntario" SERIAL NOT NULL,
    "nombre" TEXT NOT NULL,
    "segundo_nombre" TEXT,
    "apellido" TEXT NOT NULL,
    "documento_nro" TEXT,
    "documento_tipo" TEXT,
    "correo" TEXT NOT NULL,
    "fecha_nacimiento" DATE,
    "nacionalidad" TEXT,
    "sexo" TEXT,
    "direccion" TEXT,
    "ciudad" TEXT,
    "estado" TEXT,
    "pais" TEXT,
    "codigo_postal" TEXT,
    "telefono" TEXT,
    "whatsapp" TEXT,
    "profesion" TEXT,
    "lugar_trabajo" TEXT,
    "nivel_academico" TEXT,
    "status" TEXT NOT NULL DEFAULT 'pendiente',
    "form_version" TEXT,
    "email_sent_at" TIMESTAMPTZ,
    "submitted_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL,

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

-- CreateTable
CREATE TABLE "respuestas" (
    "id" UUID NOT NULL,
    "formulario_id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "seccion" TEXT NOT NULL DEFAULT '',
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "secciones" (
    "id" UUID NOT NULL,
    "title_key" TEXT NOT NULL,
    "title" TEXT,
    "orden" INTEGER NOT NULL DEFAULT 0,
    "columns" INTEGER NOT NULL DEFAULT 1,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "campos" (
    "id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "label_key" TEXT NOT NULL,
    "tipo" TEXT NOT NULL,
    "seccion_id" UUID,
    "required" BOOLEAN NOT NULL DEFAULT false,
    "orden" INTEGER NOT NULL DEFAULT 0,
    "options" JSONB,
    "source" TEXT,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "form_versions" (
    "id" UUID NOT NULL,
    "version" TEXT NOT NULL,
    "config" JSONB NOT NULL,
    "active" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "admins" (
    "id" UUID NOT NULL,
    "auth_user_id" UUID NOT NULL,
    "email" TEXT NOT NULL,
    "nombre" TEXT NOT NULL,
    "rol" TEXT NOT NULL DEFAULT 'coordinador',
    "activo" BOOLEAN NOT NULL DEFAULT true,
    "ultimo_acceso" TIMESTAMPTZ,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "contact_email_key" ON "contact"("email");

-- CreateIndex
CREATE INDEX "contact_email_createdAt_idx" ON "contact"("email", "createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "formularios_numero_voluntario_key" ON "formularios"("numero_voluntario");

-- CreateIndex
CREATE INDEX "formularios_correo_idx" ON "formularios"("correo");

-- CreateIndex
CREATE INDEX "formularios_status_idx" ON "formularios"("status");

-- CreateIndex
CREATE INDEX "formularios_pais_idx" ON "formularios"("pais");

-- CreateIndex
CREATE INDEX "formularios_submitted_at_idx" ON "formularios"("submitted_at");

-- CreateIndex
CREATE INDEX "respuestas_formulario_id_idx" ON "respuestas"("formulario_id");

-- CreateIndex
CREATE INDEX "respuestas_key_idx" ON "respuestas"("key");

-- CreateIndex
CREATE INDEX "respuestas_seccion_idx" ON "respuestas"("seccion");

-- CreateIndex
CREATE INDEX "secciones_orden_idx" ON "secciones"("orden");

-- CreateIndex
CREATE UNIQUE INDEX "campos_key_key" ON "campos"("key");

-- CreateIndex
CREATE INDEX "campos_seccion_id_idx" ON "campos"("seccion_id");

-- CreateIndex
CREATE INDEX "campos_tipo_idx" ON "campos"("tipo");

-- CreateIndex
CREATE UNIQUE INDEX "admins_auth_user_id_key" ON "admins"("auth_user_id");

-- CreateIndex
CREATE UNIQUE INDEX "admins_email_key" ON "admins"("email");

-- CreateIndex
CREATE INDEX "admins_email_idx" ON "admins"("email");

-- AddForeignKey
ALTER TABLE "respuestas" ADD CONSTRAINT "respuestas_formulario_id_fkey" FOREIGN KEY ("formulario_id") REFERENCES "formularios"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "campos" ADD CONSTRAINT "campos_seccion_id_fkey" FOREIGN KEY ("seccion_id") REFERENCES "secciones"("id") ON DELETE SET NULL ON UPDATE CASCADE;
