<?php
/**
 * FAQ Post Type Handler
 */

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

class PC_FAQ_Manager_Post_Type {
    
    /**
     * Constructor
     */
    public function __construct() {
        add_action('init', array($this, 'register_post_type'));
        add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
        add_action('save_post_pc_faq', array($this, 'save_faq_meta'));
        add_action('manage_pc_faq_posts_custom_column', array($this, 'custom_columns'), 10, 2);
        add_filter('manage_pc_faq_posts_columns', array($this, 'set_custom_columns'));
    }
    
    /**
     * Register FAQ post type
     */
    public function register_post_type() {
        $labels = array(
            'name' => __('FAQs', PC_FAQ_MANAGER_SLUG),
            'singular_name' => __('FAQ', PC_FAQ_MANAGER_SLUG),
            'menu_name' => __('FAQ Manager', PC_FAQ_MANAGER_SLUG),
            'add_new' => __('Add New FAQ', PC_FAQ_MANAGER_SLUG),
            'add_new_item' => __('Add New FAQ', PC_FAQ_MANAGER_SLUG),
            'edit_item' => __('Edit FAQ', PC_FAQ_MANAGER_SLUG),
            'new_item' => __('New FAQ', PC_FAQ_MANAGER_SLUG),
            'view_item' => __('View FAQ', PC_FAQ_MANAGER_SLUG),
            'search_items' => __('Search FAQs', PC_FAQ_MANAGER_SLUG),
            'not_found' => __('No FAQs found', PC_FAQ_MANAGER_SLUG),
            'not_found_in_trash' => __('No FAQs found in Trash', PC_FAQ_MANAGER_SLUG),
            'all_items' => __('All FAQs', PC_FAQ_MANAGER_SLUG),
        );
        
        $args = array(
            'labels' => $labels,
            'public' => false,
            'has_archive' => false,
            'publicly_queryable' => false,
            'query_var' => false,
            'rewrite' => false,
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => 25,
            'menu_icon' => 'dashicons-editor-help',
            'supports' => array('title', 'editor'),
            'show_in_rest' => false,
            'show_ui' => true,
            'show_in_menu' => false,
            'exclude_from_search' => true,
        );
        
        register_post_type('pc_faq', $args);
    }
    
    /**
     * Add meta boxes
     */
    public function add_meta_boxes() {
        add_meta_box(
            'pc_faq_order_meta',
            __('FAQ Order', PC_FAQ_MANAGER_SLUG),
            array($this, 'order_meta_box'),
            'pc_faq',
            'side',
            'default'
        );
    }
    
    /**
     * Order meta box callback
     */
    public function order_meta_box($post) {
        wp_nonce_field('pc_faq_save_meta', 'pc_faq_nonce');
        
        $order = get_post_meta($post->ID, '_pc_faq_order', true) ?: 0;
        
        echo '<label for="pc_faq_order">' . __('Display Order:', PC_FAQ_MANAGER_SLUG) . '</label>';
        echo '<input type="number" id="pc_faq_order" name="pc_faq_order" value="' . esc_attr($order) . '" min="0" style="width: 100%; margin-top: 5px;" />';
        echo '<p class="description">' . __('Lower numbers appear first. Leave at 0 for automatic ordering.', PC_FAQ_MANAGER_SLUG) . '</p>';
    }
    
    /**
     * Save FAQ meta
     */
    public function save_faq_meta($post_id) {
        if (!isset($_POST['pc_faq_nonce']) || !wp_verify_nonce($_POST['pc_faq_nonce'], 'pc_faq_save_meta')) {
            return;
        }
        
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }
        
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
        
        if (isset($_POST['pc_faq_order'])) {
            $order = sanitize_text_field($_POST['pc_faq_order']);
            update_post_meta($post_id, '_pc_faq_order', $order);
        }
    }
    
    /**
     * Set custom columns
     */
    public function set_custom_columns($columns) {
        $new_columns = array();
        $new_columns['cb'] = $columns['cb'];
        $new_columns['title'] = $columns['title'];
        $new_columns['order'] = __('Order', PC_FAQ_MANAGER_SLUG);
        $new_columns['date'] = $columns['date'];
        
        return $new_columns;
    }
    
    /**
     * Display custom columns
     */
    public function custom_columns($column, $post_id) {
        switch ($column) {
            case 'order':
                $order = get_post_meta($post_id, '_pc_faq_order', true) ?: 0;
                echo esc_html($order);
                break;
        }
    }
}