<?php
/**
 * Disable All Updates Admin Page
 *
 * Admin settings page for the Disable All Updates module.
 *
 * @package DisableAllUpdates
 * @since 1.0.0
 */

namespace DisableAllUpdates;

use WP_PowerSuite\Core\NetworkScope;
use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! NetworkScope::canManagePlugin() ) {
	return;
}

if ( ! isset( $module_id ) ) {
	$module_id = 'disable-all-updates';
}

$settings         = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
$settings         = Module::sanitizeSettings( is_array( $settings ) ? $settings : [] );
$disabled_labels  = Module::getDisabledTypeLabels( $settings );
$hide_updates_on  = OptionStoreModuleSettingsRepository::isModuleEnabled( 'hide-update-notifications' );

?>

<div class="wp-powersuite-module-settings" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<div class="notice notice-warning inline wp-powersuite-disable-all-updates-warning">
		<p>
			<span class="dashicons dashicons-warning" aria-hidden="true"></span>
			<strong><?php esc_html_e( 'Security Warning:', 'wp-powersuite' ); ?></strong>
			<?php esc_html_e( 'Disabling updates blocks security patches. Only disable updates if you have a proper update management strategy in place.', 'wp-powersuite' ); ?>
		</p>
	</div>

	<?php if ( $disabled_labels !== [] ) : ?>
		<div class="notice notice-info inline wp-powersuite-disable-all-updates-status">
			<p>
				<strong><?php esc_html_e( 'Currently disabled:', 'wp-powersuite' ); ?></strong>
				<?php echo esc_html( implode( ', ', $disabled_labels ) ); ?>
			</p>
		</div>
	<?php else : ?>
		<div class="notice notice-success inline wp-powersuite-disable-all-updates-status">
			<p><?php esc_html_e( 'No update types are disabled. All WordPress update channels are active.', 'wp-powersuite' ); ?></p>
		</div>
	<?php endif; ?>

	<?php if ( $hide_updates_on ) : ?>
		<div class="notice notice-warning inline wp-powersuite-disable-all-updates-overlap">
			<p>
				<strong><?php esc_html_e( 'Note:', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'The "Hide Update Notifications" module is also active. It only hides admin reminders; this module blocks update checks and changes. Consider disabling the hide module to avoid redundant configuration.', 'wp-powersuite' ); ?>
			</p>
		</div>
	<?php endif; ?>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<p class="description wp-powersuite-disable-all-updates-intro">
			<?php esc_html_e( 'Select which types of updates you want to disable. Each option blocks both automatic and manual updates for that type in the WordPress admin, REST, and AJAX (unless noted below).', 'wp-powersuite' ); ?>
		</p>
		<p class="description wp-powersuite-disable-all-updates-intro">
			<?php esc_html_e( 'Manual patching via WP-CLI is also blocked by default. Developers can allow CLI updates with the wp_powersuite_disable_all_updates_allow_wp_cli filter. Site Health shows a security notice while any type is disabled.', 'wp-powersuite' ); ?>
		</p>

		<table class="form-table">
			<tbody>
				<tr>
					<th scope="row">
						<?php esc_html_e( 'Disable Core Updates', 'wp-powersuite' ); ?>
						<?php
						wpps_render_modal_settings_tip(
							__( 'Help: core updates', 'wp-powersuite' ),
							__( 'Blocks WordPress core automatic and manual updates. Warning: This prevents critical security patches.', 'wp-powersuite' )
						);
						?>
					</th>
					<td>
						<label>
							<input type="checkbox"
								name="settings[disable_core_updates]"
								class="wp-powersuite-advanced-setting"
								data-setting-key="disable_core_updates"
								value="1"
								<?php checked( ! empty( $settings['disable_core_updates'] ) ); ?>
							/>
							<?php esc_html_e( 'Disable WordPress core automatic and manual updates', 'wp-powersuite' ); ?>
						</label>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<?php esc_html_e( 'Disable Plugin Updates', 'wp-powersuite' ); ?>
						<?php
						wpps_render_modal_settings_tip(
							__( 'Help: plugin updates', 'wp-powersuite' ),
							__( 'Prevents plugin updates through the WordPress admin (automatic and manual). Plugin installation is not affected.', 'wp-powersuite' )
						);
						?>
					</th>
					<td>
						<label>
							<input type="checkbox"
								name="settings[disable_plugin_updates]"
								class="wp-powersuite-advanced-setting"
								data-setting-key="disable_plugin_updates"
								value="1"
								<?php checked( ! empty( $settings['disable_plugin_updates'] ) ); ?>
							/>
							<?php esc_html_e( 'Disable plugin automatic and manual updates', 'wp-powersuite' ); ?>
						</label>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<?php esc_html_e( 'Disable Theme Updates', 'wp-powersuite' ); ?>
						<?php
						wpps_render_modal_settings_tip(
							__( 'Help: theme updates', 'wp-powersuite' ),
							__( 'Prevents theme updates through the WordPress admin (automatic and manual). Theme installation is not affected.', 'wp-powersuite' )
						);
						?>
					</th>
					<td>
						<label>
							<input type="checkbox"
								name="settings[disable_theme_updates]"
								class="wp-powersuite-advanced-setting"
								data-setting-key="disable_theme_updates"
								value="1"
								<?php checked( ! empty( $settings['disable_theme_updates'] ) ); ?>
							/>
							<?php esc_html_e( 'Disable theme automatic and manual updates', 'wp-powersuite' ); ?>
						</label>
					</td>
				</tr>

				<tr>
					<th scope="row">
						<?php esc_html_e( 'Disable Translation Updates', 'wp-powersuite' ); ?>
						<?php
						wpps_render_modal_settings_tip(
							__( 'Help: translation updates', 'wp-powersuite' ),
							__( 'Prevents translation/language pack updates from downloading.', 'wp-powersuite' )
						);
						?>
					</th>
					<td>
						<label>
							<input type="checkbox"
								name="settings[disable_translation_updates]"
								class="wp-powersuite-advanced-setting"
								data-setting-key="disable_translation_updates"
								value="1"
								<?php checked( ! empty( $settings['disable_translation_updates'] ) ); ?>
							/>
							<?php esc_html_e( 'Disable translation/language pack updates', 'wp-powersuite' ); ?>
						</label>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>

<style>
.wp-powersuite-disable-all-updates-warning .dashicons {
	vertical-align: text-bottom;
	color: #dba617;
}
.wp-powersuite-disable-all-updates-intro {
	margin-bottom: 16px;
}
.wp-powersuite-disable-all-updates-status,
.wp-powersuite-disable-all-updates-overlap,
.wp-powersuite-disable-all-updates-warning {
	margin-left: 0;
	margin-bottom: 16px;
}
</style>
