<?php
/**
 * Active Plugins First Module
 *
 * @package ActivePluginsFirst
 * @since 1.0.0
 */

namespace ActivePluginsFirst;

use WP_PowerSuite\Core\AbstractModule;

class Module extends AbstractModule {

	private const MODULE_ID = 'active-plugins-first';

	private const MODULE_VERSION = '1.2.0';

	/**
	 * List table views where plugins are already grouped; sorting is redundant.
	 *
	 * @var list<string>
	 */
	private const SKIP_SORT_STATUSES = [
		'active',
		'inactive',
		'recently_activated',
		'mustuse',
		'dropins',
	];

	protected function registerHooks(): void {
		add_action( 'pre_current_active_plugins', [ $this, 'sortPluginsList' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAssets' ] );
	}

	/**
	 * Sort installed-plugins list table: active (or network-active) first, inactive after.
	 *
	 * Runs immediately before the table is rendered on plugins.php (site and network admin).
	 */
	public function sortPluginsList(): void {
		global $wp_list_table, $status;

		if ( ! $this->canManagePluginsList() ) {
			return;
		}

		if ( in_array( $status, self::SKIP_SORT_STATUSES, true ) ) {
			return;
		}

		if ( ! is_object( $wp_list_table ) || ! is_array( $wp_list_table->items ) || $wp_list_table->items === [] ) {
			return;
		}

		$screen  = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		$network = $screen && $screen->in_admin( 'network' );

		$wp_list_table->items = $this->sortPluginItems( $wp_list_table->items, $network );
	}

	/**
	 * @param array<string, array<string, mixed>> $items Plugin list table items.
	 * @return array<string, array<string, mixed>>
	 */
	private function sortPluginItems( array $items, bool $network ): array {
		$active   = [];
		$inactive = [];

		foreach ( $items as $plugin_file => $plugin_data ) {
			$is_active = $network
				? is_plugin_active_for_network( $plugin_file )
				: is_plugin_active( $plugin_file );

			if ( $is_active ) {
				$active[ $plugin_file ] = $plugin_data;
			} else {
				$inactive[ $plugin_file ] = $plugin_data;
			}
		}

		return array_merge( $active, $inactive );
	}

	/**
	 * Whether the current user may reorder the plugins list on this screen.
	 */
	private function canManagePluginsList(): bool {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;

		if ( $screen && $screen->id === 'plugins-network' ) {
			return current_user_can( 'manage_network_plugins' );
		}

		return current_user_can( 'activate_plugins' );
	}

	/**
	 * Enqueues section-heading assets on plugins.php (site and network).
	 */
	public function enqueueAssets( string $hook ): void {
		if ( $hook !== 'plugins.php' ) {
			return;
		}

		if ( ! $this->canManagePluginsList() ) {
			return;
		}

		$base = defined( 'WP_POWERSUITE_PLUGIN_URL' ) ? WP_POWERSUITE_PLUGIN_URL : '';
		$url  = $base . 'modules/' . self::MODULE_ID . '/assets/';

		wp_enqueue_style(
			'wpps-active-plugins-first',
			$url . 'active-plugins-first.css',
			[],
			self::MODULE_VERSION
		);

		wp_enqueue_script(
			'wpps-active-plugins-first',
			$url . 'active-plugins-first.js',
			[],
			self::MODULE_VERSION,
			true
		);

		wp_localize_script(
			'wpps-active-plugins-first',
			'wppsActivePluginsFirst',
			[
				'strings' => [
					'active'   => __( 'Active Plugins', 'wp-powersuite' ),
					'inactive' => __( 'Inactive Plugins', 'wp-powersuite' ),
					'toggle'   => __( 'Toggle section', 'wp-powersuite' ),
				],
			]
		);
	}

	public function getId(): string {
		return self::MODULE_ID;
	}

	public function getName(): string {
		return __( 'Display Active Plugins First', 'wp-powersuite' );
	}

	public function getVersion(): string {
		return self::MODULE_VERSION;
	}

	public function getDescription(): string {
		return __( 'Active plugins first on the Plugins screen; inactive plugins grouped below.', 'wp-powersuite' );
	}

	public function getContext(): string {
		return 'admin';
	}
}
