<?php
/**
 * Name: Content Report Widget
 * Description: Dashboard widget showing detailed content statistics including post counts, page totals, comment numbers, and publish activity for quick overview and analytics monitoring
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Editorial Calendar Overview",
 *     "description": "See at-a-glance post counts by status (draft, pending, scheduled, published) to track content pipeline without navigating away from dashboard."
 *   },
 *   {
 *     "title": "Multi-Author Blog Management",
 *     "description": "Quickly identify how many posts each custom post type has to ensure balanced content across blog, tutorials, and case studies sections."
 *   },
 *   {
 *     "title": "Client Reporting Dashboard",
 *     "description": "Display content statistics widget for clients to see their site's content volume without technical complexity."
 *   }
 * ]
 * Added: 2025-09-22
 * Tags: dashboard, widget, statistics, content, reports, analytics
 * WhereToFind: {
 *   "location": "Dashboard Widget",
 *   "url": "admin.php",
 *   "instructions": "View the Content Report widget showing statistics",
 *   "newTab": false
 * }
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardContentReportWidget {
    
    public function __construct() {
        add_action('wp_dashboard_setup', [$this, 'add_dashboard_widget']);
    }
    
    public function add_dashboard_widget() {
        wp_add_dashboard_widget(
            'switchboard_content_report',
            'Content Report <span class="switchboard-badge">SWITCHBOARD</span>',
            [$this, 'render_widget']
        );
    }
    
    public function render_widget() {
        // Get content statistics
        $post_counts = wp_count_posts('post');
        $page_counts = wp_count_posts('page');
        $media_counts = wp_count_posts('attachment');
        
        // Get comment count
        $comment_count = wp_count_comments();
        
        // Get user count
        $user_count = count_users();
        
        // Get custom post types
        $custom_post_types = get_post_types(['public' => true, '_builtin' => false], 'objects');
        
        // Get recent activity
        $recent_posts = wp_count_posts('post');
        $recent_pages = wp_count_posts('page');
        
        // Calculate totals with property checks
        $total_posts = (isset($post_counts->publish) ? $post_counts->publish : 0) +
                       (isset($post_counts->draft) ? $post_counts->draft : 0) +
                       (isset($post_counts->pending) ? $post_counts->pending : 0) +
                       (isset($post_counts->future) ? $post_counts->future : 0);
        $total_pages = (isset($page_counts->publish) ? $page_counts->publish : 0) +
                       (isset($page_counts->draft) ? $page_counts->draft : 0) +
                       (isset($page_counts->pending) ? $page_counts->pending : 0);
        $total_media = isset($media_counts->inherit) ? $media_counts->inherit : 0;
        
        ?>
        <style>
            .content-report-grid {
                display: grid;
                grid-template-columns: repeat(3, 1fr);
                gap: 12px;
                margin-bottom: 20px;
            }
            .content-report-card {
                background: #f8fafc;
                border: 2px solid #e2e8f0;
                color: #1e293b;
                padding: 15px;
                border-radius: 8px;
                text-align: center;
                transition: all 0.2s;
            }
            .content-report-card:hover {
                border-color: #2271b1;
                box-shadow: 0 2px 8px rgba(34, 113, 177, 0.1);
            }
            .content-report-number {
                font-size: 32px;
                font-weight: 700;
                margin: 5px 0;
            }
            .content-report-label {
                font-size: 12px;
                color: #64748b;
                text-transform: uppercase;
                letter-spacing: 0.5px;
                font-weight: 600;
            }
            .content-report-details {
                margin-top: 15px;
                padding-top: 15px;
                border-top: 1px solid #ddd;
            }
            .content-report-row {
                display: flex;
                justify-content: space-between;
                padding: 8px 0;
                border-bottom: 1px solid #f0f0f0;
            }
            .content-report-row:last-child {
                border-bottom: none;
            }
            .content-report-row-label {
                font-weight: 500;
                color: #666;
            }
            .content-report-row-value {
                font-weight: 600;
                color: #2271b1;
            }
            @media (max-width: 782px) {
                .content-report-grid {
                    grid-template-columns: repeat(2, 1fr);
                }
            }
        </style>
        
        <div class="content-report-grid">
            <div class="content-report-card">
                <div class="content-report-label">Posts</div>
                <div class="content-report-number"><?php echo esc_html(number_format($total_posts)); ?></div>
            </div>
            <div class="content-report-card">
                <div class="content-report-label">Pages</div>
                <div class="content-report-number"><?php echo esc_html(number_format($total_pages)); ?></div>
            </div>
            <div class="content-report-card">
                <div class="content-report-label">Media</div>
                <div class="content-report-number"><?php echo esc_html(number_format($total_media)); ?></div>
            </div>
            <div class="content-report-card">
                <div class="content-report-label">Comments</div>
                <div class="content-report-number"><?php echo esc_html(number_format(isset($comment_count->approved) ? $comment_count->approved : 0)); ?></div>
            </div>
            <div class="content-report-card">
                <div class="content-report-label">Users</div>
                <div class="content-report-number"><?php echo esc_html(number_format(isset($user_count['total_users']) ? $user_count['total_users'] : 0)); ?></div>
            </div>
            <div class="content-report-card">
                <div class="content-report-label">Categories</div>
                <div class="content-report-number"><?php echo esc_html(number_format(wp_count_terms(['taxonomy' => 'category']))); ?></div>
            </div>
        </div>
        
        <div class="content-report-details">
            <h4 class="sb-sp-heading-sm">Post Status Breakdown</h4>
            <div class="content-report-row">
                <span class="content-report-row-label">Published</span>
                <span class="content-report-row-value"><?php echo esc_html(number_format(isset($post_counts->publish) ? $post_counts->publish : 0)); ?></span>
            </div>
            <div class="content-report-row">
                <span class="content-report-row-label">Drafts</span>
                <span class="content-report-row-value"><?php echo esc_html(number_format(isset($post_counts->draft) ? $post_counts->draft : 0)); ?></span>
            </div>
            <div class="content-report-row">
                <span class="content-report-row-label">Scheduled</span>
                <span class="content-report-row-value"><?php echo esc_html(number_format(isset($post_counts->future) ? $post_counts->future : 0)); ?></span>
            </div>
            <div class="content-report-row">
                <span class="content-report-row-label">Pending Review</span>
                <span class="content-report-row-value"><?php echo esc_html(number_format(isset($post_counts->pending) ? $post_counts->pending : 0)); ?></span>
            </div>
        </div>
        
        <?php if (!empty($custom_post_types)): ?>
        <div class="content-report-details">
            <h4 class="sb-sp-heading-sm">Custom Post Types</h4>
            <?php foreach ($custom_post_types as $cpt):
                if (!is_object($cpt) || !isset($cpt->name, $cpt->label)) {
                    continue;
                }
                $cpt_count = wp_count_posts($cpt->name);
                $cpt_total = (isset($cpt_count->publish) ? $cpt_count->publish : 0) +
                             (isset($cpt_count->draft) ? $cpt_count->draft : 0) +
                             (isset($cpt_count->pending) ? $cpt_count->pending : 0);
            ?>
            <div class="content-report-row">
                <span class="content-report-row-label"><?php echo esc_html($cpt->label); ?></span>
                <span class="content-report-row-value"><?php echo esc_html(number_format($cpt_total)); ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <div class="content-report-details">
            <h4 class="sb-sp-heading-sm">Comment Status</h4>
            <div class="content-report-row">
                <span class="content-report-row-label">Approved</span>
                <span class="content-report-row-value"><?php echo esc_html(number_format(isset($comment_count->approved) ? $comment_count->approved : 0)); ?></span>
            </div>
            <div class="content-report-row">
                <span class="content-report-row-label">Pending</span>
                <span class="content-report-row-value"><?php echo esc_html(number_format(isset($comment_count->moderated) ? $comment_count->moderated : 0)); ?></span>
            </div>
            <div class="content-report-row">
                <span class="content-report-row-label">Spam</span>
                <span class="content-report-row-value"><?php echo esc_html(number_format(isset($comment_count->spam) ? $comment_count->spam : 0)); ?></span>
            </div>
        </div>
        <?php
    }

    /**
     * Register data locations for cleanup system
     *
     * @return array
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }
}

$switchboard_instance = new SwitchboardContentReportWidget();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
