<?php
/**
 * FAQ Manager Public Interface
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class PC_FAQ_Manager_Public {
    
    /**
     * Constructor
     */
    public function __construct() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_public_scripts'));
        add_shortcode('pc_faq_page', array($this, 'render_faq_page'));
    }
    
    /**
     * Enqueue public scripts and styles
     */
    public function enqueue_public_scripts() {
        wp_enqueue_style('pc-faq-manager-public', PC_FAQ_MANAGER_PLUGIN_URL . 'public/css/public-style.css', array(), PC_FAQ_MANAGER_VERSION);
        wp_enqueue_script('pc-faq-manager-public', PC_FAQ_MANAGER_PLUGIN_URL . 'public/js/public-script.js', array('jquery'), PC_FAQ_MANAGER_VERSION, true);
    }
    
    /**
     * Render FAQ page shortcode
     */
    public function render_faq_page($atts) {
        $atts = shortcode_atts(array(
            'title' => __('Frequently Asked Questions', PC_FAQ_MANAGER_SLUG),
            'show_count' => true,
        ), $atts);
        
        $faqs = PC_FAQ_Manager_Helper::get_faqs();
        
        if (empty($faqs)) {
            return '<div class="pc-faq-empty">' . esc_html__('No FAQs available at this time.', PC_FAQ_MANAGER_SLUG) . '</div>';
        }
        
        ob_start();
        
        if ($atts['show_count']) {
            echo '<div class="pc-faq-header">';
            echo '<h1 class="pc-faq-title">' . esc_html($atts['title']) . '</h1>';
            echo '<div class="pc-faq-count">' . sprintf(esc_html__('%d FAQs available', PC_FAQ_MANAGER_SLUG), count($faqs)) . '</div>';
            echo '</div>';
        }
        
        echo '<div class="pc-faq-container">';
        echo '<div class="pc-faq-items">';
        
        foreach ($faqs as $index => $faq) {
            echo '<div class="pc-faq-item" data-faq-id="' . esc_attr($faq->ID) . '">';
            echo '<div class="pc-faq-question">';
            echo '<button class="pc-faq-toggle" aria-expanded="false" aria-controls="pc-faq-answer-' . esc_attr($faq->ID) . '">';
            echo '<span class="pc-faq-question-text">' . esc_html($faq->post_title) . '</span>';
            echo '<span class="pc-faq-toggle-icon"><span class="pc-faq-plus"></span><span class="pc-faq-minus"></span></span>';
            echo '</button>';
            echo '</div>';
            
            echo '<div class="pc-faq-answer" id="pc-faq-answer-' . esc_attr($faq->ID) . '" style="display: none;">';
            echo '<div class="pc-faq-answer-content">';
            echo wp_kses_post($faq->post_content);
            echo '</div>';
            echo '</div>';
            
            echo '</div>';
        }
        
        echo '</div>';
        echo '</div>';
        
        return ob_get_clean();
    }
}