<?php
// includes/meta-boxes.php

/**
 * Gestão de Widgets do Painel de Administração (Dashboard).
 * Substitui as antigas Meta Boxes de rifas por widgets úteis para a gestão da 1MZ Records.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Acesso direto bloqueado
}

/**
 * Regista os widgets no Dashboard do WordPress.
 */
function onemz_register_dashboard_widgets() {
    // Apenas para administradores
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    wp_add_dashboard_widget(
        'onemz_admin_dashboard_widget',         // ID do Widget
        '🎹 1MZ Records - Estado Geral',        // Título
        'onemz_render_dashboard_widget'         // Função de Callback
    );
}
add_action( 'wp_dashboard_setup', 'onemz_register_dashboard_widgets' );


/**
 * Renderiza o conteúdo do widget no Dashboard.
 * Mostra estatísticas rápidas e as últimas submissões pendentes.
 */
function onemz_render_dashboard_widget() {
    global $wpdb;
    
    // Tabelas
    $table_submissions = $wpdb->prefix . 'rmbz_artist_submissions';
    $table_projects    = $wpdb->prefix . 'rmbz_artist_projects';
    $table_wallets     = $wpdb->prefix . 'rmbz_wallets';

    // 1. Contagens Gerais
    $total_pendentes = (int) $wpdb->get_var( "SELECT COUNT(id) FROM {$table_submissions} WHERE review_status = 'pendente'" );
    $total_projetos  = (int) $wpdb->get_var( "SELECT COUNT(id) FROM {$table_projects} WHERE production_status != 'done'" );
    $total_artistas  = (int) $wpdb->get_var( "SELECT COUNT(id) FROM {$table_wallets}" );

    // 2. Últimas 5 Submissões Pendentes
    $latest_subs = $wpdb->get_results( 
        "SELECT * FROM {$table_submissions} WHERE review_status = 'pendente' ORDER BY submission_date DESC LIMIT 5" 
    );

    ?>
    <div class="onemz-dashboard-widget">
        <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 20px; text-align: center;">
            <div style="background: #f0f0f1; padding: 15px; border-radius: 5px; border: 1px solid #ccc;">
                <strong style="font-size: 24px; color: #e67e22; display: block;"><?php echo $total_pendentes; ?></strong>
                <span style="font-size: 12px; color: #666; text-transform: uppercase;">Para Avaliar</span>
            </div>
            <div style="background: #f0f0f1; padding: 15px; border-radius: 5px; border: 1px solid #ccc;">
                <strong style="font-size: 24px; color: #2ecc71; display: block;"><?php echo $total_projetos; ?></strong>
                <span style="font-size: 12px; color: #666; text-transform: uppercase;">Projetos Ativos</span>
            </div>
            <div style="background: #f0f0f1; padding: 15px; border-radius: 5px; border: 1px solid #ccc;">
                <strong style="font-size: 24px; color: #3498db; display: block;"><?php echo $total_artistas; ?></strong>
                <span style="font-size: 12px; color: #666; text-transform: uppercase;">Artistas</span>
            </div>
        </div>

        <h4 style="margin-bottom: 10px; border-bottom: 1px solid #eee; padding-bottom: 5px;">Músicas Aguardando Aprovação</h4>
        
        <?php if ( empty( $latest_subs ) ) : ?>
            <p style="color: #999;">Tudo limpo! Nenhuma submissão pendente.</p>
        <?php else : ?>
            <ul style="list-style: none; margin: 0; padding: 0;">
                <?php foreach ( $latest_subs as $sub ) : 
                    $data_fmt = date_i18n( 'd/m H:i', strtotime( $sub->submission_date ) );
                ?>
                    <li style="padding: 8px 0; border-bottom: 1px solid #f0f0f0; display: flex; justify-content: space-between; align-items: center;">
                        <div>
                            <strong><?php echo esc_html( $sub->music_title ); ?></strong>
                            <br>
                            <small style="color: #888;"><?php echo esc_html( $sub->music_genre ); ?> • <?php echo $data_fmt; ?></small>
                        </div>
                        <div>
                            <a href="<?php echo admin_url( 'admin.php?page=rmbz-audicoes' ); ?>" class="button button-small">Avaliar</a>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>

        <div style="margin-top: 15px; text-align: right;">
            <a href="<?php echo admin_url( 'admin.php?page=rmbz-audicoes' ); ?>" style="text-decoration: none;">Ver todas as audições &rarr;</a>
        </div>
    </div>
    <?php
}