-- GeneaCase Sprint 10: cronologías, informes PDF y anexos.
-- Migración aditiva: no elimina ni modifica documentos originales.

CREATE TABLE IF NOT EXISTS gene_evento_control (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  id_empresa BIGINT UNSIGNED NOT NULL,
  id_caso BIGINT UNSIGNED NOT NULL,
  id_evento BIGINT UNSIGNED NOT NULL,
  origen ENUM('manual','persona','afirmacion','documento','busqueda','comunicacion','sistema') NOT NULL DEFAULT 'manual',
  clave_automatica VARCHAR(190) NULL,
  visible_portal TINYINT(1) NOT NULL DEFAULT 0,
  archivado_en DATETIME NULL,
  creado_por BIGINT UNSIGNED NOT NULL,
  creado_en DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  actualizado_en DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (id_empresa) REFERENCES org_empresas(id),
  FOREIGN KEY (id_caso) REFERENCES gene_casos(id) ON DELETE CASCADE,
  FOREIGN KEY (id_evento) REFERENCES gene_eventos(id) ON DELETE CASCADE,
  FOREIGN KEY (creado_por) REFERENCES org_usuarios(id),
  UNIQUE KEY uq_evento_control_evento (id_evento),
  UNIQUE KEY uq_evento_control_auto (id_empresa,id_caso,clave_automatica),
  INDEX idx_evento_control_caso (id_empresa,id_caso,origen,archivado_en)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS gene_evento_referencias (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  id_empresa BIGINT UNSIGNED NOT NULL,
  id_caso BIGINT UNSIGNED NOT NULL,
  id_evento BIGINT UNSIGNED NOT NULL,
  referencia_tipo ENUM('cita','afirmacion','documento','busqueda','comunicacion','persona','otro') NOT NULL,
  referencia_id BIGINT UNSIGNED NOT NULL,
  nota VARCHAR(500) NULL,
  creado_en DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (id_empresa) REFERENCES org_empresas(id),
  FOREIGN KEY (id_caso) REFERENCES gene_casos(id) ON DELETE CASCADE,
  FOREIGN KEY (id_evento) REFERENCES gene_eventos(id) ON DELETE CASCADE,
  UNIQUE KEY uq_evento_referencia (id_evento,referencia_tipo,referencia_id),
  INDEX idx_evento_ref_caso (id_empresa,id_caso,referencia_tipo)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS gene_informes (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  id_empresa BIGINT UNSIGNED NOT NULL,
  id_caso BIGINT UNSIGNED NOT NULL,
  uuid_publico CHAR(36) NOT NULL UNIQUE,
  tipo ENUM('profesional','familiar') NOT NULL DEFAULT 'profesional',
  idioma CHAR(2) NOT NULL DEFAULT 'es',
  titulo VARCHAR(300) NOT NULL,
  estado ENUM('borrador','generado','publicado','archivado') NOT NULL DEFAULT 'borrador',
  resumen_ejecutivo TEXT NULL,
  conclusiones LONGTEXT NULL,
  asuntos_pendientes LONGTEXT NULL,
  configuracion_json JSON NULL,
  version_actual INT UNSIGNED NOT NULL DEFAULT 0,
  creado_por BIGINT UNSIGNED NOT NULL,
  generado_por BIGINT UNSIGNED NULL,
  publicado_por BIGINT UNSIGNED NULL,
  creado_en DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  actualizado_en DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  generado_en DATETIME NULL,
  publicado_en DATETIME NULL,
  archivado_en DATETIME NULL,
  FOREIGN KEY (id_empresa) REFERENCES org_empresas(id),
  FOREIGN KEY (id_caso) REFERENCES gene_casos(id) ON DELETE CASCADE,
  FOREIGN KEY (idioma) REFERENCES sys_idiomas(codigo),
  FOREIGN KEY (creado_por) REFERENCES org_usuarios(id),
  FOREIGN KEY (generado_por) REFERENCES org_usuarios(id),
  FOREIGN KEY (publicado_por) REFERENCES org_usuarios(id),
  INDEX idx_informe_caso_estado (id_empresa,id_caso,estado),
  INDEX idx_informe_publicado (id_empresa,id_caso,publicado_en)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS gene_informe_versiones (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  id_empresa BIGINT UNSIGNED NOT NULL,
  id_caso BIGINT UNSIGNED NOT NULL,
  id_informe BIGINT UNSIGNED NOT NULL,
  numero_version INT UNSIGNED NOT NULL,
  contenido_html LONGTEXT NOT NULL,
  ruta_pdf VARCHAR(900) NOT NULL,
  sha256 CHAR(64) NOT NULL,
  tamano_bytes BIGINT UNSIGNED NOT NULL,
  metadata_json JSON NULL,
  generado_por BIGINT UNSIGNED NOT NULL,
  generado_en DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (id_empresa) REFERENCES org_empresas(id),
  FOREIGN KEY (id_caso) REFERENCES gene_casos(id) ON DELETE CASCADE,
  FOREIGN KEY (id_informe) REFERENCES gene_informes(id) ON DELETE CASCADE,
  FOREIGN KEY (generado_por) REFERENCES org_usuarios(id),
  UNIQUE KEY uq_informe_version (id_informe,numero_version),
  INDEX idx_informe_version_caso (id_empresa,id_caso,id_informe)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS portal_informes_publicados (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  id_empresa BIGINT UNSIGNED NOT NULL,
  id_caso BIGINT UNSIGNED NOT NULL,
  id_informe BIGINT UNSIGNED NOT NULL,
  publicado_por BIGINT UNSIGNED NOT NULL,
  publicado_en DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (id_empresa) REFERENCES org_empresas(id),
  FOREIGN KEY (id_caso) REFERENCES gene_casos(id) ON DELETE CASCADE,
  FOREIGN KEY (id_informe) REFERENCES gene_informes(id) ON DELETE CASCADE,
  FOREIGN KEY (publicado_por) REFERENCES org_usuarios(id),
  UNIQUE KEY uq_portal_informe_caso (id_empresa,id_caso),
  INDEX idx_portal_informe (id_empresa,id_informe)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
