<?php
/**
 * Plugin Name: FAQ Manager
 * Plugin URI: https://plugincompass.com/plugins/pc-faq-manager-abc123
 * Description: Easily create and manage FAQ pages with drag-and-drop reordering functionality.
 * Version: 1.0.0
 * Author: Plugin Compass
 * Author URI: https://plugincompass.com
 * License: GPLv2 or later
 * Text Domain: pc-faq-manager-abc123
 * Domain Path: /languages
 * Update URI: false
 */

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

// Plugin constants
define('PC_FAQ_MANAGER_VERSION', '1.0.0');
define('PC_FAQ_MANAGER_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('PC_FAQ_MANAGER_PLUGIN_URL', plugin_dir_url(__FILE__));
define('PC_FAQ_MANAGER_SLUG', 'pc-faq-manager-abc123');

// Prevent WordPress.org update checks
add_filter('site_transient_update_plugins', function($value) {
    $plugin_file = plugin_basename(__FILE__);
    if (isset($value->response[$plugin_file])) {
        unset($value->response[$plugin_file]);
    }
    return $value;
});

/**
 * Main plugin class
 */
class PC_FAQ_Manager {
    
    private static $instance = null;
    
    /**
     * Get singleton instance
     */
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * Constructor
     */
    private function __construct() {
        add_action('init', array($this, 'init'));
        register_activation_hook(__FILE__, array($this, 'activate'));
        register_deactivation_hook(__FILE__, array($this, 'deactivate'));
    }
    
    /**
     * Initialize plugin
     */
    public function init() {
        // Load text domain
        load_plugin_textdomain(PC_FAQ_MANAGER_SLUG, false, dirname(plugin_basename(__FILE__)) . '/languages');
        
        // Include required files
        $this->include_files();
        
        // Initialize admin functionality
        if (is_admin()) {
            new PC_FAQ_Manager_Admin();
        }
        
        // Initialize frontend functionality
        new PC_FAQ_Manager_Public();
    }
    
    /**
     * Include required files
     */
    private function include_files() {
        require_once PC_FAQ_MANAGER_PLUGIN_DIR . 'includes/class-pc-faq-manager-post-type.php';
        require_once PC_FAQ_MANAGER_PLUGIN_DIR . 'includes/class-pc-faq-manager-helper.php';
        
        if (is_admin()) {
            require_once PC_FAQ_MANAGER_PLUGIN_DIR . 'admin/class-pc-faq-manager-admin.php';
        }
        
        require_once PC_FAQ_MANAGER_PLUGIN_DIR . 'public/class-pc-faq-manager-public.php';
    }
    
    /**
     * Plugin activation
     */
    public function activate() {
        // Flush rewrite rules
        flush_rewrite_rules();
        
        // Create FAQ page if it doesn't exist
        $this->create_faq_page();
        
        // Set default options
        add_option('pc_faq_manager_version', PC_FAQ_MANAGER_VERSION);
    }
    
    /**
     * Plugin deactivation
     */
    public function deactivate() {
        // Flush rewrite rules
        flush_rewrite_rules();
    }
    
    /**
     * Create FAQ page automatically
     */
    private function create_faq_page() {
        $page_title = __('FAQ', PC_FAQ_MANAGER_SLUG);
        $page_content = '[pc_faq_page]';
        $page_template = '';
        
        // Check if page already exists
        $existing_page = get_page_by_title($page_title);
        
        if (!$existing_page) {
            $page_data = array(
                'post_title'    => $page_title,
                'post_content'  => $page_content,
                'post_status'   => 'publish',
                'post_author'   => get_current_user_id(),
                'post_type'     => 'page',
                'post_name'     => 'faq'
            );
            
            $page_id = wp_insert_post($page_data);
            
            if ($page_id && !is_wp_error($page_id)) {
                update_post_meta($page_id, '_wp_page_template', $page_template);
            }
        }
    }
}

// Initialize the plugin
PC_FAQ_Manager::get_instance();