-- CreateEnum
CREATE TYPE "RoleName" AS ENUM ('ADMIN', 'DIGITADOR', 'VALIDADOR', 'CONSULTA');

-- CreateEnum
CREATE TYPE "EstadoFicha" AS ENUM ('BORRADOR', 'COMPLETA', 'INCOMPLETA', 'ARCHIVADA');

-- CreateEnum
CREATE TYPE "EstadoOcupacion" AS ENUM ('CON_DUENO', 'CON_RESOLUCION', 'SIN_RESOLUCION', 'SIN_OCUPANTE', 'INHABILITADO_SERVIU', 'SEDE_SOCIAL', 'OTRO');

-- CreateEnum
CREATE TYPE "SituacionLote" AS ENUM ('CON_DUENO', 'CON_RESOLUCION', 'SIN_RESOLUCION', 'SIN_OCUPANTE', 'OTRO');

-- CreateEnum
CREATE TYPE "TipoDocumento" AS ENUM ('RSH', 'CUENTA_LUZ', 'CEDULA_IDENTIDAD', 'ESCRITURA', 'RESOLUCION', 'CERTIFICADO', 'FOTO_VIVIENDA', 'FOTO_TERRENO', 'OTRO');

-- CreateEnum
CREATE TYPE "EstadoDocumento" AS ENUM ('PENDIENTE', 'VALIDADO', 'RECHAZADO');

