<?php
/**
 * Çözüm Merkezi - Web Scraper & Image Compressor Script
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(300);

$baseUrl = 'https://printerservis.net/';
$imagesDir = __DIR__ . '/../public/assets/images';
if (!is_dir($imagesDir)) {
    mkdir($imagesDir, 0777, true);
}

// Helper to convert images to WebP and compress
function compressToWebP($sourceUrl, $destDir) {
    global $baseUrl;
    $filename = basename(parse_url($sourceUrl, PHP_URL_PATH));
    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
    
    // Clean filename
    $cleanName = preg_replace('/[^a-zA-Z0-9_\-]/', '', pathinfo($filename, PATHINFO_FILENAME));
    $webpFilename = $cleanName . '.webp';
    $destPath = $destDir . '/' . $webpFilename;

    if (file_exists($destPath)) {
        return 'assets/images/' . $webpFilename;
    }

    // Download file
    $imgData = @file_get_contents($sourceUrl);
    if (!$imgData) {
        // Try relative URL path correction
        if (strpos($sourceUrl, 'http') !== 0) {
            $sourceUrl = rtrim($baseUrl, '/') . '/' . ltrim($sourceUrl, '/');
            $imgData = @file_get_contents($sourceUrl);
        }
    }

    if (!$imgData) return $sourceUrl; // Fallback to original URL if download fails

    $tempFile = tempnam(sys_get_temp_dir(), 'img');
    file_put_contents($tempFile, $imgData);

    $img = null;
    if ($ext === 'png') {
        $img = @imagecreatefrompng($tempFile);
    } elseif ($ext === 'jpg' || $ext === 'jpeg') {
        $img = @imagecreatefromjpeg($tempFile);
    } elseif ($ext === 'gif') {
        $img = @imagecreatefromgif($tempFile);
    }

    if ($img) {
        // Enable alpha blending and save alpha channel for transparent PNGs
        imagealphablending($img, true);
        imagesavealpha($img, true);
        
        // Save as WebP with 80% quality
        imagewebp($img, $destPath, 80);
        imagedestroy($img);
        unlink($tempFile);
        return 'assets/images/' . $webpFilename;
    }

    // Fallback: copy original if conversion fails
    unlink($tempFile);
    return $sourceUrl;
}

// Helper to clean Turkish encodings
function cleanEncoding($string) {
    if (!mb_check_encoding($string, 'UTF-8')) {
        return mb_convert_encoding($string, 'UTF-8', 'Windows-1254');
    }
    return $string;
}

function slugify($text) {
    $find =    ['Ç', 'Ş', 'Ğ', 'Ü', 'İ', 'Ö', 'ç', 'ş', 'ğ', 'ü', 'ı', 'ö'];
    $replace = ['c', 's', 'g', 'u', 'i', 'o', 'c', 's', 'g', 'u', 'i', 'o'];
    $text = str_replace($find, $replace, $text);
    $text = preg_replace('~[^\pL\d]+~u', '-', $text);
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    $text = preg_replace('~[^-\w]+~', '', $text);
    $text = trim($text, '-');
    $text = preg_replace('~-+~', '-', $text);
    $text = strtolower($text);
    return empty($text) ? 'n-a' : $text;
}

echo "Starting crawler...\n";

// 1. Fetch homepage to get links
$homepageHtml = file_get_contents($baseUrl);
$homepageHtml = cleanEncoding($homepageHtml);

// Extract all links
preg_match_all('/href="([^"]+\.php)"/i', $homepageHtml, $matches);
$links = array_unique($matches[1]);

$articles = [];
$compatibilities = [];

// Helper to extract nested div content reliably
function extractTagContent($html, $class = 'ps-article') {
    if (!preg_match('/<div[^>]+class="' . preg_quote($class, '/') . '"[^>]*>/i', $html, $m, PREG_OFFSET_CAPTURE)) {
        return null;
    }
    
    $startTag = $m[0][0];
    $startPos = $m[0][1];
    
    $contentStart = $startPos + strlen($startTag);
    $length = strlen($html);
    $depth = 1;
    $pos = $contentStart;
    
    while ($pos < $length && $depth > 0) {
        $nextOpen = strpos($html, '<div', $pos);
        $nextClose = strpos($html, '</div', $pos);
        
        if ($nextClose === false) {
            break;
        }
        
        if ($nextOpen !== false && $nextOpen < $nextClose) {
            $depth++;
            $pos = $nextOpen + 4;
        } else {
            $depth--;
            $pos = $nextClose + 6;
        }
    }
    
    return substr($html, $contentStart, $pos - $contentStart - 6);
}

// Fallback helper for older WYSIWYG pages
function getWbTextContent($html) {
    preg_match_all('/<div id="(wb_Text\d+)" style="([^"]*)">(.*?)<\/div>/is', $html, $matches);
    $contentParts = [];
    foreach ($matches[1] as $idx => $id) {
        $style = $matches[2][$idx];
        if (preg_match('/top:\s*(\d+)px/i', $style, $topMatch)) {
            $top = (int)$topMatch[1];
            if ($top >= 280 && $top < 1900) {
                $contentParts[$top] = $matches[3][$idx];
            }
        }
    }
    ksort($contentParts);
    return implode("\n", $contentParts);
}

$skipPages = ['index.php', 'contact.php', 'about.php', 'privacy.php'];

foreach ($links as $link) {
    // Clean relative path characters like ./
    $linkClean = ltrim($link, './');
    if (in_array($linkClean, $skipPages) || empty($linkClean)) {
        continue;
    }

    $pageUrl = $baseUrl . $linkClean;
    echo "Crawling: $pageUrl\n";
    
    $pageHtml = @file_get_contents($pageUrl);
    if (!$pageHtml) continue;
    
    $pageHtml = cleanEncoding($pageHtml);

    // Check if it is a compatibility table page (has <table> and no ps-article)
    if (strpos($pageHtml, '<table') !== false && strpos($pageHtml, 'ps-article') === false) {
        echo "Found Compatibility Table: $linkClean\n";
        
        // Extract Title
        preg_match('/<title>(.*?)<\/title>/is', $pageHtml, $titleMatch);
        $title = isset($titleMatch[1]) ? trim(str_replace('- printerservis', '', $titleMatch[1])) : 'Uyumluluk Tablosu';
        $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');

        // Extract Table
        if (preg_match('/<table[^>]*>(.*?)<\/table>/is', $pageHtml, $tableMatch)) {
            $tableContent = $tableMatch[1];
            
            // Extract headers
            $headers = [];
            if (preg_match('/<tr[^>]*>(.*?)<\/tr>/is', $tableContent, $firstRow)) {
                preg_match_all('/<t[hd][^>]*>(.*?)<\/t[hd]>/is', $firstRow[1], $headerCells);
                foreach ($headerCells[1] as $cell) {
                    $headers[] = html_entity_decode(trim(strip_tags($cell)), ENT_QUOTES, 'UTF-8');
                }
            }

            // Extract rows
            $rows = [];
            preg_match_all('/<tr[^>]*>(.*?)<\/tr>/is', $tableContent, $allRows);
            // Skip first row which is headers
            for ($i = 1; $i < count($allRows[1]); $i++) {
                preg_match_all('/<td[^>]*>(.*?)<\/td>/is', $allRows[1][$i], $rowCells);
                $row = [];
                foreach ($rowCells[1] as $cell) {
                    $row[] = html_entity_decode(trim(strip_tags($cell)), ENT_QUOTES, 'UTF-8');
                }
                if (!empty($row)) {
                    $rows[] = $row;
                }
            }

            $compatibilities[] = [
                'slug' => pathinfo($linkClean, PATHINFO_FILENAME),
                'title' => $title,
                'headers' => $headers,
                'rows' => $rows
            ];
        }
        continue;
    }

    // Otherwise, treat as Article
    $articleContent = extractTagContent($pageHtml, 'ps-article');
    if (!$articleContent) {
        $articleContent = getWbTextContent($pageHtml);
    }
    
    if ($articleContent) {
        echo "Found Article: $linkClean\n";
        // Clean and process images inside content
        preg_match_all('/<img[^>]+src="([^"]+)"/i', $articleContent, $images);
        foreach (array_unique($images[1]) as $imgSrc) {
            echo "  Found Image: $imgSrc\n";
            $localWebpUrl = compressToWebP($imgSrc, $imagesDir);
            $articleContent = str_replace($imgSrc, BASE_URL . '/' . $localWebpUrl, $articleContent);
        }

        // Extract Title
        preg_match('/<title>(.*?)<\/title>/is', $pageHtml, $titleMatch);
        $title = isset($titleMatch[1]) ? trim(str_replace('- printerservis', '', $titleMatch[1])) : 'Yazıcı Çözümü';
        $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
        $articleContent = html_entity_decode($articleContent, ENT_QUOTES, 'UTF-8');

        // Detect brand
        $brand_slug = 'diger';
        $brandsMap = ['hp', 'samsung', 'canon', 'oki', 'brother', 'epson', 'kyocera', 'lexmark', 'xerox', 'ricoh', 'panasonic'];
        $titleLower = mb_strtolower($title, 'UTF-8');
        foreach ($brandsMap as $b) {
            if (strpos($titleLower, $b) !== false) {
                $brand_slug = $b;
                break;
            }
        }

        // Detect category
        $category_slug = 'yazilim-ve-suruculer';
        if (strpos(strtolower($linkClean), 'reset') !== false || strpos($titleLower, 'reset') !== false) {
            $category_slug = 'reset-programlari';
        } elseif (strpos(strtolower($linkClean), 'error') !== false || strpos($titleLower, 'hata') !== false || strpos(strtolower($linkClean), 'hata') !== false) {
            $category_slug = 'hata-kodlari';
        } elseif (strpos(strtolower($linkClean), 'manual') !== false) {
            $category_slug = 'service-manuals';
        }

        // Extract Download Link if any
        $downloadUrl = '';
        $fileSize = '';
        if (preg_match('/href="([^"]+\.(?:zip|rar|exe|pdf))"/i', $pageHtml, $dlMatch)) {
            $downloadUrl = $dlMatch[1];
            if (strpos($downloadUrl, 'http') !== 0) {
                $downloadUrl = $baseUrl . $downloadUrl;
            }
            $fileSize = 'Bilinmiyor';
        }

        $articles[] = [
            'id' => 'art_' . uniqid(),
            'title' => $title,
            'slug' => pathinfo($linkClean, PATHINFO_FILENAME),
            'content' => trim($articleContent),
            'download_url' => $downloadUrl,
            'file_size' => $fileSize,
            'view_count' => rand(100, 1500),
            'is_featured' => (count($articles) < 6) ? 1 : 0, // Feature first 6
            'category_slug' => $category_slug,
            'brand_slug' => $brand_slug,
            'created_at' => date('Y-m-d H:i:s', strtotime('-' . rand(1, 30) . ' days'))
        ];
    }
}

// Recursive UTF-8 cleaner to prevent json_encode errors
function cleanUtf8Recursive($data) {
    if (is_array($data)) {
        foreach ($data as $key => $value) {
            $data[$key] = cleanUtf8Recursive($value);
        }
    } elseif (is_string($data)) {
        return mb_convert_encoding($data, 'UTF-8', 'UTF-8');
    }
    return $data;
}

// 2. Save data to JSON files
if (!empty($articles)) {
    $articles = cleanUtf8Recursive($articles);
    $json = json_encode($articles, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
    if ($json === false) {
        echo "JSON Encode Error for articles: " . json_last_error_msg() . "\n";
    } else {
        file_put_contents(__DIR__ . '/../data/articles.json', $json);
        echo "Saved " . count($articles) . " articles to data/articles.json\n";
    }
}

if (!empty($compatibilities)) {
    $compatibilities = cleanUtf8Recursive($compatibilities);
    $json = json_encode($compatibilities, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
    if ($json === false) {
        echo "JSON Encode Error for compatibilities: " . json_last_error_msg() . "\n";
    } else {
        file_put_contents(__DIR__ . '/../data/compatibility.json', $json);
        echo "Saved " . count($compatibilities) . " compatibility tables to data/compatibility.json\n";
    }
}

echo "Crawler finished successfully!\n";
