<?php
/**
 * Scans PHP files for potential DataTables issues:
 * 1. Tables with class="datatable" but no id attribute
 * 2. colspan/rowspan inside <tbody> (causes "Incorrect column count")
 * 3. References to DataTables_Table_0 (auto-generated ID collision risk)
 * 4. Permission-based conditional <td> removal inside datatable tables
 * 5. columnDefs targets that reference non-existent columns
 */

$rootDir = __DIR__ . '/..';
$issues = [];

// Recursively scan PHP files (skip vendor, node_modules, cache dirs)
$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootDir, RecursiveDirectoryIterator::SKIP_DOTS)
);

foreach ($iterator as $file) {
    if ($file->getExtension() !== 'php') continue;
    $path = $file->getRealPath();
    $relPath = str_replace($rootDir . '\\', '', $path);

    // Skip known non-application directories
    $skipDirs = ['vendor\\', 'node_modules\\', 'cache\\', 'tools\\'];
    $skip = false;
    foreach ($skipDirs as $d) {
        if (strpos($relPath, $d) === 0) { $skip = true; break; }
    }
    if ($skip) continue;

    $content = file_get_contents($path);
    $lines = explode("\n", $content);
    $lineNum = 0;
    $inDataTable = false;
    $inTable = false;
    $inTbody = false;
    $tbodyStartLine = 0;
    $theadCols = 0;
    $hasId = false;
    $hasDatatableClass = false;

    foreach ($lines as $rawLine) {
        $lineNum++;
        $line = trim($rawLine);

        // Check for datatable class (but not no-datatable)
        if (preg_match('/<table[^>]*class="[^"]*\bdatatable\b[^"]*"/i', $line, $m)
            && !preg_match('/no-datatable/i', $line)) {
            $inDataTable = true;
            $inTable = true;
            if (!preg_match('/id\s*=\s*["\']/', $line)) {
                $issues[] = "$relPath:$lineNum - datatable without id attribute";
            }
        }

        // Check for any table with colspan in tbody
        if (preg_match('/<table[^>]*>/i', $line)) {
            $inTable = true;
            $inTbody = false;
            $tbodyStartLine = 0;
        }

        if ($inTable && preg_match('/<tbody/i', $line)) {
            $inTbody = true;
            $tbodyStartLine = $lineNum;
        }

        if ($inDataTable && $inTbody && preg_match('/colspan\s*=/i', $line)) {
            $issues[] = "$relPath:$lineNum - colspan inside <tbody> of datatable (causes 'Incorrect column count')";
        }

        if ($inTbody && preg_match('/<\/tbody>/i', $line)) {
            $inTbody = false;
        }

        if (preg_match('/<\/table>/i', $line)) {
            $inDataTable = false;
            $inTable = false;
            $inTbody = false;
        }

        // Check for DataTables_Table_0 references
        if (preg_match('/DataTables_Table_0/i', $line)) {
            $issues[] = "$relPath:$lineNum - references DataTables_Table_0 (auto-generated, collision risk)";
        }
    }
}

// Output
echo "DataTables Audit Results\n";
echo "=======================\n\n";

if (empty($issues)) {
    echo "No issues found.\n";
} else {
    foreach ($issues as $issue) {
        echo "  - $issue\n";
    }
    echo "\n" . count($issues) . " issue(s) found.\n";
}
