<?php
/**
 * Migración: registra lab_tomas_config en system_modules
 * Ejecutar: php migrations/run_20260710_lab_tomas_config_module.php
 */
require_once __DIR__ . '/../config/config.php';

$pdo = Database::getInstance()->getConnection();

// Buscar el sort_order máximo en la categoría 'clinico' para poner al final
$row = $pdo->query("SELECT MAX(sort_order) AS mx FROM system_modules WHERE category = 'clinico'")->fetch(PDO::FETCH_ASSOC);
$nextOrder = (int)($row['mx'] ?? 50) + 10;

$stmt = $pdo->prepare("
    INSERT INTO system_modules (slug, name, icon, category, route, is_active, sort_order, description)
    VALUES (?, ?, ?, ?, ?, 1, ?, ?)
    ON DUPLICATE KEY UPDATE
        name        = VALUES(name),
        icon        = VALUES(icon),
        category    = VALUES(category),
        route       = VALUES(route),
        is_active   = 1,
        sort_order  = VALUES(sort_order),
        description = VALUES(description)
");

$stmt->execute([
    'lab_tomas_config',
    'Tipos de Examen (Tomas)',
    'fas fa-vials',
    'clinico',
    '/lab_tomas_config.php',
    $nextOrder,
    'Configurar tipos de examen y ciclos de tomas prolongadas (F-LAB-28)',
]);

echo "✅ Módulo lab_tomas_config registrado en system_modules (sort_order=$nextOrder).\n";