-- CreateTable
CREATE TABLE "Role" (
    "id" TEXT NOT NULL,
    "name" "RoleName" NOT NULL,
    "description" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "User" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "passwordHash" TEXT NOT NULL,
    "roleId" TEXT NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "deletedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "EmpadronamientoFicha" (
    "id" TEXT NOT NULL,
    "codigo" TEXT NOT NULL,
    "estadoFicha" "EstadoFicha" NOT NULL DEFAULT 'BORRADOR',
    "observacionesRelevantes" TEXT,
    "createdById" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "deletedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "Ocupante" (
    "id" TEXT NOT NULL,
    "fichaId" TEXT NOT NULL,
    "nombre" TEXT,
    "rut" TEXT,
    "direccionCompleta" TEXT,
    "desdeAnio" INTEGER,
    "aniosAntiguedad" INTEGER,
    "telefono" TEXT,
    "estadoOcupacion" "EstadoOcupacion" NOT NULL DEFAULT 'SIN_RESOLUCION',
    "observaciones" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Lote" (
    "id" TEXT NOT NULL,
    "fichaId" TEXT NOT NULL,
    "lote" TEXT,
    "manzana" TEXT,
    "direccionLote" TEXT,
    "nombreAsociado" TEXT,
    "resolucion" TEXT,
    "fojas" TEXT,
    "numero" TEXT,
    "anio" INTEGER,
    "rol" TEXT,
    "situacionLote" "SituacionLote" NOT NULL DEFAULT 'SIN_RESOLUCION',
    "duenoAsignatario" TEXT,
    "rutDuenoAsignatario" TEXT,
    "contribuciones" TEXT,
    "actual" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SituacionVivienda" (
    "id" TEXT NOT NULL,
    "fichaId" TEXT NOT NULL,
    "tipoVivienda" TEXT,
    "estadoConstructivo" TEXT,
    "habitabilidad" TEXT,
    "materialPredominante" TEXT,
    "tieneAgua" BOOLEAN NOT NULL DEFAULT false,
    "tieneLuz" BOOLEAN NOT NULL DEFAULT false,
    "tieneAlcantarillado" BOOLEAN NOT NULL DEFAULT false,
    "grupoFamiliar" TEXT,
    "observacionesSociales" TEXT,
    "observacionesTerritoriales" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Deslinde" (
    "id" TEXT NOT NULL,
    "fichaId" TEXT NOT NULL,
    "norte" TEXT,
    "sur" TEXT,
    "este" TEXT,
    "oeste" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Documento" (
    "id" TEXT NOT NULL,
    "fichaId" TEXT NOT NULL,
    "tipo" "TipoDocumento" NOT NULL,
    "nombreArchivo" TEXT NOT NULL,
    "rutaArchivo" TEXT NOT NULL,
    "size" INTEGER NOT NULL,
    "mimeType" TEXT NOT NULL,
    "estado" "EstadoDocumento" NOT NULL DEFAULT 'PENDIENTE',
    "uploadedById" TEXT NOT NULL,
    "validatedById" TEXT,
    "validatedAt" TIMESTAMP(3),
    "observacionValidador" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "deletedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "Comentario" (
    "id" TEXT NOT NULL,
    "fichaId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "contenido" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "deletedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "AuditLog" (
    "id" TEXT NOT NULL,
    "fichaId" TEXT,
    "userId" TEXT,
    "action" TEXT NOT NULL,
    "entity" TEXT NOT NULL,
    "entityId" TEXT,
    "metadata" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "Role_name_key" ON "Role"("name");

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

-- CreateIndex
CREATE INDEX "User_roleId_idx" ON "User"("roleId");

-- CreateIndex
CREATE INDEX "User_deletedAt_idx" ON "User"("deletedAt");

-- CreateIndex
CREATE UNIQUE INDEX "EmpadronamientoFicha_codigo_key" ON "EmpadronamientoFicha"("codigo");

-- CreateIndex
CREATE INDEX "EmpadronamientoFicha_createdById_idx" ON "EmpadronamientoFicha"("createdById");

-- CreateIndex
CREATE INDEX "EmpadronamientoFicha_estadoFicha_idx" ON "EmpadronamientoFicha"("estadoFicha");

-- CreateIndex
CREATE INDEX "EmpadronamientoFicha_deletedAt_idx" ON "EmpadronamientoFicha"("deletedAt");

-- CreateIndex
CREATE UNIQUE INDEX "Ocupante_fichaId_key" ON "Ocupante"("fichaId");

-- CreateIndex
CREATE INDEX "Ocupante_rut_idx" ON "Ocupante"("rut");

-- CreateIndex
CREATE INDEX "Ocupante_nombre_idx" ON "Ocupante"("nombre");

-- CreateIndex
CREATE INDEX "Ocupante_estadoOcupacion_idx" ON "Ocupante"("estadoOcupacion");

-- CreateIndex
CREATE UNIQUE INDEX "Lote_fichaId_key" ON "Lote"("fichaId");

-- CreateIndex
CREATE INDEX "Lote_lote_idx" ON "Lote"("lote");

-- CreateIndex
CREATE INDEX "Lote_manzana_idx" ON "Lote"("manzana");

-- CreateIndex
CREATE INDEX "Lote_rol_idx" ON "Lote"("rol");

-- CreateIndex
CREATE INDEX "Lote_resolucion_idx" ON "Lote"("resolucion");

-- CreateIndex
CREATE INDEX "Lote_situacionLote_idx" ON "Lote"("situacionLote");

-- CreateIndex
CREATE UNIQUE INDEX "SituacionVivienda_fichaId_key" ON "SituacionVivienda"("fichaId");

-- CreateIndex
CREATE UNIQUE INDEX "Deslinde_fichaId_key" ON "Deslinde"("fichaId");

-- CreateIndex
CREATE INDEX "Documento_fichaId_idx" ON "Documento"("fichaId");

-- CreateIndex
CREATE INDEX "Documento_tipo_idx" ON "Documento"("tipo");

-- CreateIndex
CREATE INDEX "Documento_estado_idx" ON "Documento"("estado");

-- CreateIndex
CREATE INDEX "Documento_uploadedById_idx" ON "Documento"("uploadedById");

-- CreateIndex
CREATE INDEX "Documento_validatedById_idx" ON "Documento"("validatedById");

-- CreateIndex
CREATE INDEX "Documento_deletedAt_idx" ON "Documento"("deletedAt");

-- CreateIndex
CREATE INDEX "Comentario_fichaId_idx" ON "Comentario"("fichaId");

-- CreateIndex
CREATE INDEX "Comentario_userId_idx" ON "Comentario"("userId");

-- CreateIndex
CREATE INDEX "Comentario_deletedAt_idx" ON "Comentario"("deletedAt");

-- CreateIndex
CREATE INDEX "AuditLog_fichaId_idx" ON "AuditLog"("fichaId");

-- CreateIndex
CREATE INDEX "AuditLog_userId_idx" ON "AuditLog"("userId");

-- CreateIndex
CREATE INDEX "AuditLog_entity_idx" ON "AuditLog"("entity");

-- CreateIndex
CREATE INDEX "AuditLog_createdAt_idx" ON "AuditLog"("createdAt");

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EmpadronamientoFicha" ADD CONSTRAINT "EmpadronamientoFicha_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Ocupante" ADD CONSTRAINT "Ocupante_fichaId_fkey" FOREIGN KEY ("fichaId") REFERENCES "EmpadronamientoFicha"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Lote" ADD CONSTRAINT "Lote_fichaId_fkey" FOREIGN KEY ("fichaId") REFERENCES "EmpadronamientoFicha"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SituacionVivienda" ADD CONSTRAINT "SituacionVivienda_fichaId_fkey" FOREIGN KEY ("fichaId") REFERENCES "EmpadronamientoFicha"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Deslinde" ADD CONSTRAINT "Deslinde_fichaId_fkey" FOREIGN KEY ("fichaId") REFERENCES "EmpadronamientoFicha"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Documento" ADD CONSTRAINT "Documento_fichaId_fkey" FOREIGN KEY ("fichaId") REFERENCES "EmpadronamientoFicha"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Documento" ADD CONSTRAINT "Documento_uploadedById_fkey" FOREIGN KEY ("uploadedById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Documento" ADD CONSTRAINT "Documento_validatedById_fkey" FOREIGN KEY ("validatedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Comentario" ADD CONSTRAINT "Comentario_fichaId_fkey" FOREIGN KEY ("fichaId") REFERENCES "EmpadronamientoFicha"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Comentario" ADD CONSTRAINT "Comentario_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AuditLog" ADD CONSTRAINT "AuditLog_fichaId_fkey" FOREIGN KEY ("fichaId") REFERENCES "EmpadronamientoFicha"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AuditLog" ADD CONSTRAINT "AuditLog_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
