<?php
/**
 * FAQ Manager Uninstall Script
 * 
 * This script runs when the plugin is uninstalled and cleans up all plugin data.
 */

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

// Plugin constants
define('PC_FAQ_MANAGER_SLUG', 'pc-faq-manager-abc123');

// Delete all FAQ posts
function pc_faq_manager_delete_faqs() {
    $args = array(
        'post_type' => 'pc_faq',
        'post_status' => 'any',
        'posts_per_page' => -1,
        'fields' => 'ids',
    );
    
    $faq_ids = get_posts($args);
    
    if (!empty($faq_ids)) {
        foreach ($faq_ids as $faq_id) {
            // Delete post permanently
            wp_delete_post($faq_id, true);
        }
    }
}

// Delete FAQ page created by plugin
function pc_faq_manager_delete_faq_page() {
    $page = get_page_by_title('FAQ');
    
    if ($page) {
        // Check if page contains our shortcode before deleting
        $content = $page->post_content;
        if (strpos($content, '[pc_faq_page]') !== false) {
            wp_delete_post($page->ID, true);
        }
    }
}

// Delete plugin options
function pc_faq_manager_delete_options() {
    delete_option('pc_faq_manager_version');
}

// Clean up post meta
function pc_faq_manager_cleanup_post_meta() {
    global $wpdb;
    
    // Delete FAQ order meta
    $wpdb->delete(
        $wpdb->postmeta,
        array('meta_key' => '_pc_faq_order'),
        array('%s')
    );
}

// Run cleanup functions
pc_faq_manager_delete_faqs();
pc_faq_manager_delete_faq_page();
pc_faq_manager_delete_options();
pc_faq_manager_cleanup_post_meta();