<?php
/**
 * Multisite network activation and admin capability helpers.
 *
 * @package WP_PowerSuite\Core
 * @since 1.0.0
 */

namespace WP_PowerSuite\Core;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Detects network activation and resolves admin capabilities for OptionStore and AJAX.
 */
final class NetworkScope {

	/**
	 * Whether WP PowerSuite is active for the entire network.
	 */
	public static function isNetworkActivated(): bool {
		if ( ! is_multisite() ) {
			return false;
		}

		if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		if ( ! defined( 'WP_POWERSUITE_PLUGIN_BASENAME' ) ) {
			return false;
		}

		return is_plugin_active_for_network( WP_POWERSUITE_PLUGIN_BASENAME );
	}

	/**
	 * Whether settings should be stored in site meta (network-wide).
	 */
	public static function usesNetworkStorage(): bool {
		return self::isNetworkActivated();
	}

	/**
	 * Capability required to manage WP PowerSuite in the current context.
	 */
	public static function adminCapability(): string {
		if ( self::isNetworkActivated() ) {
			return 'manage_network_options';
		}

		return 'manage_options';
	}

	/**
	 * Whether the current user may manage WP PowerSuite admin screens.
	 */
	public static function canManagePlugin(): bool {
		return current_user_can( self::adminCapability() );
	}

	/**
	 * Base admin URL for plugin screens (site or network admin as appropriate).
	 *
	 * @param string               $path       Path relative to admin (e.g. admin.php).
	 * @param array<string, mixed> $query_args Optional query arguments.
	 */
	public static function adminUrl( string $path = 'admin.php', array $query_args = [] ): string {
		$url = self::isNetworkActivated() ? network_admin_url( $path ) : admin_url( $path );

		if ( $query_args !== [] ) {
			$url = add_query_arg( $query_args, $url );
		}

		return $url;
	}

	/**
	 * Whether the plugin admin UI should register under Network Admin.
	 */
	public static function isNetworkAdminContext(): bool {
		return is_multisite() && function_exists( 'is_network_admin' ) && is_network_admin();
	}

	/**
	 * Hook suffix prefix for admin menu registration.
	 */
	public static function adminMenuHook(): string {
		return self::isNetworkActivated() ? 'network_admin_menu' : 'admin_menu';
	}

	/**
	 * Hook suffix prefix for admin head styles.
	 */
	public static function adminHeadHook(): string {
		return self::isNetworkActivated() ? 'network_admin_head' : 'admin_head';
	}

	/**
	 * Read a plugin-owned option from the correct scope (site vs blog).
	 *
	 * @param string $option_name Option name.
	 * @param mixed  $default     Default when missing.
	 */
	public static function getPluginOption( string $option_name, mixed $default = false ): mixed {
		if ( self::usesNetworkStorage() ) {
			return get_site_option( $option_name, $default );
		}

		return get_option( $option_name, $default );
	}

	/**
	 * Write a plugin-owned option to the correct scope (site vs blog).
	 *
	 * @param string $option_name Option name.
	 * @param mixed  $value       Value to store.
	 */
	public static function updatePluginOption( string $option_name, mixed $value ): bool {
		if ( self::usesNetworkStorage() ) {
			return update_site_option( $option_name, $value );
		}

		return update_option( $option_name, $value, false );
	}

	/**
	 * Delete a plugin-owned option from both scopes when needed.
	 *
	 * @param string $option_name Option name.
	 */
	public static function deletePluginOption( string $option_name ): void {
		delete_option( $option_name );
		if ( is_multisite() ) {
			delete_site_option( $option_name );
		}
	}

	/**
	 * Whether the user opted to wipe plugin data during uninstall.
	 */
	public static function shouldDeleteOnUninstall(): bool {
		$value = self::getPluginOption( 'wp_powersuite_delete_on_uninstall', false );
		return $value === 'yes' || $value === '1';
	}
}
