<?php
// Ajouter les styles admin
add_action('admin_head', 'wprg_admin_styles');
function wprg_admin_styles() {
    global $post_type;
    
    if ($post_type == 'wprg_reel') {
        ?>
        <style>
            /* Style général de l'admin */
            #adminmenu .wp-menu-image.dashicons-instagram {
                color: #e1306c !important;
            }
            
            /* Style des meta boxes */
            .wprg-meta-box {
                background: #fff;
                border-radius: 8px;
            }
            
            .wprg-meta-box .dashicons {
                color: #e1306c;
                margin-right: 5px;
            }
            
            /* Style des statistiques */
            .wprg-stats-box {
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                color: white;
                padding: 15px;
                border-radius: 8px;
            }
            
            .wprg-stats-box .dashicons {
                color: rgba(255,255,255,0.9);
                margin-right: 5px;
            }
            
            /* Style des boutons */
            .wprg-copy-shortcode {
                background: #e1306c !important;
                color: white !important;
                border: none !important;
            }
            
            .wprg-copy-shortcode:hover {
                background: #c13584 !important;
            }
            
            /* Style des colonnes */
            .column-shortcode {
                width: 250px;
            }
            
            .column-reels_count {
                width: 80px;
                text-align: center;
            }
            
            /* Badges */
            .wprg-badge {
                display: inline-block;
                padding: 3px 8px;
                border-radius: 12px;
                font-size: 11px;
                font-weight: 600;
                text-transform: uppercase;
            }
            
            .wprg-badge-upload {
                background: #4caf50;
                color: white;
            }
            
            .wprg-badge-youtube {
                background: #ff0000;
                color: white;
            }
            
            /* Animation de chargement */
            @keyframes wprg-pulse {
                0% { opacity: 1; }
                50% { opacity: 0.5; }
                100% { opacity: 1; }
            }
            
            .wprg-loading {
                animation: wprg-pulse 1.5s ease-in-out infinite;
            }
        </style>
        <?php
    }
}

// Ajouter des colonnes personnalisées dans la liste des reels
add_filter('manage_wprg_reel_posts_columns', 'wprg_reel_columns');
function wprg_reel_columns($columns) {
    $new_columns = array();
    
    foreach ($columns as $key => $value) {
        if ($key == 'title') {
            $new_columns[$key] = $value;
            $new_columns['wprg_thumbnail'] = '📸 Miniature';
            $new_columns['wprg_type'] = '🎬 Type';
            $new_columns['wprg_gallery'] = '📁 Galerie';
            $new_columns['wprg_views'] = '👁️ Vues';
        } else if ($key != 'date') {
            $new_columns[$key] = $value;
        } else {
            $new_columns['date'] = $value;
        }
    }
    
    return $new_columns;
}

// Remplir les colonnes personnalisées
add_action('manage_wprg_reel_posts_custom_column', 'wprg_reel_column_content', 10, 2);
function wprg_reel_column_content($column, $post_id) {
    switch ($column) {
        case 'wprg_thumbnail':
            if (has_post_thumbnail($post_id)) {
                echo get_the_post_thumbnail($post_id, array(50, 50));
            } else {
                $video_type = get_post_meta($post_id, '_wprg_video_type', true);
                if ($video_type == 'youtube') {
                    $video_url = get_post_meta($post_id, '_wprg_video_url', true);
                    $youtube_id = wprg_get_youtube_id($video_url);
                    if ($youtube_id) {
                        echo '<img src="https://img.youtube.com/vi/' . $youtube_id . '/default.jpg" width="50" height="50" style="border-radius:4px;">';
                    }
                } else {
                    echo '<div style="width:50px;height:50px;background:#f0f0f1;border-radius:4px;display:flex;align-items:center;justify-content:center;">🎬</div>';
                }
            }
            break;
            
        case 'wprg_type':
            $type = get_post_meta($post_id, '_wprg_video_type', true);
            if ($type == 'youtube') {
                echo '<span class="wprg-badge wprg-badge-youtube">YouTube</span>';
            } else {
                echo '<span class="wprg-badge wprg-badge-upload">Upload</span>';
            }
            break;
            
        case 'wprg_gallery':
            $terms = get_the_terms($post_id, 'wprg_gallery');
            if ($terms && !is_wp_error($terms)) {
                $term_links = array();
                foreach ($terms as $term) {
                    $term_links[] = '<a href="edit-tags.php?action=edit&taxonomy=wprg_gallery&tag_ID=' . $term->term_id . '">' . $term->name . '</a>';
                }
                echo implode(', ', $term_links);
            } else {
                echo '—';
            }
            break;
            
        case 'wprg_views':
            $views = get_post_meta($post_id, '_wprg_views', true) ?: 0;
            echo number_format($views);
            break;
    }
}