#!/usr/bin/env php
<?php
/**
 * Preview Conversion - Test extraction on sample pages
 * Shows what data will be extracted before converting
 */

require_once __DIR__ . '/config.php';

// Sample pages to test
$samples = [
    'agile-management/advanced-certified-scrum-product-owner-training.php',
    'devops/devops-training.php',
    'project-management/pmp-certification-training.php',
    'it-service-management/itil-foundation-certification-training.php'
];

echo "CONVERSION PREVIEW - Testing data extraction\n";
echo str_repeat("=", 60) . "\n\n";

foreach ($samples as $file) {
    $path = __DIR__ . '/' . $file;
    
    if (!file_exists($path)) {
        echo "[$file] NOT FOUND\n\n";
        continue;
    }
    
    echo "[$file]\n";
    
    $content = file_get_contents($path);
    
    // Extract basic data
    $data = [];
    
    // Title
    if (preg_match('/<h1[^>]*>([^<]+)<\/h1>/i', $content, $m)) {
        $data['title'] = trim($m[1]);
    }
    
    // Subtitle
    if (preg_match('/<meta[^>]*name=["\']description["\'][^>]*content=["\']([^"\']+)["\']/i', $content, $m)) {
        $data['subtitle'] = trim($m[1]);
    }
    
    // Features
    $features = [];
    if (preg_match_all('/<li[^>]*class=["\'][^"\']*(?:listItem|feature)[^"\']*["\'][^>]*>(?:<[^>]*>)?([^<]+)/i', $content, $m)) {
        $features = array_slice($m[1], 0, 3);
    }
    
    // Duration
    if (preg_match('/(\d+)\s*(?:Hours?|Hrs?)/i', $content, $m)) {
        $data['duration'] = $m[1] . ' Hours';
    }
    
    // Prerequisites
    $prereqs = [];
    if (preg_match('/Prerequisites[^>]*>.*?<ul[^>]*>(.*?)<\/ul>/is', $content, $m)) {
        $section = $m[1];
        if (preg_match_all('/<li[^>]*>(?:<[^>]*>)?([^<]+)/i', $section, $matches)) {
            $prereqs = array_map('trim', array_slice($matches[1], 0, 3));
        }
    }
    
    echo "  Title: " . ($data['title'] ?? 'NOT FOUND') . "\n";
    echo "  Subtitle: " . (isset($data['subtitle']) ? substr($data['subtitle'], 0, 60) . '...' : 'NOT FOUND') . "\n";
    echo "  Duration: " . ($data['duration'] ?? 'NOT FOUND') . "\n";
    echo "  Features found: " . count($features) . "\n";
    echo "  Prerequisites found: " . count($prereqs) . "\n";
    
    if (!empty($features)) {
        foreach ($features as $i => $f) {
            echo "    - " . trim($f) . "\n";
        }
    }
    
    echo "\n";
}

echo "PREVIEW COMPLETE\n";
echo "Run the actual conversion with: php batch-convert-courses.php\n";
