#!/usr/bin/env php
<?php
/**
 * Convert remaining pages to use KnowledgeHut template
 * These are the pages missing KH layout
 */

require_once __DIR__ . '/config.php';

$pagesToConvert = [
    'itsm-certifications.php',
    'artificial-intelligence-courses.php',
    'it-security/ceh-ethical-hacking-certification-course.php',
    'it-security/cisa-certification-training.php',
    'it-security/cism-training.php',
    'it-security/ccsp-certification-training.php',
    'it-security/pci-data-security-standard-training.php',
    'it-security/cobit-5-foundation-training.php',
];

$PLACEHOLDERS = [
    'features' => [
        "World-class certification training designed to help you pass on your first attempt.",
        "Live instructor-led sessions with hands-on exercises and real-world examples.",
        "Comprehensive study materials and practice tests included."
    ],
    'prerequisites' => [
        'No specific prerequisites required',
        'Suitable for beginners and experienced professionals',
        'Basic understanding of the subject is helpful'
    ],
    'highlights' => [
        ['icon' => 'users', 'title' => 'Live Training', 'text' => '35 Hours of instructor-led sessions'],
        ['icon' => 'certificate', 'title' => 'Certification', 'text' => 'Exam fee included in course'],
        ['icon' => 'clock', 'title' => 'Flexible Schedule', 'text' => 'Weekend & weekday batches'],
        ['icon' => 'headset', 'title' => '24/7 Support', 'text' => 'Learning assistance anytime'],
        ['icon' => 'file-alt', 'title' => 'Study Materials', 'text' => 'Official courseware included'],
        ['icon' => 'award', 'title' => 'Mock Exams', 'text' => 'Practice tests to prepare']
    ],
    'syllabus' => [
        ['title' => 'Module 1: Foundation Concepts', 'items' => ['Introduction to key concepts', 'Core principles and terminology', 'Industry best practices']],
        ['title' => 'Module 2: Implementation', 'items' => ['Practical application', 'Case studies', 'Hands-on exercises']],
        ['title' => 'Module 3: Advanced Topics', 'items' => ['Deep dive into methodology', 'Real-world scenarios', 'Problem-solving techniques']],
        ['title' => 'Module 4: Exam Preparation', 'items' => ['Review of key concepts', 'Practice questions', 'Exam tips and strategies']]
    ],
    'faqs' => [
        ['q' => 'What is this certification?', 'a' => 'This is a professional certification that validates your expertise in the field. It is recognized globally by employers.'],
        ['q' => 'How long is the training?', 'a' => 'The training duration varies by course. Check the schedule for specific batch timings.'],
        ['q' => 'What is the exam format?', 'a' => 'The exam consists of multiple-choice questions. You will have sufficient time to complete it.'],
        ['q' => 'Is there any prerequisite?', 'a' => 'No specific prerequisites are required. The course is suitable for beginners.']
    ],
    'reviews' => [
        ['name' => 'John D.', 'role' => 'IT Manager', 'rating' => 5, 'text' => 'The training was excellent and helped me pass my certification on the first attempt.'],
        ['name' => 'Sarah M.', 'role' => 'Project Lead', 'rating' => 5, 'text' => 'Highly recommended for anyone looking to upskill. The course materials were comprehensive.']
    ],
    'price' => ['current' => '$999', 'original' => '$1,299', 'discount' => '23% OFF'],
    'rating' => ['google' => 4.8, 'facebook' => 4.7],
    'enrolled' => '50,000+',
    'duration' => '35 Hours'
];

$CATEGORY_IMAGES = [
    'Cyber Security' => [
        'https://images.unsplash.com/photo-1563206767-5b1d129e4353?w=1200&q=80',
        'https://images.unsplash.com/photo-1550751827-4bd374c3f58b?w=1200&q=80',
        'https://images.unsplash.com/photo-1516110833967-0b5716ca1387?w=1200&q=80',
    ],
    'IT Service Management' => [
        'https://images.unsplash.com/photo-1558494949-ef010cbdcc31?w=1200&q=80',
        'https://images.unsplash.com/photo-1451187580459-43490279c0fa?w=1200&q=80',
        'https://images.unsplash.com/photo-1518770660439-4636190af475?w=1200&q=80',
    ],
    'Artificial Intelligence' => [
        'https://images.unsplash.com/photo-1677442136019-21780ecad995?w=1200&q=80',
        'https://images.unsplash.com/photo-1620712943543-bcc4688e7485?w=1200&q=80',
        'https://images.unsplash.com/photo-1485827404703-89b55fcc595e?w=1200&q=80',
    ],
];

