<?php

if (!defined('ABSPATH')) {
    exit;
}

class RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_SEO_Score {

    public function __construct() {

        add_filter(
            'manage_upload_columns',
            [$this, 'add_score_column']
        );

        add_action(
            'manage_media_custom_column',
            [$this, 'show_score_column'],
            10,
            2
        );
    }

    /**
     * Add SEO Score Column
     */
    public function add_score_column(
        $columns
    ) {

        $columns[
            'rizwan_image_alt_text_webp_optimizer_score'
        ] =
        esc_html__(
        'SEO Score',
        'rizwan-image-alt-text-webp-optimizer'
         );

        return $columns;
    }

    /**
     * Show SEO Score
     */
    public function show_score_column(
        $column_name,
        $attachment_id
    ) {

        if (
            $column_name !==
            'rizwan_image_alt_text_webp_optimizer_score'
        ) {
            return;
        }

        $score =
            absint(
                get_post_meta(
                    $attachment_id,
                    'rizwan_image_alt_text_webp_optimizer_score',
                    true
                )
            );

        $score = min(
            100,
            max(
                0,
                $score
            )
        );

        $color =
            '#dc2626';

        if (
            $score >= 80
        ) {

            $color =
                '#16a34a';

        } elseif (
            $score >= 50
        ) {

            $color =
                '#f59e0b';
        }

        ?>

        <strong
        style="
        color:
        <?php
        echo esc_attr(
            $color
        );
        ?>;
        font-size:14px;
        ">

        <?php
        echo esc_html(
            $score
        );
        ?>%

        </strong>

        <?php
    }
}