<?php
if (!defined('ABSPATH')) exit;

class RANKFORGE_Content_Scorer {
    private static $instance = null;
    public static function instance() { if (!self::$instance) self::$instance = new self(); return self::$instance; }

    private function __construct() {
        add_filter('manage_posts_columns', [$this, 'add_score_column']);
        add_filter('manage_pages_columns', [$this, 'add_score_column']);
        add_action('manage_posts_custom_column', [$this, 'render_score_column'], 10, 2);
        add_action('manage_pages_custom_column', [$this, 'render_score_column'], 10, 2);
    }

    public function add_score_column($columns) {
        $columns['rankforge_score'] = __('SEO', 'rankforge');
        return $columns;
    }

    public function render_score_column($column, $post_id) {
        if ($column !== 'rankforge_score') return;

        $cached = RANKFORGE_Analyzer::instance()->get_cached($post_id);
        $score = $cached ? (int) $cached->score : 0;

        if (!$cached) {
            echo '<span style="color:#94a3b8;">—</span>';
            return;
        }

        $color = $score >= 80 ? '#059669' : ($score >= 50 ? '#D97706' : '#DC2626');
        $bg = $score >= 80 ? '#ECFDF5' : ($score >= 50 ? '#FFFBEB' : '#FEF2F2');

        printf(
            '<span style="display:inline-block;padding:2px 8px;border-radius:4px;font-weight:700;font-size:14px;color:%s;background:%s;">%d</span>',
            esc_attr( $color ), esc_attr( $bg ), (int) $score
        );
    }
}
