<?php
/**
 * Plugin Name: ShopCentral - Advanced Store Management & Analytics for WooCommerce
 * Plugin URI: https://dominopress.com/plugin/shopcentral
 * Description: All-in-one WooCommerce toolkit including personalized upsells, custom product addons, checkout field editor, multi-store manager, and interactive slide cart.
 * Version: 2.2.0
 * Author: Dominopress
 * Author URI: https://dominopress.com/
 * Text Domain: shopcentral
 * Requires at least: 6.8
 * Requires PHP: 7.4
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */

defined('ABSPATH') || exit;

// Define constants
define('SHOPCENTRAL_VERSION', '2.2.0');
define('SHOPCENTRAL_PATH', plugin_dir_path(__FILE__));
define('SHOPCENTRAL_URL', plugin_dir_url(__FILE__));

// Include requirement files
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-db.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-remote-api.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-settings-api.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-api.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-store-api.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-advanced-analytics.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-customer-intelligence.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-marketing-controller.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-automation-engine.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-notification-system.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-export-tools.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-event-tracker.php';
require_once SHOPCENTRAL_PATH . 'includes/class-shopcentral-deactivation-survey.php';

class ShopCentral_Plugin
{
    public function __construct()
    {
        // Activation hooks
        register_activation_hook(__FILE__, ['ShopCentral_DB', 'init']);
        register_activation_hook(__FILE__, [$this, 'plugin_activation_redirect']);
        register_activation_hook(__FILE__, function () {
            ShopCentral_Event_Tracker::track_install();
            ShopCentral_Event_Tracker::track_activation();
        });

        // Deactivation hook
        register_deactivation_hook(__FILE__, ['ShopCentral_Event_Tracker', 'track_deactivation']);

        // Initialize event tracker and deactivation survey
        ShopCentral_Event_Tracker::init();
        ShopCentral_Deactivation_Survey::init();

        // Also check tables on admin init
        add_action('admin_init', [$this, 'check_tables']);
        add_action('admin_init', [$this, 'handle_activation_redirect']);

        // Get feature settings
        $features = ShopCentral_Settings_API::get_feature_settings();

        // Initialize core API (always needed)
        new ShopCentral_API();
        new ShopCentral_Settings_API('shopcentral/v1');



        if (isset($features['store_manager']) && $features['store_manager']) {
            new ShopCentral_Store_API('shopcentral/v1');
            new ShopCentral_Advanced_Analytics('shopcentral/v1');
            new ShopCentral_Customer_Intelligence('shopcentral/v1');
            new ShopCentral_Marketing_Controller('shopcentral/v1');
            new ShopCentral_Automation_Engine('shopcentral/v1');
            new ShopCentral_Notification_System('shopcentral/v1');
            new ShopCentral_Export_Tools('shopcentral/v1');
        }

        add_action('admin_menu', [$this, 'register_menu']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);



        // Nonce Refresh
        add_action('wp_ajax_shopcentral_get_nonce', [$this, 'ajax_get_nonce']);
        add_action('wp_ajax_nopriv_shopcentral_get_nonce', [$this, 'ajax_get_nonce']);
    }



    public function check_tables()
    {
        global $wpdb;
        $stores_table = $wpdb->prefix . 'shopcentral_stores';
        $stores_exists = $wpdb->get_var("SHOW TABLES LIKE '$stores_table'");

        if (!$stores_exists) {
            ShopCentral_DB::init();
        }
    }

    public function plugin_activation_redirect()
    {
        // Set a transient to trigger redirect after activation
        set_transient('shopcentral_activation_redirect', true, 30);
    }

    public function handle_activation_redirect()
    {
        // Check if we should redirect
        if (get_transient('shopcentral_activation_redirect')) {
            delete_transient('shopcentral_activation_redirect');

            // Only redirect if not doing bulk activation
            if (!isset($_GET['activate-multi'])) {
                wp_safe_redirect(admin_url('admin.php?page=shopcentral'));
                exit;
            }
        }
    }

    public function register_menu()
    {
        // Main Menu
        add_menu_page(
            'ShopCentral',
            'ShopCentral',
            'manage_options',
            'shopcentral',
            [$this, 'render_admin_page'],
            'dashicons-store',
            6
        );

        add_submenu_page(
            'shopcentral',
            'Store Manager',
            'Store Manager',
            'manage_options',
            'shopcentral',
            [$this, 'render_admin_page']
        );
    }

    public function enqueue_scripts($hook)
    {
        $allowed_hooks = [
            'toplevel_page_shopcentral'
        ];

        if (!in_array($hook, $allowed_hooks)) {
            return;
        }

        $script_path = SHOPCENTRAL_PATH . 'assets/admin.js';
        $script_url = SHOPCENTRAL_URL . 'assets/admin.js';
        $vendors_path = SHOPCENTRAL_PATH . 'assets/vendors.js';
        $vendors_url = SHOPCENTRAL_URL . 'assets/vendors.js';

        if (file_exists($vendors_path)) {
            wp_enqueue_script(
                'shopcentral-vendors',
                $vendors_url,
                [],
                filemtime($vendors_path),
                true
            );
        }

        if (file_exists($script_path)) {
            wp_enqueue_script(
                'shopcentral-admin',
                $script_url,
                ['shopcentral-vendors', 'wp-element'],
                filemtime($script_path),
                true
            );

            $currentPage = 'store-manager';

            wp_localize_script('shopcentral-admin', 'shopCentralData', [
                'rootUrl' => home_url(),
                'apiUrl' => rest_url('shopcentral/v1/'),
                'nonce' => wp_create_nonce('wp_rest'),
                'currentPage' => $currentPage,
                'isPro' => function_exists('shopcentral_pro_fs') && shopcentral_pro_fs()->can_use_premium_code()
            ]);

            // Hide standard WordPress footer and notices ONLY on our admin page
            if ($hook === 'toplevel_page_shopcentral') {
                echo '<style>
                    #wpfooter { display: none !important; }
                    #wpbody-content { padding-bottom: 0 !important; }
                    html, body, #wpwrap, #wpbody, #wpbody-content { 
                        overflow: auto !important; 
                        height: auto !important; 
                        min-height: 100vh;
                    }
                    #shopcentral-root { min-height: 100vh; }
                </style>';
            }
        }
    }

    public function render_admin_page()
    {
        echo '<div id="shopcentral-root"></div>';
    }

    public function ajax_get_nonce()
    {
        wp_send_json_success(['nonce' => wp_create_nonce('wp_rest')]);
    }


}

new ShopCentral_Plugin();
