<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( ! class_exists( 'Reactify_React_Post' ) ):

    class Reactify_React_Post {

        function __construct() {
            add_filter( 'the_content', array( $this, 'wp_react_content' ) );
        }

        function wp_react_content( $content ) {
            if ( is_singular() && in_the_loop() ) {
                $get_post_id = get_the_ID();

                $disable_reactions = get_post_meta( $get_post_id, 'reactify_disable_reactions', true );
                if ( $disable_reactions === '1' ) {
                    return $content; // Return original content without reactions
                }

                $user_id = get_current_user_id();
                global $wpdb;
                $table_name = $wpdb->prefix . 'reactify';

                // Fetch counts
                $emojis = get_option( 'reactify_custom_emojis', ['like', 'love', 'happy', 'haha', 'sad', 'angry'] );

                $counts = [];
                foreach ( $emojis as $emoji ) {
                    $count = $wpdb->get_var( $wpdb->prepare(
                        "SELECT COUNT(*) FROM $table_name WHERE post_id = %d AND emoji_type = %s",
                        $get_post_id,
                        $emoji
                    ) );
                    $counts[$emoji] = $count ? $count : 0;
                }

                // Percentage Count
                $total_reactions = array_sum( $counts );
                $percentages = [];
                foreach ( $counts as $emoji => $count ) {
                    $percentages[$emoji] = $total_reactions > 0 ? round( ( $count / $total_reactions ) * 100 ) : 0;
                }

                $total_reactions = array_sum( $counts );
                $counts_copy = [];
                foreach ( $counts as $emoji => $count ) {
                    $counts_copy[$emoji] = $count;
                }

                // Check if user has reacted
                $user_reactions = [];
                if ( $user_id ) {
                    foreach ( $emojis as $emoji ) {
                        $reaction = $wpdb->get_var( $wpdb->prepare(
                            "SELECT COUNT(*) FROM $table_name WHERE post_id = %d AND emoji_type = %s AND user_id = %d",
                            $get_post_id,
                            $emoji,
                            $user_id
                        ) );
                        if ( $reaction > 0 ) {
                            $user_reactions[$emoji] = true;
                        }
                    }
                } else {
                    $cookie_name = 'wp_post_reaction_' . $get_post_id;
                    if ( isset( $_COOKIE[$cookie_name] ) ) {
                        $cookie_value = sanitize_text_field( wp_unslash( $_COOKIE[$cookie_name] ) );
                        foreach ( $emojis as $emoji ) {
                            $reaction = $wpdb->get_var( $wpdb->prepare(
                                "SELECT COUNT(*) FROM $table_name WHERE post_id = %d AND emoji_type = %s AND cookie_value = %s",
                                $get_post_id,
                                $emoji,
                                $cookie_value
                            ) );
                            if ( $reaction > 0 ) {
                                $user_reactions[$emoji] = true;
                            }
                        }
                    }
                }

                ob_start();

                $reaction_title = get_option( 'reactify_reaction_title', 'What do you feel about this post?' );
                $reaction_title_enabled = get_option( 'reactify_reaction_title_enable', 'yes' );
                $reaction_value = get_option( 'reactify_selected_reaction_type', 'percentage' );

?>
                <div class="reactify-react-container" data-post-id="<?php echo esc_attr( $get_post_id ); ?>">
                    <?php if ( $reaction_title_enabled === 'yes' ): ?>
                        <h2 class="reactify-react-block-title"><?php echo esc_html( $reaction_title ); ?></h2>
                    <?php endif; ?>
                    <div class="reactify-react-emoji-wrapper">
                        <?php foreach ( $emojis as $emoji ): ?>
                            <div class="reactify-react-emoji">
                                <?php if ( $reaction_value == 'percentage' ) { ?>
                                    <div class="reactify-react-percentage"><?php echo esc_html( $percentages[$emoji] ); ?>%</div>
                                <?php } else { ?>
                                    <div class="reactify-react-count"><?php echo esc_html( $counts_copy[$emoji] ); ?></div>
                                <?php } ?>
                                <div class="reactify-emoji <?php echo isset( $user_reactions[$emoji] ) ? 'emoji-reacted' : ''; ?>"
                                    data-emoji="<?php echo esc_attr( $emoji ); ?>" data-count="<?php echo esc_attr( $counts[$emoji] ); ?>">
                                    <img
                                        src="<?php echo esc_url( REACTIFY_BLOCK_INCLUDES_URL . '/assets/images/emoji-' . sanitize_file_name( $emoji ) . '.png' ); ?>"
                                        alt="<?php echo esc_attr( $emoji ); ?>">
                                </div>

                                <h4 class="reactify-emoji-name"><?php echo esc_html( ucfirst( $emoji ) ); ?></h4>
                            </div>
                        <?php endforeach; ?>
                    </div><!-- reactify-react-emoji-wrapper -->
                </div><!-- reactify-react-container -->
<?php
                $content .= ob_get_clean();
            }

            return $content;
        }
    }

    $reactify_react_post = new Reactify_React_Post();

endif;