<?php
/**
 * Prueba corrección en casos específicos variados
 */

$conn = new mysqli("localhost", "preciosunitarios_seo", "ACl%EEFd=V-Yvb??", "preciosunitarios_seo");
$conn->set_charset("utf8mb4");

// IDs a probar (casos variados)
$test_ids = [20, 23, 65, 377, 98, 107, 144];

function detectIssues($html) {
    $issues = [];
    libxml_use_internal_errors(true);
    $doc = new DOMDocument("1.0", "UTF-8");
    $doc->loadHTML('<?xml encoding="UTF-8"><div id="w">' . $html . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    libxml_clear_errors();

    $validChildren = ["li", "script", "template"];
    foreach (["ul", "ol"] as $tag) {
        foreach ($doc->getElementsByTagName($tag) as $list) {
            foreach ($list->childNodes as $child) {
                if ($child->nodeType === XML_ELEMENT_NODE) {
                    $childTag = strtolower($child->nodeName);
                    if (!in_array($childTag, $validChildren)) {
                        $issues[] = "<$tag> contiene <$childTag>";
                    }
                }
            }
        }
    }
    return $issues;
}

function fixMalformedLists($html) {
    $result = ['fixed' => false, 'html' => $html, 'changes' => 0];

    libxml_use_internal_errors(true);
    $doc = new DOMDocument("1.0", "UTF-8");
    $doc->loadHTML('<?xml encoding="UTF-8"><div id="w">' . $html . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    libxml_clear_errors();

    $lists = [];
    foreach ($doc->getElementsByTagName('ul') as $ul) { $lists[] = $ul; }
    foreach ($doc->getElementsByTagName('ol') as $ol) { $lists[] = $ol; }

    $changes = 0;
    $validChildren = ["li", "script", "template"];

    foreach ($lists as $list) {
        $nodesToProcess = [];
        foreach ($list->childNodes as $child) {
            if ($child->nodeType === XML_ELEMENT_NODE) {
                $tagName = strtolower($child->nodeName);
                if (!in_array($tagName, $validChildren)) {
                    $nodesToProcess[] = $child;
                }
            }
        }

        foreach ($nodesToProcess as $node) {
            $tagName = strtolower($node->nodeName);
            $prevLi = null;
            $prev = $node->previousSibling;

            while ($prev) {
                if ($prev->nodeType === XML_ELEMENT_NODE && strtolower($prev->nodeName) === 'li') {
                    $prevLi = $prev;
                    break;
                }
                $prev = $prev->previousSibling;
            }

            if ($prevLi) {
                $prevLi->appendChild($node);
                $changes++;
            } else {
                $newLi = $doc->createElement('li');
                $list->insertBefore($newLi, $node);
                $newLi->appendChild($node);
                $changes++;
            }
        }
    }

    if ($changes > 0) {
        $wrapper = $doc->getElementById('w');
        if ($wrapper) {
            $innerHTML = '';
            foreach ($wrapper->childNodes as $child) {
                $innerHTML .= $doc->saveHTML($child);
            }
            $result['html'] = $innerHTML;
            $result['fixed'] = true;
            $result['changes'] = $changes;
        }
    }

    return $result;
}

echo "=====================================================\n";
echo "  PRUEBA DE CORRECCIÓN EN CASOS VARIADOS\n";
echo "=====================================================\n\n";

$ids_str = implode(',', $test_ids);
$query = "SELECT id, page, html FROM datos_seo_pagina WHERE id IN ($ids_str)";
$result = $conn->query($query);

$all_passed = true;

while ($row = $result->fetch_assoc()) {
    $id = $row['id'];
    $url = $row['page'];
    $html = $row['html'];

    echo "─────────────────────────────────────────────────\n";
    echo "POST ID: $id\n";
    echo "URL: $url\n\n";

    // Detectar problemas antes
    $issues_before = detectIssues($html);
    echo "ANTES:\n";
    echo "  Problemas: " . count($issues_before) . "\n";
    $unique_types = array_unique($issues_before);
    foreach ($unique_types as $type) {
        echo "    - $type\n";
    }

    // Aplicar corrección
    $fixResult = fixMalformedLists($html);

    // Detectar problemas después
    $issues_after = detectIssues($fixResult['html']);

    echo "\nDESPUÉS:\n";
    echo "  Cambios aplicados: {$fixResult['changes']}\n";
    echo "  Problemas restantes: " . count($issues_after) . "\n";

    if (count($issues_after) > 0) {
        echo "  ⚠️ Problemas NO resueltos:\n";
        foreach (array_unique($issues_after) as $type) {
            echo "    - $type\n";
        }
        $all_passed = false;
    }

    // Verificar integridad del HTML
    $tags_before = [
        'ul' => substr_count($html, '<ul'),
        'ol' => substr_count($html, '<ol'),
        'li' => substr_count($html, '<li'),
    ];
    $tags_after = [
        'ul' => substr_count($fixResult['html'], '<ul'),
        'ol' => substr_count($fixResult['html'], '<ol'),
        'li' => substr_count($fixResult['html'], '<li'),
    ];

    echo "\nINTEGRIDAD DE TAGS:\n";
    echo "  <ul>: {$tags_before['ul']} → {$tags_after['ul']} ";
    echo ($tags_before['ul'] === $tags_after['ul'] ? "✓" : "⚠️ CAMBIÓ") . "\n";
    echo "  <ol>: {$tags_before['ol']} → {$tags_after['ol']} ";
    echo ($tags_before['ol'] === $tags_after['ol'] ? "✓" : "⚠️ CAMBIÓ") . "\n";
    echo "  <li>: {$tags_before['li']} → {$tags_after['li']} ";
    echo ($tags_before['li'] === $tags_after['li'] ? "✓" : "⚠️ CAMBIÓ") . "\n";

    // Resultado
    if (count($issues_after) === 0 &&
        $tags_before['ul'] === $tags_after['ul'] &&
        $tags_before['ol'] === $tags_after['ol']) {
        echo "\n✅ RESULTADO: CORRECCIÓN EXITOSA\n";
    } else {
        echo "\n❌ RESULTADO: REQUIERE REVISIÓN\n";
        $all_passed = false;
    }
}

echo "\n=====================================================\n";
if ($all_passed) {
    echo "✅ TODOS LOS CASOS PASARON LA PRUEBA\n";
} else {
    echo "⚠️ ALGUNOS CASOS REQUIEREN REVISIÓN\n";
}
echo "=====================================================\n";

$conn->close();
