<?php

defined( 'ABSPATH' ) || exit;

class ZACLS_Multisite {

	public static function is_network_active() {
		if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		return is_plugin_active_for_network( plugin_basename( ZACLS_PLUGIN_FILE ) );
	}

	public static function get_sites() {
		if ( ! is_multisite() ) {
			return array( get_current_blog_id() );
		}

		$sites = get_sites( array(
			'number'   => 500,
			'fields'   => 'ids',
		) );

		if ( empty( $sites ) ) {
			return array( get_current_blog_id() );
		}

		return $sites;
	}

	public static function run_on_all_sites( $callback, $args = array() ) {
		if ( ! is_multisite() ) {
			return call_user_func_array( $callback, $args );
		}

		$results = array();

		if ( function_exists( 'get_sites' ) ) {
			$sites = self::get_sites();
		} else {
			$sites = array( get_current_blog_id() );
		}

		foreach ( $sites as $site_id ) {
			switch_to_blog( $site_id );
			$results[ $site_id ] = call_user_func_array( $callback, $args );
			restore_current_blog();
		}

		return $results;
	}

	public static function network_activate_tables() {
		if ( ! is_multisite() || ! self::is_network_active() ) {
			return;
		}

		global $wpdb;
		$original_blog_id = get_current_blog_id();

		$sites = self::get_sites();
		foreach ( $sites as $site_id ) {
			switch_to_blog( $site_id );
			ZACLS_Install::create_tables();
			ZACLS_Install::set_default_options();
		}

		switch_to_blog( $original_blog_id );
	}

	public static function network_deactivate_cleanup() {
		if ( ! is_multisite() || ! self::is_network_active() ) {
			return;
		}

		global $wpdb;
		$original_blog_id = get_current_blog_id();
		$delete_data      = 'yes' === get_option( 'zacls_delete_data_on_uninstall', 'yes' );

		$sites = self::get_sites();
		foreach ( $sites as $site_id ) {
			switch_to_blog( $site_id );
			if ( $delete_data ) {
				self::drop_tables();
				self::delete_options();
			}
			wp_clear_scheduled_hook( 'zacls_daily_cleanup' );
		}

		switch_to_blog( $original_blog_id );
	}

	public static function drop_tables() {
		global $wpdb;
		$tables = array(
			'zacls_login_attempts',
			'zacls_blocked_ips',
			'zacls_2fa_codes',
			'zacls_2fa_status',
			'zacls_sessions',
			'zacls_device_fingerprints',
			'zacls_lockdown',
			'zacls_audit_log',
			'zacls_webauthn_credentials',
			'zacls_webauthn_challenges',
			'zacls_notification_center',
			'zacls_password_history',
		);

		foreach ( $tables as $table ) {
			$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}{$table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
		}
	}

	public static function delete_options() {
		global $wpdb;
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", 'zacls_%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
	}
}
