<?php
/**
 * Image Boost — Media Library Integration
 * Adds optimization column to the Media Library list view.
 */

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

class ImgBoost_Media_Library {

    /**
     * Initialize Media Library hooks.
     */
    public static function init() {
        add_filter('manage_media_columns', [__CLASS__, 'add_column']);
        add_action('manage_media_custom_column', [__CLASS__, 'render_column'], 10, 2);
        add_action('admin_enqueue_scripts', [__CLASS__, 'column_styles']);
    }

    /**
     * Add custom column header.
     */
    public static function add_column($columns) {
        $columns['imgboost'] = __('Image Boost', 'image-boost');
        return $columns;
    }

    /**
     * Render column content for each attachment.
     */
    public static function render_column($column_name, $attachment_id) {
        if ($column_name !== 'imgboost') {
            return;
        }

        $mime = get_post_mime_type($attachment_id);
        $supported = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];

        if (!in_array($mime, $supported)) {
            echo '<span class="imgboost-na">—</span>';
            return;
        }

        $status = get_post_meta($attachment_id, '_imgboost_status', true);
        $level = get_post_meta($attachment_id, '_imgboost_level', true);
        $savings = get_post_meta($attachment_id, '_imgboost_savings', true);
        $original = get_post_meta($attachment_id, '_imgboost_original_size', true);
        $optimized = get_post_meta($attachment_id, '_imgboost_optimized_size', true);
        $has_backup = get_post_meta($attachment_id, '_imgboost_has_backup', true);

        ?>
        <div class="imgboost-col" data-id="<?php echo esc_attr($attachment_id); ?>">
        <?php
        if ($status === 'optimized') {
            $level_labels = [
                'normal' => '🟢 Normal',
                'pro'    => '🔵 Pro',
                'ultra'  => '🟣 Ultra',
            ];
            $level_label = isset($level_labels[$level]) ? $level_labels[$level] : $level;
            ?>
            <span class="imgboost-badge imgboost-optimized">✅ <?php esc_html_e('Optimized', 'image-boost'); ?></span>
            <div class="imgboost-details">
                <span class="imgboost-savings"><?php echo esc_html($savings); ?>% saved</span>
                <span class="imgboost-level"><?php echo esc_html($level_label); ?></span>
                <?php if ($original && $optimized): ?>
                    <span class="imgboost-sizes"><?php echo esc_html(imgboost_format_bytes($original)) . ' &rarr; ' . esc_html(imgboost_format_bytes($optimized)); ?></span>
                <?php endif; ?>
                <?php if ($has_backup): ?>
                    <button type="button" class="button button-small imgboost-restore-btn" data-id="<?php echo esc_attr($attachment_id); ?>">
                        &#x21A9; <?php esc_html_e('Restore', 'image-boost'); ?>
                    </button>
                <?php endif; ?>
            </div>
            <?php
        } elseif ($status === 'error') {
            $error = get_post_meta($attachment_id, '_imgboost_error', true);
            ?>
            <span class="imgboost-badge imgboost-error">&#x274C; <?php esc_html_e('Error', 'image-boost'); ?></span>
            <?php if ($error): ?>
                <small class="imgboost-error-msg"><?php echo esc_html($error); ?></small>
            <?php endif; ?>
            <button type="button" class="button button-small imgboost-optimize-btn" data-id="<?php echo esc_attr($attachment_id); ?>">
                &#x1F504; <?php esc_html_e('Retry', 'image-boost'); ?>
            </button>
            <?php
        } else {
            ?>
            <span class="imgboost-badge imgboost-pending">&#x23F3; <?php esc_html_e('Not Optimized', 'image-boost'); ?></span>
            <button type="button" class="button button-small imgboost-optimize-btn" data-id="<?php echo esc_attr($attachment_id); ?>">
                &#x26A1; <?php esc_html_e('Optimize', 'image-boost'); ?>
            </button>
            <?php
        }
        ?>
        </div>
        <?php
    }

    /**
     * Column styles.
     */
    public static function column_styles() {
        $screen = get_current_screen();
        if (!$screen || $screen->id !== 'upload') {
            return;
        }
        wp_add_inline_style('list-tables', '.column-imgboost_status { width: 15%; }');
    }
}