function generateTitle($filename) {
    $name = str_replace('.php', '', $filename);
    $name = str_replace('-', ' ', $name);
    
    // Uppercase acronyms
    $acronyms = ['ceh', 'cisa', 'cism', 'ccsp', 'cobit', 'cissp', 'pci', 'dss'];
    foreach ($acronyms as $acro) {
        $name = str_replace($acro, strtoupper($acro), $name);
    }
    
    return ucwords($name) . ' Certification Training';
}

function getCategoryForPage($pagePath) {
    if (strpos($pagePath, 'it-security') !== false || strpos($pagePath, 'ceh') !== false || strpos($pagePath, 'cisa') !== false || strpos($pagePath, 'cism') !== false || strpos($pagePath, 'ccsp') !== false || strpos($pagePath, 'pci') !== false || strpos($pagePath, 'cobit') !== false) {
        return 'Cyber Security';
    }
    if (strpos($pagePath, 'itsm') !== false) {
        return 'IT Service Management';
    }
    if (strpos($pagePath, 'artificial') !== false || strpos($pagePath, 'ai') !== false) {
        return 'Artificial Intelligence';
    }
    return 'Professional Courses';
}

function getImageForCategory($category) {
    global $CATEGORY_IMAGES;
    if (isset($CATEGORY_IMAGES[$category])) {
        $images = $CATEGORY_IMAGES[$category];
        return $images[array_rand($images)];
    }
    return 'https://images.unsplash.com/photo-1522202176988-66273c2fd55f?w=1200&q=80';
}

echo "=== CONVERTING PAGES TO KH TEMPLATE ===\n\n";

$converted = 0;
$failed = 0;

foreach ($pagesToConvert as $page) {
    $filePath = __DIR__ . '/' . $page;
    
    if (!file_exists($filePath)) {
        echo "[$page] FILE NOT FOUND\n";
        $failed++;
        continue;
    }
    
    echo "[$page] Converting... ";
    
    $filename = basename($page);
    $title = generateTitle($filename);
    $category = getCategoryForPage($page);
    $heroImage = getImageForCategory($category);
    
    // Determine require path
    $requirePath = (strpos($page, '/') !== false) ? "__DIR__ . '/../course-template.php'" : "__DIR__ . '/course-template.php'";
    
    $phpArray = var_export([
        'title' => $title,
        'category' => $category,
        'category_url' => '../courses.php',
        'features' => $PLACEHOLDERS['features'],
        'prerequisites' => $PLACEHOLDERS['prerequisites'],
        'duration' => $PLACEHOLDERS['duration'],
        'price' => $PLACEHOLDERS['price'],
        'hero_image' => $heroImage,
        'highlights' => $PLACEHOLDERS['highlights'],
        'rating' => $PLACEHOLDERS['rating'],
        'enrolled' => $PLACEHOLDERS['enrolled'],
        'syllabus' => $PLACEHOLDERS['syllabus'],
        'faqs' => $PLACEHOLDERS['faqs'],
        'reviews' => $PLACEHOLDERS['reviews'],
    ], true);
    
    $content = <<<PHP
<?php
/**
 * {$title}
 * Auto-generated on: {$GLOBALS['timestamp']}
 */

\$courseData = {$phpArray};

require_once {$requirePath};
PHP;
    
    if (file_put_contents($filePath, $content) !== false) {
        echo "OK\n";
        $converted++;
    } else {
        echo "FAILED\n";
        $failed++;
    }
}

echo "\n=== COMPLETE ===\n";
echo "Converted: $converted\n";
echo "Failed: $failed\n";
