<?php
/**
 * Plugin Name: WP Digital Download
 * Plugin URI: https://example.com/wp-digital-download
 * Description: A comprehensive digital download marketplace plugin allowing creators to sell digital products with PayPal integration
 * Version: 1.0.0
 * Author: Josh Knapp
 * License: GPL v2 or later
 * Text Domain: wp-digital-download
 * Domain Path: /languages
 */

if (!defined('ABSPATH')) {
    exit;
}

define('WPDD_VERSION', '1.0.2');
define('WPDD_PLUGIN_URL', plugin_dir_url(__FILE__));
define('WPDD_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('WPDD_PLUGIN_BASENAME', plugin_basename(__FILE__));
define('WPDD_UPLOADS_DIR', 'wp-digital-downloads');

final class WP_Digital_Download {
    
    private static $instance = null;
    
    public static function instance() {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        $this->load_dependencies();
        $this->init_hooks();
    }
    
    private function load_dependencies() {
        $files = array(
            'includes/class-wpdd-install.php',
            'includes/class-wpdd-post-types.php',
            'includes/class-wpdd-roles.php',
            'includes/class-wpdd-metaboxes.php',
            'includes/class-wpdd-shortcodes.php',
            'includes/class-wpdd-paypal.php',
            'includes/class-wpdd-download-handler.php',
            'includes/class-wpdd-customer.php',
            'includes/class-wpdd-orders.php',
            'includes/class-wpdd-file-protection.php',
            'includes/class-wpdd-watermark.php',
            'includes/class-wpdd-ajax.php',
            'includes/class-wpdd-creator.php',
            'includes/class-wpdd-paypal-payouts.php'
        );
        
        foreach ($files as $file) {
            $file_path = WPDD_PLUGIN_PATH . $file;
            if (file_exists($file_path)) {
                require_once $file_path;
            } else {
                error_log('WPDD Error: File not found: ' . $file_path);
            }
        }
        
        if (is_admin()) {
            $admin_files = array(
                'admin/class-wpdd-admin.php',
                'admin/class-wpdd-settings.php',
                'admin/class-wpdd-admin-payouts.php'
            );
            
            foreach ($admin_files as $file) {
                $file_path = WPDD_PLUGIN_PATH . $file;
                if (file_exists($file_path)) {
                    require_once $file_path;
                } else {
                    error_log('WPDD Error: Admin file not found: ' . $file_path);
                }
            }
        }
    }
    
    private function init_hooks() {
        register_activation_hook(__FILE__, array('WPDD_Install', 'activate'));
        register_deactivation_hook(__FILE__, array('WPDD_Install', 'deactivate'));
        
        // Initialize sessions for PayPal integration (must be early priority)
        add_action('init', array($this, 'start_session'), 1);
        add_action('init', array($this, 'init'));
        add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
        
        if (is_admin()) {
            add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
        }
    }
    
    public function init() {
        load_plugin_textdomain('wp-digital-download', false, dirname(WPDD_PLUGIN_BASENAME) . '/languages');
        
        // Initialize classes with existence checks
        if (class_exists('WPDD_Roles')) {
            WPDD_Roles::init();
        } else {
            error_log('WPDD Error: WPDD_Roles class not found');
        }
        
        if (class_exists('WPDD_Post_Types')) {
            WPDD_Post_Types::init();
        } else {
            error_log('WPDD Error: WPDD_Post_Types class not found');
        }
        
        if (class_exists('WPDD_Metaboxes')) {
            WPDD_Metaboxes::init();
        } else {
            error_log('WPDD Error: WPDD_Metaboxes class not found');
        }
        
        if (class_exists('WPDD_Shortcodes')) {
            WPDD_Shortcodes::init();
        } else {
            error_log('WPDD Error: WPDD_Shortcodes class not found');
        }
        
        if (class_exists('WPDD_PayPal')) {
            WPDD_PayPal::init();
        } else {
            error_log('WPDD Error: WPDD_PayPal class not found');
        }
        
        if (class_exists('WPDD_Download_Handler')) {
            WPDD_Download_Handler::init();
        } else {
            error_log('WPDD Error: WPDD_Download_Handler class not found');
        }
        
        if (class_exists('WPDD_Customer')) {
            WPDD_Customer::init();
        } else {
            error_log('WPDD Error: WPDD_Customer class not found');
        }
        
        if (class_exists('WPDD_File_Protection')) {
            WPDD_File_Protection::init();
        } else {
            error_log('WPDD Error: WPDD_File_Protection class not found');
        }
        
        if (class_exists('WPDD_Creator')) {
            WPDD_Creator::init();
        } else {
            error_log('WPDD Error: WPDD_Creator class not found');
        }
        
        if (class_exists('WPDD_PayPal_Payouts')) {
            WPDD_PayPal_Payouts::init();
        } else {
            error_log('WPDD Error: WPDD_PayPal_Payouts class not found');
        }
        
        // Fix search functionality to include wpdd_product post type
        add_filter('pre_get_posts', array($this, 'include_custom_post_types_in_search'));
        
        if (class_exists('WPDD_Ajax')) {
            WPDD_Ajax::init();
        } else {
            error_log('WPDD Error: WPDD_Ajax class not found');
        }
        
        if (is_admin()) {
            if (class_exists('WPDD_Admin')) {
                WPDD_Admin::init();
            } else {
                error_log('WPDD Error: WPDD_Admin class not found');
            }
            
            if (class_exists('WPDD_Settings')) {
                WPDD_Settings::init();
            } else {
                error_log('WPDD Error: WPDD_Settings class not found');
            }
        }
    }
    
    /**
     * Initialize PHP sessions for PayPal order data persistence
     * Called early in WordPress init to ensure sessions work properly
     */
    public function start_session() {
        if (!session_id()) {
            session_start();
        }
    }
    
    public function enqueue_scripts() {
        // Use file modification time for cache busting
        $frontend_css_ver = file_exists(WPDD_PLUGIN_PATH . 'assets/css/frontend.css') 
            ? filemtime(WPDD_PLUGIN_PATH . 'assets/css/frontend.css') 
            : WPDD_VERSION;
            
        $frontend_js_ver = file_exists(WPDD_PLUGIN_PATH . 'assets/js/frontend.js') 
            ? filemtime(WPDD_PLUGIN_PATH . 'assets/js/frontend.js') 
            : WPDD_VERSION;
        
        wp_enqueue_style(
            'wpdd-frontend',
            WPDD_PLUGIN_URL . 'assets/css/frontend.css',
            array(),
            $frontend_css_ver
        );
        
        wp_enqueue_script(
            'wpdd-frontend',
            WPDD_PLUGIN_URL . 'assets/js/frontend.js',
            array('jquery'),
            $frontend_js_ver,
            true
        );
        
        wp_localize_script('wpdd-frontend', 'wpdd_ajax', array(
            'url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('wpdd-ajax-nonce')
        ));
    }
    
    public function admin_enqueue_scripts($hook) {
        global $post;
        
        if (($hook == 'post.php' || $hook == 'post-new.php') && 
            isset($post) && $post->post_type == 'wpdd_product') {
            
            // Use file modification time for cache busting
            $admin_css_ver = file_exists(WPDD_PLUGIN_PATH . 'assets/css/admin.css') 
                ? filemtime(WPDD_PLUGIN_PATH . 'assets/css/admin.css') 
                : WPDD_VERSION;
                
            $admin_js_ver = file_exists(WPDD_PLUGIN_PATH . 'assets/js/admin.js') 
                ? filemtime(WPDD_PLUGIN_PATH . 'assets/js/admin.js') 
                : WPDD_VERSION;
            
            wp_enqueue_media();
            wp_enqueue_style(
                'wpdd-admin',
                WPDD_PLUGIN_URL . 'assets/css/admin.css',
                array(),
                $admin_css_ver
            );
            
            wp_enqueue_script(
                'wpdd-admin',
                WPDD_PLUGIN_URL . 'assets/js/admin.js',
                array('jquery', 'jquery-ui-sortable'),
                $admin_js_ver,
                true
            );
            
            // Add nonce for admin AJAX
            wp_localize_script('wpdd-admin', 'wpdd_admin_nonce', wp_create_nonce('wpdd-admin-nonce'));
        }
    }
    
    /**
     * Include custom post types in search results
     * This fixes the search functionality to include wpdd_product posts in frontend searches
     */
    public function include_custom_post_types_in_search($query) {
        // Apply to all frontend searches, not just main query
        if (!is_admin() && $query->is_search()) {
            $current_post_types = $query->get('post_type');
            
            // If post_type is already set to wpdd_product, don't override it
            if ($current_post_types === 'wpdd_product') {
                return $query;
            }
            
            // Otherwise, include wpdd_product in searches
            if (empty($current_post_types) || $current_post_types === 'post') {
                $query->set('post_type', array('post', 'wpdd_product'));
            } elseif (is_array($current_post_types)) {
                $current_post_types[] = 'wpdd_product';
                $query->set('post_type', array_unique($current_post_types));
            }
        }
        return $query;
    }
}

function wpdd() {
    return WP_Digital_Download::instance();
}

wpdd();

// Helper function for formatting prices
if (!function_exists('wpdd_format_price')) {
    function wpdd_format_price($price, $currency = 'USD') {
        $currencies = array(
            'USD' => '$',
            'EUR' => '€',
            'GBP' => '£',
            'JPY' => '¥',
            'AUD' => 'A$',
            'CAD' => 'C$'
        );
        
        $symbol = isset($currencies[$currency]) ? $currencies[$currency] : $currency . ' ';
        return $symbol . number_format($price, 2);
    }
}

// Deactivation hook
register_deactivation_hook(__FILE__, 'wpdd_deactivation');

function wpdd_deactivation() {
    if (class_exists('WPDD_PayPal_Payouts')) {
        WPDD_PayPal_Payouts::deactivate();
    }
}