<?php

/**
 * ZoloBlocks Plugin Installer Class
 *
 * This class handles the automatic installation and activation of the ZoloBlocks plugin
 * when users import templates from the template library.
 *
 * @package Blynex
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

class Blynex_ZoloBlocks_Installer {

    /**
     * Plugin slug for ZoloBlocks
     */
    const PLUGIN_SLUG = 'zoloblocks';

    /**
     * Plugin file path
     */
    const PLUGIN_FILE = 'zoloblocks/zoloblocks.php';

    /**
     * Plugin download URL
     */
    const PLUGIN_DOWNLOAD_URL = 'https://downloads.wordpress.org/plugin/zoloblocks.latest-stable.zip';

    /**
     * Initialize the installer
     */
    public function __construct() {
        add_action('wp_ajax_blynex_install_zoloblocks', array($this, 'install_zoloblocks_ajax'));
        add_action('wp_ajax_blynex_activate_zoloblocks', array($this, 'activate_zoloblocks_ajax'));
        add_action('wp_ajax_blynex_check_zoloblocks_status', array($this, 'check_zoloblocks_status_ajax'));
    }

    /**
     * Check if ZoloBlocks plugin is installed
     *
     * @return bool
     */
    public function is_plugin_installed() {
        return file_exists(WP_PLUGIN_DIR . '/' . self::PLUGIN_FILE);
    }

    /**
     * Check if ZoloBlocks plugin is activated
     *
     * @return bool
     */
    public function is_plugin_activated() {
        return is_plugin_active(self::PLUGIN_FILE);
    }

    /**
     * Install ZoloBlocks plugin
     *
     * @return array
     */
    public function install_plugin() {
        // Check if user has permission to install plugins
        if (!current_user_can('install_plugins')) {
            return array(
                'success' => false,
                'message' => __('You do not have permission to install plugins.', 'blynex')
            );
        }

        // Check if plugin is already installed
        if ($this->is_plugin_installed()) {
            return array(
                'success' => true,
                'message' => __('Plugin is already installed.', 'blynex'),
                'installed' => true
            );
        }

        // Include required files for plugin installation
        require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
        require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
        require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

        // Create upgrader instance
        $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin());

        // Install the plugin
        $result = $upgrader->install(self::PLUGIN_DOWNLOAD_URL);

        if (is_wp_error($result)) {
            return array(
                'success' => false,
                'message' => $result->get_error_message()
            );
        }

        return array(
            'success' => true,
            'message' => __('Plugin installed successfully.', 'blynex'),
            'installed' => true
        );
    }

    /**
     * Activate ZoloBlocks plugin
     *
     * @return array
     */
    public function activate_plugin() {
        // Check if user has permission to activate plugins
        if (!current_user_can('activate_plugins')) {
            return array(
                'success' => false,
                'message' => __('You do not have permission to activate plugins.', 'blynex')
            );
        }

        // Check if plugin is installed
        if (!$this->is_plugin_installed()) {
            return array(
                'success' => false,
                'message' => __('Plugin is not installed.', 'blynex')
            );
        }

        // Check if plugin is already activated
        if ($this->is_plugin_activated()) {
            return array(
                'success' => true,
                'message' => __('Plugin is already activated.', 'blynex'),
                'activated' => true
            );
        }

        // Activate the plugin
        $result = activate_plugin(self::PLUGIN_FILE);

        if (is_wp_error($result)) {
            return array(
                'success' => false,
                'message' => $result->get_error_message()
            );
        }

        return array(
            'success' => true,
            'message' => __('Plugin activated successfully.', 'blynex'),
            'activated' => true
        );
    }

    /**
     * AJAX handler for installing ZoloBlocks plugin
     */
    public function install_zoloblocks_ajax() {
        // Verify nonce
        if (!wp_verify_nonce($_POST['nonce'], 'blynex_zoloblocks_nonce')) {
            wp_die(__('Security check failed.', 'blynex'));
        }

        $result = $this->install_plugin();
        wp_send_json($result);
    }

    /**
     * AJAX handler for activating ZoloBlocks plugin
     */
    public function activate_zoloblocks_ajax() {
        // Verify nonce
        if (!wp_verify_nonce($_POST['nonce'], 'blynex_zoloblocks_nonce')) {
            wp_die(__('Security check failed.', 'blynex'));
        }

        $result = $this->activate_plugin();
        wp_send_json($result);
    }

    /**
     * AJAX handler for checking ZoloBlocks plugin status
     */
    public function check_zoloblocks_status_ajax() {
        // Verify nonce
        if (!wp_verify_nonce($_POST['nonce'], 'blynex_zoloblocks_nonce')) {
            wp_die(__('Security check failed.', 'blynex'));
        }

        $status = array(
            'installed' => $this->is_plugin_installed(),
            'activated' => $this->is_plugin_activated(),
            'ready' => $this->is_plugin_installed() && $this->is_plugin_activated()
        );

        wp_send_json($status);
    }

    /**
     * Get plugin status
     *
     * @return array
     */
    public function get_plugin_status() {
        return array(
            'installed' => $this->is_plugin_installed(),
            'activated' => $this->is_plugin_activated(),
            'ready' => $this->is_plugin_installed() && $this->is_plugin_activated()
        );
    }
}

// Initialize the installer
new Blynex_ZoloBlocks_Installer();
