<?php
/**
 * Themes and Plugins Handler
 *
 * @package Post_Export_Import_With_Media
 * @since 1.3.0
 */

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

/**
 * Themes and Plugins Handler Class - Manages themes and plugins backup/restore operations
 */
class PEIWM_Themes_Plugins_Handler {

	/**
	 * Instance
	 *
	 * @var PEIWM_Themes_Plugins_Handler|null
	 */
	private static $instance = null;

	/**
	 * Get instance
	 *
	 * @return PEIWM_Themes_Plugins_Handler
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor
	 */
	private function __construct() {
		$this->init_ajax_hooks();
	}

	/**
	 * Initialize AJAX hooks
	 */
	private function init_ajax_hooks() {
		// Themes
		add_action( 'wp_ajax_peiwm_get_themes_list', array( $this, 'ajax_get_themes_list' ) );
		add_action( 'wp_ajax_peiwm_export_themes', array( $this, 'ajax_export_themes' ) );
		add_action( 'wp_ajax_peiwm_import_themes', array( $this, 'ajax_import_themes' ) );
		
		// Plugins
		add_action( 'wp_ajax_peiwm_get_plugins_list', array( $this, 'ajax_get_plugins_list' ) );
		add_action( 'wp_ajax_peiwm_export_plugins', array( $this, 'ajax_export_plugins' ) );
		add_action( 'wp_ajax_peiwm_import_plugins', array( $this, 'ajax_import_plugins' ) );
	}

