<?php

require __DIR__ . '/../vendor/autoload.php';

$app = require __DIR__ . '/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

$service = app(App\Services\LoadDocumentService::class);
$ref = new ReflectionClass($service);
$extractText = $ref->getMethod('extractText');
$extractText->setAccessible(true);

$folder = 'C:/Users/Hp/Downloads/not working';
$files = glob($folder . '/*.pdf') ?: [];
sort($files);

$keywords = '/miles?|mileage|distance|line\s*haul|carrier\s*pay|total\s*carrier\s*pay|total\s*agreed\s*to\s*charges|total\s*:\s*\$|rate|origin|destination|from:|to:/i';

foreach ($files as $filePath) {
    $text = (string) $extractText->invoke($service, $filePath, 'pdf');
    $lines = preg_split('/\r\n|\r|\n/', $text) ?: [];

    echo "\n==============================\n";
    echo basename($filePath) . "\n";
    echo "Text length: " . strlen($text) . "\n";

    $printed = 0;
    foreach ($lines as $idx => $line) {
        $line = trim($line);
        if ($line === '') {
            continue;
        }
        if (preg_match($keywords, $line)) {
            echo str_pad((string) ($idx + 1), 4, ' ', STR_PAD_LEFT) . ': ' . $line . "\n";
            $printed++;
            if ($printed >= 50) {
                break;
            }
        }
    }

    if ($printed === 0) {
        echo "No keyword lines found.\n";
    }
}
