<?php
/**************************
 * Plugin Installer
 **************************/

// Admin Enqueue for Admin
function author_portfolio_admin_enqueue_scripts() {
    wp_enqueue_script( 'author-portfolio-plugin-installer', get_theme_file_uri( '/inc/welcome/welcome-notice.js' ), array( 'jquery' ), '', true );
    wp_localize_script(
        'author-portfolio-plugin-installer',
        'author_portfolio_ajax_object',
        array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'nonce'    => wp_create_nonce( 'author_portfolio_install_plugins_nonce' ) // Added Nonce
        )
    );
}
add_action( 'admin_enqueue_scripts', 'author_portfolio_admin_enqueue_scripts' );

add_action( 'wp_ajax_install_act_plugin', 'author_portfolio_admin_install_plugin' );

function author_portfolio_admin_install_plugin() {
    // 1. Verify Nonce
    if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'author_portfolio_install_plugins_nonce' ) ) {
        wp_send_json_error( array( 'message' => esc_html__( 'Security check failed. Please refresh the page and try again.', 'author-portfolio' ) ) );
    }

    // 2. Check Capability
    if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
        wp_send_json_error( array( 'message' => esc_html__( 'You do not have sufficient permissions to install or activate plugins.', 'author-portfolio' ) ) );
    }

    include_once ABSPATH . 'wp-admin/includes/plugin.php';

    /**
     * 1️⃣ Plugins that can be INSTALLED + ACTIVATED
     */
    $installable_plugins = array(
        'rs-author-info-box'       => 'rs-author-info-box/rs-author-info-box.php',
        'rs-wp-books-showcase'     => 'rs-wp-books-showcase/rs-wp-books-showcase.php',
        'author-website-templates' => 'author-website-templates/author-website-templates.php',
        'advanced-import'          => 'advanced-import/advanced-import.php',
    );

    /**
     * 2️⃣ Pro plugins (ONLY activate if already installed)
     */
    $pro_plugins = array(
        'rs-wp-book-showcase-pro/rs-wp-book-showcase-pro.php',
        'author-website-templates-pro/author-website-templates-pro.php',
    );

    /**
     * ----------------------------------
     * Install + Activate normal plugins
     * ----------------------------------
     */
    include_once ABSPATH . 'wp-admin/includes/file.php';
    include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

    if ( ! WP_Filesystem() ) {
        wp_send_json_error( array( 'message' => 'Filesystem permission error.' ) );
    }

    $skin     = new WP_Upgrader_Skin();
    $upgrader = new Plugin_Upgrader( $skin );

    foreach ( $installable_plugins as $slug => $main_file ) {

        if ( ! file_exists( WP_PLUGIN_DIR . '/' . dirname( $main_file ) ) ) {

            $api = plugins_api( 'plugin_information', array(
                'slug'   => sanitize_key( $slug ),
                'fields' => array( 'sections' => false ),
            ) );

            if ( is_wp_error( $api ) ) {
                continue;
            }

            ob_start();
            $upgrader->install( $api->download_link );
            ob_end_clean();
        }

        if ( file_exists( WP_PLUGIN_DIR . '/' . $main_file ) && ! is_plugin_active( $main_file ) ) {
            activate_plugin( $main_file );
        }
    }

    /**
     * ----------------------------------
     * Activate ONLY installed PRO plugins
     * ----------------------------------
     */
    foreach ( $pro_plugins as $pro_plugin ) {

        if ( file_exists( WP_PLUGIN_DIR . '/' . $pro_plugin ) ) {

            if ( ! is_plugin_active( $pro_plugin ) ) {
                activate_plugin( $pro_plugin );
            }
        }
        // ❌ Installed না থাকলে কিছুই করবে না
    }

    wp_send_json_success( array(
        'message' => 'Installed plugins activated. Pro plugins activated only if already installed.'
    ) );
}
