<?php
// Filename: cron_news.php
// Version: 3.0 (Robust Scraping)

header('Content-Type: text/plain');
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Ensure cache directory
$cache_dir = __DIR__ . '/cache';
if (!is_dir($cache_dir)) { mkdir($cache_dir, 0755, true); }

function fetchMoeNewsFinal() {
    $url = "https://www.moe.gov.my/pemberitahuan/pengumuman";
    
    echo "1. Connecting to: $url ...\n";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    // Use a very standard browser User Agent to avoid blocking
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    
    $html = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($html === false || $httpCode != 200) {
        echo "FAIL: HTTP $httpCode or Connection Error\n";
        return false;
    }

    echo "2. HTML fetched (" . strlen($html) . " bytes).\n";

    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    // Force UTF-8 loading
    $dom->loadHTML('<?xml encoding="UTF-8">' . $html);
    libxml_clear_errors();

    $xpath = new DOMXPath($dom);
    $news_items = [];

    // --- STRATEGY 3: Generic Content Area ---
    // The previous table-based XPaths failed. 
    // Now we look for the main content block and find ALL links inside it.
    // Joomla usually uses 'item-page' or 'blog' or 'k2ItemsBlock'.
    // We will look for links that are NOT navigation links (e.g., have length > 10 chars).
    
    // Attempt 1: Table rows (Common in MOE archives)
    $nodes = $xpath->query("//table//tr//a");
    
    // Attempt 2: If table fails, look for list items or headers
    if ($nodes->length == 0) {
        echo "   (Table strategy failed, trying generic links...)\n";
        $nodes = $xpath->query("//div[contains(@class,'item-page')]//a | //div[contains(@class,'blog')]//a");
    }

    echo "3. Found " . $nodes->length . " raw links. Filtering...\n";

    $count = 0;
    $seen_titles = [];

    foreach ($nodes as $node) {
        if ($count >= 15) break;

        $title = trim($node->nodeValue);
        $href = trim($node->getAttribute('href'));

        // Skip empty or short titles (navigation links)
        if (empty($title) || strlen($title) < 10) continue;
        
        // Skip duplicate titles
        if (in_array($title, $seen_titles)) continue;

        // Skip "Read More" or "Lihat Semua" links
        if (stripos($title, 'Lihat Semua') !== false || stripos($title, 'Read More') !== false) continue;

        // Fix Relative URLs
        if (strpos($href, 'http') === false) {
            $href = "https://www.moe.gov.my" . (strpos($href, '/') === 0 ? '' : '/') . $href;
        }

        // Add to list
        $seen_titles[] = $title;
        $news_items[] = [
            'title' => $title,
            'link' => $href,
            'date' => date('d M Y') // Fallback date
        ];
        $count++;
    }

    return $news_items;
}

// --- RUN ---
$data = fetchMoeNewsFinal();

if ($data !== false && !empty($data)) {
    // Save to JSON
    $save_result = file_put_contents($cache_dir . '/moe_news.json', json_encode([
        'last_updated' => date('d M Y, h:i A'),
        'items' => $data
    ]));

    if ($save_result) {
        echo "\nSUCCESS: Saved " . count($data) . " news items.\n";
        echo "1. " . $data[0]['title'] . "\n"; // Show first item as proof
    } else {
        echo "\nFAIL: Could not write file. Check permissions for /cache folder.\n";
    }
} else {
    echo "\nFAIL: No valid news items found in HTML.\n";
    echo "Check if MOE website structure has changed completely.\n";
}
?>