<?php
class PC_Community_Suggestions_Shortcodes {
    
    public function __construct() {
        add_shortcode('community_suggestions', array($this, 'render_suggestions_page'));
    }
    
    public function render_suggestions_page($atts) {
        $current_suggestion = isset($_GET['suggestion']) ? intval($_GET['suggestion']) : 0;
        
        ob_start();
        
        if ($current_suggestion > 0) {
            $this->render_suggestion_detail($current_suggestion);
        } else {
            $this->render_suggestions_interface();
        }
        
        return ob_get_clean();
    }
    
    private function render_suggestion_detail($suggestion_id) {
        $suggestion = PC_Community_Suggestions_Database::get_suggestion_by_id($suggestion_id);
        $comments = $suggestion ? PC_Community_Suggestions_Database::get_comments($suggestion_id) : array();
        
        if (!$suggestion) {
            echo '<div class="pc-suggestions-card">';
            echo '<p>' . esc_html__('Suggestion not found.', 'pc-community-suggestions-7d3f') . '</p>';
            echo '<a href="' . esc_url(remove_query_arg('suggestion')) . '" class="pc-button">' . esc_html__('Back to Suggestions', 'pc-community-suggestions-7d3f') . '</a>';
            echo '</div>';
            return;
        }
        
        ?>
        <div class="pc-suggestions-container">
            <div class="pc-suggestions-header">
                <a href="<?php echo esc_url(remove_query_arg('suggestion')); ?>" class="pc-back-link">
                    ← <?php esc_html_e('Back to Suggestions', 'pc-community-suggestions-7d3f'); ?>
                </a>
            </div>
            
            <div class="pc-suggestion-detail">
                <?php $this->render_suggestion_card($suggestion, true); ?>
                
                <div class="pc-comments-section">
                    <h3><?php esc_html_e('Admin Comments', 'pc-community-suggestions-7d3f'); ?></h3>
                    
                    <?php if (empty($comments)) : ?>
                        <p class="pc-no-comments"><?php esc_html_e('No admin comments yet.', 'pc-community-suggestions-7d3f'); ?></p>
                    <?php else : ?>
                        <?php foreach ($comments as $comment) : ?>
                            <div class="pc-comment-card">
                                <div class="pc-comment-header">
                                    <span class="pc-comment-author"><?php echo esc_html($comment->display_name ?: $comment->user_login); ?></span>
                                    <span class="pc-comment-date">
                                        <?php echo date_i18n(get_option('date_format'), strtotime($comment->created_at)); ?>
                                    </span>
                                </div>
                                <div class="pc-comment-content">
                                    <?php echo wp_kses_post(wpautop($comment->comment)); ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php
    }
    
    private function render_suggestions_interface() {
        $current_page = max(1, get_query_var('paged', 1));
        $sort = isset($_GET['sort']) && in_array($_GET['sort'], ['popular', 'newest']) ? $_GET['sort'] : 'popular';
        $per_page = 10;
        
        $suggestions = PC_Community_Suggestions_Database::get_suggestions($current_page, $per_page, $sort);
        $total_suggestions = PC_Community_Suggestions_Database::get_suggestion_count();
        $total_pages = ceil($total_suggestions / $per_page);
        
        ?>
        <div class="pc-community-suggestions-container">
            <div class="pc-suggestions-header">
                <div class="pc-suggestions-controls">
                    <?php if (is_user_logged_in()) : ?>
                        <button class="pc-button pc-button-primary" onclick="pcToggleSuggestionForm()">
                            <?php esc_html_e('Add Suggestion', 'pc-community-suggestions-7d3f'); ?>
                        </button>
                    <?php endif; ?>
                    <div class="pc-sort-dropdown">
                        <select onchange="pcChangeSort(this.value)">
                            <option value="popular" <?php selected($sort, 'popular'); ?>><?php esc_html_e('Most Upvoted', 'pc-community-suggestions-7d3f'); ?></option>
                            <option value="newest" <?php selected($sort, 'newest'); ?>><?php esc_html_e('Newest', 'pc-community-suggestions-7d3f'); ?></option>
                        </select>
                    </div>
                </div>
            </div>
            
            <?php if (is_user_logged_in()) : ?>
                <div id="pc-suggestion-form" class="pc-suggestion-form" style="display: none;">
                    <div class="pc-form-card">
                        <h3><?php esc_html_e('Add Your Suggestion', 'pc-community-suggestions-7d3f'); ?></h3>
                        <form id="pc-new-suggestion-form">
                            <div class="pc-form-group">
                                <label for="pc-suggestion-title"><?php esc_html_e('Title', 'pc-community-suggestions-7d3f'); ?></label>
                                <input type="text" id="pc-suggestion-title" name="title" required maxlength="255">
                            </div>
                            <div class="pc-form-group">
                                <label for="pc-suggestion-content"><?php esc_html_e('Details', 'pc-community-suggestions-7d3f'); ?></label>
                                <textarea id="pc-suggestion-content" name="content" rows="5" required></textarea>
                            </div>
                            <div class="pc-form-actions">
                                <button type="button" class="pc-button" onclick="pcToggleSuggestionForm()">
                                    <?php esc_html_e('Cancel', 'pc-community-suggestions-7d3f'); ?>
                                </button>
                                <button type="submit" class="pc-button pc-button-primary">
                                    <?php esc_html_e('Submit Suggestion', 'pc-community-suggestions-7d3f'); ?>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            <?php endif; ?>
            
            <div class="pc-suggestions-list">
                <?php if (empty($suggestions)) : ?>
                    <div class="pc-no-suggestions">
                        <p><?php esc_html_e('No suggestions yet. Be the first to share your idea!', 'pc-community-suggestions-7d3f'); ?></p>
                    </div>
                <?php else : ?>
                    <?php foreach ($suggestions as $suggestion) : ?>
                        <?php $this->render_suggestion_card($suggestion); ?>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
            
            <?php if ($total_pages > 1) : ?>
                <div class="pc-suggestions-pagination">
                    <?php
                    echo paginate_links(array(
                        'base' => get_pagenum_link(1) . '%_%',
                        'format' => 'page/%#%/',
                        'current' => $current_page,
                        'total' => $total_pages,
                        'prev_text' => __('« Previous', 'pc-community-suggestions-7d3f'),
                        'next_text' => __('Next »', 'pc-community-suggestions-7d3f'),
                        'add_args' => array('sort' => $sort)
                    ));
                    ?>
                </div>
            <?php endif; ?>
        </div>
        <?php
    }
    