	/**
	 * AJAX: Get themes list
	 */
	public function ajax_get_themes_list() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'peiwm_secure_nonce' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Security check failed', 'post-export-import-with-media' ) ) );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Permission denied', 'post-export-import-with-media' ) ) );
		}

		try {
			$themes = wp_get_themes();
			$active_theme = get_stylesheet();
			$themes_list = array();

			foreach ( $themes as $theme_slug => $theme ) {
				$themes_list[] = array(
					'slug' => $theme_slug,
					'name' => $theme->get( 'Name' ),
					'version' => $theme->get( 'Version' ),
					'description' => $theme->get( 'Description' ),
					'author' => $theme->get( 'Author' ),
					'is_active' => ( $theme_slug === $active_theme ),
					'screenshot' => $theme->get_screenshot() ? $theme->get_screenshot() : '',
				);
			}

			wp_send_json_success( array(
				'themes' => $themes_list,
				'active_theme' => $active_theme,
				'total_count' => count( $themes_list ),
			) );

		} catch ( Exception $e ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Failed to get themes list', 'post-export-import-with-media' ) ) );
		}
	}

	/**
	 * AJAX: Get plugins list
	 */
	public function ajax_get_plugins_list() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'peiwm_secure_nonce' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Security check failed', 'post-export-import-with-media' ) ) );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Permission denied', 'post-export-import-with-media' ) ) );
		}

		try {
			$all_plugins = get_plugins();
			$active_plugins = get_option( 'active_plugins', array() );
			$plugins_list = array();

			foreach ( $all_plugins as $plugin_file => $plugin_data ) {
				$plugin_slug = dirname( $plugin_file );
				if ( $plugin_slug === '.' ) {
					$plugin_slug = basename( $plugin_file, '.php' );
				}

				$plugins_list[] = array(
					'file' => $plugin_file,
					'slug' => $plugin_slug,
					'name' => $plugin_data['Name'],
					'version' => $plugin_data['Version'],
					'description' => $plugin_data['Description'],
					'author' => $plugin_data['Author'],
					'is_active' => in_array( $plugin_file, $active_plugins, true ),
				);
			}

			wp_send_json_success( array(
				'plugins' => $plugins_list,
				'active_plugins' => $active_plugins,
				'total_count' => count( $plugins_list ),
			) );

		} catch ( Exception $e ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Failed to get plugins list', 'post-export-import-with-media' ) ) );
		}
	}

	/**
	 * AJAX: Export themes
	 */
	public function ajax_export_themes() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'peiwm_secure_nonce' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Security check failed', 'post-export-import-with-media' ) ) );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Permission denied', 'post-export-import-with-media' ) ) );
		}

		try {
			$export_type = isset( $_POST['export_type'] ) ? sanitize_text_field( wp_unslash( $_POST['export_type'] ) ) : 'active';
			$selected_themes = isset( $_POST['selected_themes'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_themes'] ) ) : array();

			$themes_to_export = $this->get_themes_to_export( $export_type, $selected_themes );
			
			if ( empty( $themes_to_export ) ) {
				wp_send_json_error( array( 'message' => esc_html__( 'No themes selected for export', 'post-export-import-with-media' ) ) );
			}

			$zip_file = $this->create_themes_zip( $themes_to_export );
			
			if ( ! $zip_file ) {
				wp_send_json_error( array( 'message' => esc_html__( 'Failed to create themes backup', 'post-export-import-with-media' ) ) );
			}

			wp_send_json_success( array(
				'download_url' => $zip_file['url'],
				'file_size' => $zip_file['size'],
				'themes_count' => count( $themes_to_export ),
				'message' => sprintf(
					esc_html__( 'Successfully exported %d theme(s)', 'post-export-import-with-media' ),
					count( $themes_to_export )
				),
			) );

		} catch ( Exception $e ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Themes export failed. Please try again.', 'post-export-import-with-media' ) ) );
		}
	}

	/**
	 * AJAX: Export plugins
	 */
	public function ajax_export_plugins() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'peiwm_secure_nonce' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Security check failed', 'post-export-import-with-media' ) ) );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Permission denied', 'post-export-import-with-media' ) ) );
		}

		try {
			$export_type = isset( $_POST['export_type'] ) ? sanitize_text_field( wp_unslash( $_POST['export_type'] ) ) : 'active';
			$selected_plugins = isset( $_POST['selected_plugins'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_plugins'] ) ) : array();

			$plugins_to_export = $this->get_plugins_to_export( $export_type, $selected_plugins );
			
			if ( empty( $plugins_to_export ) ) {
				wp_send_json_error( array( 'message' => esc_html__( 'No plugins selected for export', 'post-export-import-with-media' ) ) );
			}

			$zip_file = $this->create_plugins_zip( $plugins_to_export );
			
			if ( ! $zip_file ) {
				wp_send_json_error( array( 'message' => esc_html__( 'Failed to create plugins backup', 'post-export-import-with-media' ) ) );
			}

			wp_send_json_success( array(
				'download_url' => $zip_file['url'],
				'file_size' => $zip_file['size'],
				'plugins_count' => count( $plugins_to_export ),
				'message' => sprintf(
					esc_html__( 'Successfully exported %d plugin(s)', 'post-export-import-with-media' ),
					count( $plugins_to_export )
				),
			) );

		} catch ( Exception $e ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Plugins export failed. Please try again.', 'post-export-import-with-media' ) ) );
		}
	}

	/**
	 * AJAX: Import themes
	 */
	public function ajax_import_themes() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'peiwm_secure_nonce' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Security check failed', 'post-export-import-with-media' ) ) );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Permission denied', 'post-export-import-with-media' ) ) );
		}

		try {
			if ( ! isset( $_FILES['themes_file'] ) || ! isset( $_FILES['themes_file']['error'] ) || $_FILES['themes_file']['error'] !== UPLOAD_ERR_OK ) {
				wp_send_json_error( array( 'message' => esc_html__( 'No file uploaded or upload error', 'post-export-import-with-media' ) ) );
			}

			$replace_existing = isset( $_POST['replace_existing'] ) && $_POST['replace_existing'] === '1';
			$activate_theme = isset( $_POST['activate_theme'] ) && $_POST['activate_theme'] === '1';

			$uploaded_file = $_FILES['themes_file']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- File data is validated and processed by WordPress core functions
			$result = $this->import_themes_from_zip( $uploaded_file, $replace_existing, $activate_theme );

			if ( $result['success'] ) {
				wp_send_json_success( $result );
			} else {
				wp_send_json_error( array( 'message' => $result['message'] ) );
			}

		} catch ( Exception $e ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Themes import failed. Please try again.', 'post-export-import-with-media' ) ) );
		}
	}

	/**
	 * AJAX: Import plugins
	 */
	public function ajax_import_plugins() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'peiwm_secure_nonce' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Security check failed', 'post-export-import-with-media' ) ) );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Permission denied', 'post-export-import-with-media' ) ) );
		}

		try {
			if ( ! isset( $_FILES['plugins_file'] ) || ! isset( $_FILES['plugins_file']['error'] ) || $_FILES['plugins_file']['error'] !== UPLOAD_ERR_OK ) {
				wp_send_json_error( array( 'message' => esc_html__( 'No file uploaded or upload error', 'post-export-import-with-media' ) ) );
			}

			$replace_existing = isset( $_POST['replace_existing'] ) && $_POST['replace_existing'] === '1';
			$activate_plugins = isset( $_POST['activate_plugins'] ) && $_POST['activate_plugins'] === '1';

			$uploaded_file = $_FILES['plugins_file']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- File data is validated and processed by WordPress core functions
			$result = $this->import_plugins_from_zip( $uploaded_file, $replace_existing, $activate_plugins );

			if ( $result['success'] ) {
				wp_send_json_success( $result );
			} else {
				wp_send_json_error( array( 'message' => $result['message'] ) );
			}

		} catch ( Exception $e ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Plugins import failed. Please try again.', 'post-export-import-with-media' ) ) );
		}
	}

	/**
	 * Get themes to export based on type and selection
	 *
	 * @param string $export_type Export type (active, all, selected)
	 * @param array  $selected_themes Selected theme slugs
	 * @return array Themes to export
	 */
	private function get_themes_to_export( $export_type, $selected_themes = array() ) {
		$themes = wp_get_themes();
		$active_theme = get_stylesheet();
		$themes_to_export = array();

		switch ( $export_type ) {
			case 'active':
				if ( isset( $themes[ $active_theme ] ) ) {
					$themes_to_export[ $active_theme ] = $themes[ $active_theme ];
				}
				break;

			case 'all':
				$themes_to_export = $themes;
				break;

			case 'selected':
				foreach ( $selected_themes as $theme_slug ) {
					if ( isset( $themes[ $theme_slug ] ) ) {
						$themes_to_export[ $theme_slug ] = $themes[ $theme_slug ];
					}
				}
				break;
		}

		return $themes_to_export;
	}

	/**
	 * Get plugins to export based on type and selection
	 *
	 * @param string $export_type Export type (active, all, selected)
	 * @param array  $selected_plugins Selected plugin files
	 * @return array Plugins to export
	 */
	private function get_plugins_to_export( $export_type, $selected_plugins = array() ) {
		$all_plugins = get_plugins();
		$active_plugins = get_option( 'active_plugins', array() );
		$plugins_to_export = array();

		switch ( $export_type ) {
			case 'active':
				foreach ( $active_plugins as $plugin_file ) {
					if ( isset( $all_plugins[ $plugin_file ] ) ) {
						$plugins_to_export[ $plugin_file ] = $all_plugins[ $plugin_file ];
					}
				}
				break;

			case 'all':
				$plugins_to_export = $all_plugins;
				break;

			case 'selected':
				foreach ( $selected_plugins as $plugin_file ) {
					if ( isset( $all_plugins[ $plugin_file ] ) ) {
						$plugins_to_export[ $plugin_file ] = $all_plugins[ $plugin_file ];
					}
				}
				break;
		}

		return $plugins_to_export;
	}

	/**
	 * Create themes ZIP file
	 *
	 * @param array $themes Themes to include in ZIP
	 * @return array|false ZIP file info or false on failure
	 */
	private function create_themes_zip( $themes ) {
		if ( ! class_exists( 'ZipArchive' ) ) {
			return false;
		}

		$upload_dir = wp_upload_dir();
		$export_dir = $upload_dir['basedir'] . '/peiwm-exports/';
		
		if ( ! wp_mkdir_p( $export_dir ) ) {
			return false;
		}

		$zip_filename = 'themes-backup-' . date( 'Y-m-d-H-i-s' ) . '.zip';
		$zip_path = $export_dir . $zip_filename;

		$zip = new ZipArchive();
		if ( $zip->open( $zip_path, ZipArchive::CREATE ) !== true ) {
			return false;
		}

		$themes_dir = get_theme_root();
		
		foreach ( $themes as $theme_slug => $theme ) {
			$theme_path = $themes_dir . '/' . $theme_slug;
			
			if ( is_dir( $theme_path ) ) {
				$this->add_directory_to_zip( $zip, $theme_path, 'themes/' . $theme_slug );
			}
		}

		$zip->close();

		if ( ! file_exists( $zip_path ) ) {
			return false;
		}

		return array(
			'path' => $zip_path,
			'url' => $upload_dir['baseurl'] . '/peiwm-exports/' . $zip_filename,
			'size' => filesize( $zip_path ),
		);
	}

	/**
	 * Create plugins ZIP file
	 *
	 * @param array $plugins Plugins to include in ZIP
	 * @return array|false ZIP file info or false on failure
	 */
	private function create_plugins_zip( $plugins ) {
		if ( ! class_exists( 'ZipArchive' ) ) {
			return false;
		}

		$upload_dir = wp_upload_dir();
		$export_dir = $upload_dir['basedir'] . '/peiwm-exports/';
		
		if ( ! wp_mkdir_p( $export_dir ) ) {
			return false;
		}

		$zip_filename = 'plugins-backup-' . date( 'Y-m-d-H-i-s' ) . '.zip';
		$zip_path = $export_dir . $zip_filename;

		$zip = new ZipArchive();
		if ( $zip->open( $zip_path, ZipArchive::CREATE ) !== true ) {
			return false;
		}

		$plugins_dir = WP_PLUGIN_DIR;
		
		foreach ( $plugins as $plugin_file => $plugin_data ) {
			$plugin_slug = dirname( $plugin_file );
			
			if ( $plugin_slug === '.' ) {
				// Single file plugin
				$plugin_path = $plugins_dir . '/' . $plugin_file;
				if ( file_exists( $plugin_path ) ) {
					$zip->addFile( $plugin_path, 'plugins/' . $plugin_file );
				}
			} else {
				// Directory plugin
				$plugin_path = $plugins_dir . '/' . $plugin_slug;
				if ( is_dir( $plugin_path ) ) {
					$this->add_directory_to_zip( $zip, $plugin_path, 'plugins/' . $plugin_slug );
				}
			}
		}

		$zip->close();

		if ( ! file_exists( $zip_path ) ) {
			return false;
		}

		return array(
			'path' => $zip_path,
			'url' => $upload_dir['baseurl'] . '/peiwm-exports/' . $zip_filename,
			'size' => filesize( $zip_path ),
		);
	}

	/**
	 * Add directory to ZIP recursively
	 *
	 * @param ZipArchive $zip ZIP archive object
	 * @param string     $source_dir Source directory path
	 * @param string     $zip_dir Directory path in ZIP
	 */
	private function add_directory_to_zip( $zip, $source_dir, $zip_dir ) {
		$iterator = new RecursiveIteratorIterator(
			new RecursiveDirectoryIterator( $source_dir, RecursiveDirectoryIterator::SKIP_DOTS ),
			RecursiveIteratorIterator::SELF_FIRST
		);

		foreach ( $iterator as $file ) {
			$file_path = $file->getRealPath();
			$relative_path = $zip_dir . '/' . substr( $file_path, strlen( $source_dir ) + 1 );
			
			// Convert Windows paths to Unix paths for ZIP
			$relative_path = str_replace( '\\', '/', $relative_path );

			if ( $file->isDir() ) {
				$zip->addEmptyDir( $relative_path );
			} elseif ( $file->isFile() ) {
				$zip->addFile( $file_path, $relative_path );
			}
		}
	}

	/**
	 * Import themes from ZIP file
	 *
	 * @param array $uploaded_file Uploaded file data
	 * @param bool  $replace_existing Whether to replace existing themes
	 * @param bool  $activate_theme Whether to activate the first imported theme
	 * @return array Import result
	 */
	private function import_themes_from_zip( $uploaded_file, $replace_existing = true, $activate_theme = false ) {
		if ( ! class_exists( 'ZipArchive' ) ) {
			return array(
				'success' => false,
				'message' => esc_html__( 'ZipArchive class not available', 'post-export-import-with-media' ),
			);
		}

		$upload_dir = wp_upload_dir();
		$temp_dir = $upload_dir['basedir'] . '/peiwm-temp/';
		
		if ( ! wp_mkdir_p( $temp_dir ) ) {
			return array(
				'success' => false,
				'message' => esc_html__( 'Failed to create temporary directory', 'post-export-import-with-media' ),
			);
		}

		// Move uploaded file to temp directory
		$temp_zip = $temp_dir . 'themes-import-' . time() . '.zip';
		if ( ! move_uploaded_file( $uploaded_file['tmp_name'], $temp_zip ) ) {
			return array(
				'success' => false,
				'message' => esc_html__( 'Failed to move uploaded file', 'post-export-import-with-media' ),
			);
		}

		$zip = new ZipArchive();
		if ( $zip->open( $temp_zip ) !== true ) {
			wp_delete_file( $temp_zip );
			return array(
				'success' => false,
				'message' => esc_html__( 'Failed to open ZIP file', 'post-export-import-with-media' ),
			);
		}

		$themes_dir = get_theme_root();
		$imported_themes = array();
		$skipped_themes = array();
		$failed_themes = array();

		// Extract themes
		for ( $i = 0; $i < $zip->numFiles; $i++ ) {
			$file_info = $zip->statIndex( $i );
			$file_path = $file_info['name'];

			// Check if this is a theme file (starts with themes/)
			if ( strpos( $file_path, 'themes/' ) !== 0 ) {
				continue;
			}

			// Get theme slug from path
			$path_parts = explode( '/', $file_path );
			if ( count( $path_parts ) < 3 ) {
				continue;
			}

			$theme_slug = $path_parts[1];
			$theme_dir = $themes_dir . '/' . $theme_slug;

			// Check if theme already exists
			if ( is_dir( $theme_dir ) && ! $replace_existing ) {
				if ( ! in_array( $theme_slug, $skipped_themes, true ) ) {
					$skipped_themes[] = $theme_slug;
				}
				continue;
			}

			// Extract file
			$relative_path = substr( $file_path, strlen( 'themes/' ) );
			$target_path = $themes_dir . '/' . $relative_path;

			// Create directory if needed
			$target_dir = dirname( $target_path );
			if ( ! is_dir( $target_dir ) ) {
				wp_mkdir_p( $target_dir );
			}

			// Extract file
			if ( ! $file_info['size'] ) {
				// Directory
				wp_mkdir_p( $target_path );
			} else {
				// File
				$file_content = $zip->getFromIndex( $i );
				if ( $file_content !== false ) {
					if ( file_put_contents( $target_path, $file_content ) !== false ) {
						if ( ! in_array( $theme_slug, $imported_themes, true ) ) {
							$imported_themes[] = $theme_slug;
						}
					} else {
						if ( ! in_array( $theme_slug, $failed_themes, true ) ) {
							$failed_themes[] = $theme_slug;
						}
					}
				}
			}
		}

		$zip->close();
		wp_delete_file( $temp_zip );

		// Activate first imported theme if requested
		$activated_theme = '';
		if ( $activate_theme && ! empty( $imported_themes ) ) {
			$theme_to_activate = $imported_themes[0];
			$theme = wp_get_theme( $theme_to_activate );
			
			if ( $theme->exists() ) {
				switch_theme( $theme_to_activate );
				$activated_theme = $theme->get( 'Name' );
			}
		}

		$message = sprintf(
			esc_html__( 'Themes import completed: %d imported, %d skipped, %d failed', 'post-export-import-with-media' ),
			count( $imported_themes ),
			count( $skipped_themes ),
			count( $failed_themes )
		);

		if ( $activated_theme ) {
			$message .= sprintf( esc_html__( '. Activated theme: %s', 'post-export-import-with-media' ), $activated_theme );
		}

		return array(
			'success' => true,
			'message' => $message,
			'imported_count' => count( $imported_themes ),
			'skipped_count' => count( $skipped_themes ),
			'failed_count' => count( $failed_themes ),
			'imported_themes' => $imported_themes,
			'skipped_themes' => $skipped_themes,
			'failed_themes' => $failed_themes,
			'activated_theme' => $activated_theme,
		);
	}

	/**
	 * Import plugins from ZIP file
	 *
	 * @param array $uploaded_file Uploaded file data
	 * @param bool  $replace_existing Whether to replace existing plugins
	 * @param bool  $activate_plugins Whether to activate imported plugins
	 * @return array Import result
	 */
	private function import_plugins_from_zip( $uploaded_file, $replace_existing = true, $activate_plugins = false ) {
		if ( ! class_exists( 'ZipArchive' ) ) {
			return array(
				'success' => false,
				'message' => esc_html__( 'ZipArchive class not available', 'post-export-import-with-media' ),
			);
		}

		$upload_dir = wp_upload_dir();
		$temp_dir = $upload_dir['basedir'] . '/peiwm-temp/';
		
		if ( ! wp_mkdir_p( $temp_dir ) ) {
			return array(
				'success' => false,
				'message' => esc_html__( 'Failed to create temporary directory', 'post-export-import-with-media' ),
			);
		}

		// Move uploaded file to temp directory
		$temp_zip = $temp_dir . 'plugins-import-' . time() . '.zip';
		if ( ! move_uploaded_file( $uploaded_file['tmp_name'], $temp_zip ) ) {
			return array(
				'success' => false,
				'message' => esc_html__( 'Failed to move uploaded file', 'post-export-import-with-media' ),
			);
		}

		$zip = new ZipArchive();
		if ( $zip->open( $temp_zip ) !== true ) {
			wp_delete_file( $temp_zip );
			return array(
				'success' => false,
				'message' => esc_html__( 'Failed to open ZIP file', 'post-export-import-with-media' ),
			);
		}

		$plugins_dir = WP_PLUGIN_DIR;
		$imported_plugins = array();
		$skipped_plugins = array();
		$failed_plugins = array();

		// Extract plugins
		for ( $i = 0; $i < $zip->numFiles; $i++ ) {
			$file_info = $zip->statIndex( $i );
			$file_path = $file_info['name'];

			// Check if this is a plugin file (starts with plugins/)
			if ( strpos( $file_path, 'plugins/' ) !== 0 ) {
				continue;
			}

			// Get plugin slug from path
			$relative_path = substr( $file_path, strlen( 'plugins/' ) );
			$path_parts = explode( '/', $relative_path );
			$plugin_slug = $path_parts[0];

			// Check if plugin already exists
			$plugin_dir = $plugins_dir . '/' . $plugin_slug;
			if ( ( is_dir( $plugin_dir ) || is_file( $plugin_dir . '.php' ) ) && ! $replace_existing ) {
				if ( ! in_array( $plugin_slug, $skipped_plugins, true ) ) {
					$skipped_plugins[] = $plugin_slug;
				}
				continue;
			}

			// Extract file
			$target_path = $plugins_dir . '/' . $relative_path;

			// Create directory if needed
			$target_dir = dirname( $target_path );
			if ( ! is_dir( $target_dir ) ) {
				wp_mkdir_p( $target_dir );
			}

			// Extract file
			if ( ! $file_info['size'] ) {
				// Directory
				wp_mkdir_p( $target_path );
			} else {
				// File
				$file_content = $zip->getFromIndex( $i );
				if ( $file_content !== false ) {
					if ( file_put_contents( $target_path, $file_content ) !== false ) {
						if ( ! in_array( $plugin_slug, $imported_plugins, true ) ) {
							$imported_plugins[] = $plugin_slug;
						}
					} else {
						if ( ! in_array( $plugin_slug, $failed_plugins, true ) ) {
							$failed_plugins[] = $plugin_slug;
						}
					}
				}
			}
		}

		$zip->close();
		wp_delete_file( $temp_zip );

		// Activate imported plugins if requested
		$activated_plugins = array();
		if ( $activate_plugins && ! empty( $imported_plugins ) ) {
			$all_plugins = get_plugins();
			
			foreach ( $imported_plugins as $plugin_slug ) {
				// Find the main plugin file
				foreach ( $all_plugins as $plugin_file => $plugin_data ) {
					if ( dirname( $plugin_file ) === $plugin_slug || basename( $plugin_file, '.php' ) === $plugin_slug ) {
						$result = activate_plugin( $plugin_file );
						if ( ! is_wp_error( $result ) ) {
							$activated_plugins[] = $plugin_data['Name'];
						}
						break;
					}
				}
			}
		}

		$message = sprintf(
			esc_html__( 'Plugins import completed: %d imported, %d skipped, %d failed', 'post-export-import-with-media' ),
			count( $imported_plugins ),
			count( $skipped_plugins ),
			count( $failed_plugins )
		);

		if ( ! empty( $activated_plugins ) ) {
			$message .= sprintf( esc_html__( '. Activated %d plugin(s)', 'post-export-import-with-media' ), count( $activated_plugins ) );
		}

		return array(
			'success' => true,
			'message' => $message,
			'imported_count' => count( $imported_plugins ),
			'skipped_count' => count( $skipped_plugins ),
			'failed_count' => count( $failed_plugins ),
			'imported_plugins' => $imported_plugins,
			'skipped_plugins' => $skipped_plugins,
			'failed_plugins' => $failed_plugins,
			'activated_plugins' => $activated_plugins,
		);
	}

	/**
	 * Format file size to human readable
	 *
	 * @param int $bytes File size in bytes
	 * @return string Formatted size
	 */
	private function format_file_size( $bytes ) {
		$units = array( 'B', 'KB', 'MB', 'GB', 'TB' );
		
		for ( $i = 0; $bytes > 1024; $i++ ) {
			$bytes /= 1024;
		}

		return round( $bytes, 2 ) . ' ' . $units[ $i ];
	}
}