<?php
$pdo = require 'config/database.php';

function calculateBlogScore($title, $description, $focusKeyword, $content) {
    $score = 0;
    if (strlen($title) > 0 && strlen($title) <= 60) $score += 30;
    if (strlen($description) > 0 && strlen($description) <= 160) $score += 25;
    if (strlen($focusKeyword) > 0) {
        $score += 20;
        if (stripos($content, $focusKeyword) !== false) $score += 10;
    }
    if (strlen($content) > 300) $score += 15;
    return $score;
}

$posts = $pdo->query("
    SELECT p.id, p.title, p.content, s.seo_title, s.meta_description, s.focus_keyword 
    FROM blog_posts p 
    LEFT JOIN blog_seo s ON p.id = s.post_id
")->fetchAll(PDO::FETCH_ASSOC);

foreach ($posts as $p) {
    $score = calculateBlogScore($p['seo_title'] ?? '', $p['meta_description'] ?? '', $p['focus_keyword'] ?? '', $p['content'] ?? '');
    echo "Blog ID {$p['id']}: Score $score%\n";
}