    private function render_suggestion_card($suggestion, $detail_view = false) {
        $user_has_voted = is_user_logged_in() ? PC_Community_Suggestions_Database::has_user_voted($suggestion->id, get_current_user_id()) : false;
        $vote_class = $user_has_voted ? 'pc-voted' : '';
        
        if ($detail_view) {
            $vote_button_html = '';
        } else {
            $vote_button_html = 'onclick="pcVoteSuggestion(' . esc_attr($suggestion->id) . ')"';
        }
        
        ?>
        <div class="pc-suggestion-card <?php echo $detail_view ? 'pc-suggestion-card-detail' : ''; ?>" data-suggestion-id="<?php echo esc_attr($suggestion->id); ?>">
            <div class="pc-suggestion-vote">
                <?php if (is_user_logged_in()) : ?>
                    <button class="pc-vote-button <?php echo esc_attr($vote_class); ?>" 
                            <?php echo $vote_button_html; ?>
                            <?php echo $user_has_voted || $detail_view ? 'disabled' : ''; ?>>
                        <span class="pc-vote-arrow">↑</span>
                        <span class="pc-vote-count"><?php echo esc_html($suggestion->upvotes); ?></span>
                    </button>
                <?php else : ?>
                    <div class="pc-vote-button pc-vote-disabled">
                        <span class="pc-vote-arrow">↑</span>
                        <span class="pc-vote-count"><?php echo esc_html($suggestion->upvotes); ?></span>
                    </div>
                <?php endif; ?>
            </div>
            
            <div class="pc-suggestion-content">
                <h3 class="pc-suggestion-title">
                    <?php if (!$detail_view) : ?>
                        <a href="<?php echo esc_url(add_query_arg('suggestion', $suggestion->id, get_permalink())); ?>">
                            <?php echo esc_html($suggestion->title); ?>
                        </a>
                    <?php else : ?>
                        <?php echo esc_html($suggestion->title); ?>
                    <?php endif; ?>
                </h3>
                <div class="pc-suggestion-body">
                    <?php 
                    if ($detail_view) {
                        echo wp_kses_post(wpautop($suggestion->content));
                    } else {
                        echo wp_kses_post(wpautop(wp_trim_words($suggestion->content, 30, '...')));
                    }
                    ?>
                </div>
                
                <?php if (!$detail_view) : ?>
                    <div class="pc-suggestion-actions">
                        <a href="<?php echo esc_url(add_query_arg('suggestion', $suggestion->id, get_permalink())); ?>" 
                           class="pc-button pc-button-small">
                            <?php esc_html_e('View Details', 'pc-community-suggestions-7d3f'); ?>
                        </a>
                    </div>
                <?php endif; ?>
                
                <div class="pc-suggestion-meta">
                    <span class="pc-suggestion-author">
                        <?php
                        printf(
                            __('By %s', 'pc-community-suggestions-7d3f'),
                            esc_html($suggestion->display_name ?: $suggestion->user_login)
                        );
                        ?>
                    </span>
                    <span class="pc-suggestion-date">
                        <?php echo human_time_diff(strtotime($suggestion->created_at), current_time('timestamp')) . ' ' . __('ago', 'pc-community-suggestions-7d3f'); ?>
                    </span>
                </div>
            </div>
        </div>
        <?php
    }
}