<?php
/**
 * Système de likes pour les reels
 */

// Initialiser les métadonnées de likes à la création d'un reel
add_action('save_post_wprg_reel', 'wprg_init_likes_meta');
function wprg_init_likes_meta($post_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    
    $likes = get_post_meta($post_id, '_wprg_likes', true);
    if ($likes === '') {
        update_post_meta($post_id, '_wprg_likes', 0);
    }
}

// AJAX: Récupérer le nombre de likes
add_action('wp_ajax_wprg_get_likes', 'wprg_get_likes');
add_action('wp_ajax_nopriv_wprg_get_likes', 'wprg_get_likes');
function wprg_get_likes() {
    // Vérifier le nonce
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'wprg_likes_nonce')) {
        wp_send_json_error('Security check failed');
    }
    
    $post_id = intval($_POST['post_id']);
    $likes = get_post_meta($post_id, '_wprg_likes', true) ?: 0;
    
    wp_send_json_success(array(
        'likes' => intval($likes)
    ));
}

// AJAX: Ajouter un like
add_action('wp_ajax_wprg_add_like', 'wprg_add_like');
add_action('wp_ajax_nopriv_wprg_add_like', 'wprg_add_like');
function wprg_add_like() {
    // Vérifier le nonce
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'wprg_likes_nonce')) {
        wp_send_json_error('Security check failed');
    }
    
    $post_id = intval($_POST['post_id']);
    
    // Vérifier si le post existe
    if (!get_post_status($post_id)) {
        wp_send_json_error('Reel non trouvé');
    }
    
    // Récupérer l'IP pour éviter les likes multiples
    $ip = $_SERVER['REMOTE_ADDR'];
    $liked_ips = get_post_meta($post_id, '_wprg_liked_ips', true) ?: array();
    
    // Vérifier si cette IP a déjà liké
    $has_liked = in_array($ip, $liked_ips);
    
    $current_likes = get_post_meta($post_id, '_wprg_likes', true) ?: 0;
    
    if (!$has_liked) {
        // Ajouter le like
        $new_likes = $current_likes + 1;
        update_post_meta($post_id, '_wprg_likes', $new_likes);
        
        // Enregistrer l'IP
        $liked_ips[] = $ip;
        update_post_meta($post_id, '_wprg_liked_ips', $liked_ips);
        
        wp_send_json_success(array(
            'likes' => $new_likes,
            'action' => 'added'
        ));
    } else {
        // Retirer le like
        $new_likes = max(0, $current_likes - 1);
        update_post_meta($post_id, '_wprg_likes', $new_likes);
        
        // Retirer l'IP
        $liked_ips = array_diff($liked_ips, array($ip));
        update_post_meta($post_id, '_wprg_liked_ips', $liked_ips);
        
        wp_send_json_success(array(
            'likes' => $new_likes,
            'action' => 'removed'
        ));
    }
}

// AJAX: Vérifier si l'utilisateur a déjà liké
add_action('wp_ajax_wprg_check_liked', 'wprg_check_liked');
add_action('wp_ajax_nopriv_wprg_check_liked', 'wprg_check_liked');
function wprg_check_liked() {
    // Vérifier le nonce
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'wprg_likes_nonce')) {
        wp_send_json_error('Security check failed');
    }
    
    $post_id = intval($_POST['post_id']);
    $ip = $_SERVER['REMOTE_ADDR'];
    
    $liked_ips = get_post_meta($post_id, '_wprg_liked_ips', true) ?: array();
    $has_liked = in_array($ip, $liked_ips);
    
    wp_send_json_success(array(
        'liked' => $has_liked
    ));
}

// Fonction utilitaire pour obtenir le nombre de likes formaté
function wprg_get_formatted_likes($post_id) {
    $likes = get_post_meta($post_id, '_wprg_likes', true) ?: 0;
    
    if ($likes >= 1000000) {
        return round($likes / 1000000, 1) . 'M';
    } elseif ($likes >= 1000) {
        return round($likes / 1000, 1) . 'k';
    }
    
    return number_format($likes);
}