<?php
/**
 * Post Admin Class
 *
 * @package InsightPress
 */

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

class InsightPress_Post_Admin
{
    public function __construct()
    {
        add_filter('manage_post_posts_columns', array($this, 'add_post_analytics_column'));
        add_action('manage_post_posts_custom_column', array($this, 'render_post_analytics_column'), 10, 2);
    }

    /**
     * Add Analytics column to blog posts list.
     */
    public function add_post_analytics_column($columns)
    {
        $new_columns = array();
        foreach ($columns as $key => $column) {
            $new_columns[$key] = $column;
            if ($key === 'title') {
                $new_columns['ip_post_analytics'] = __('Engagement', 'insightpress-advanced-analytics-for-woocommerce');
            }
        }
        return $new_columns;
    }

    /**
     * Render Analytics column content for blog posts.
     */
    public function render_post_analytics_column($column, $post_id)
    {
        if ($column === 'ip_post_analytics') {
            global $wpdb;
            $table = InsightPress_Post_Tracker::get_table_name();

            $is_pro = insightpress_is_pro();

            // Basic quick stats for the column
            $views = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table WHERE post_id = %d AND event_type = 'view'", $post_id));
            $uniques = $wpdb->get_var($wpdb->prepare("SELECT COUNT(DISTINCT session_id) FROM $table WHERE post_id = %d AND event_type = 'view'", $post_id));

            $blur_style = !$is_pro ? 'filter:blur(2px);user-select:none;pointer-events:none;opacity:0.7;' : '';

            echo '<div style="position:relative;margin-bottom:8px;">';
            if (!$is_pro) {
                echo '<div style="position:absolute;top:0;left:0;width:100%;height:100%;display:flex;align-items:center;justify-content:center;z-index:10;color:#7c3aed;font-size:9px;font-weight:800;text-transform:uppercase;letter-spacing:1px;pointer-events:none;">';
                echo '<span class="dashicons dashicons-lock" style="font-size:11px;width:11px;height:11px;margin-right:3px;"></span> PRO';
                echo '</div>';
            }

            echo '<div class="insightpress-post-stats" style="' . $blur_style . '">';
            echo '<div><span class="dashicons dashicons-visibility" style="font-size: 14px; width: 14px; height: 14px; margin-right: 4px; vertical-align: middle; opacity: 0.7;"></span><strong>' . __('Total Views:', 'insightpress-advanced-analytics-for-woocommerce') . '</strong> ' . number_format((int) $views) . '</div>';
            echo '<div style="margin-top: 2px;"><span class="dashicons dashicons-groups" style="font-size: 14px; width: 14px; height: 14px; margin-right: 4px; vertical-align: middle; opacity: 0.7;"></span><strong>' . __('Unique Visitors:', 'insightpress-advanced-analytics-for-woocommerce') . '</strong> ' . number_format((int) $uniques) . '</div>';
            echo '</div>';
            echo '</div>';

            $url = admin_url('admin.php?page=insightpress#/post/' . $post_id);
            echo '<a href="' . esc_url($url) . '" class="insightpress-analytics-button" style="background: #007cba; border-radius: 6px; box-shadow: 0 2px 4px rgba(0,124,186,0.2);">';
            echo '<span class="dashicons dashicons-chart-area"></span> ' . __('View Analytics', 'insightpress-advanced-analytics-for-woocommerce');
            echo '</a>';
        }
    }
}
