<?php
namespace DisableAutoUpdateEmails;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! isset( $module_id ) ) {
	$module_id = 'disable-auto-update-emails';
}

$default_settings = ( new Module() )->getDefaultSettings();

$settings = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $settings ) ) {
	$settings = [];
}

// Back-compat: older saves used disable_all_updates for this module.
if ( ! isset( $settings['disable_all_update_emails'] ) && ! empty( $settings['disable_all_updates'] ) ) {
	$settings['disable_all_update_emails'] = true;
}

$settings = array_merge( $default_settings, $settings );

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Control which update notification emails WordPress sends after automatic core, plugin, or theme updates.', 'wp-powersuite' ); ?>
	</p>

	<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' ); ?>

		<table class="form-table">
			<tbody>
				<tr>
					<th scope="row"><?php esc_html_e( 'Disable Update Emails:', 'wp-powersuite' ); ?></th>
					<td>
						<label>
							<input type="checkbox"
								name="disable_all_update_emails"
								class="wp-powersuite-advanced-setting"
								data-setting-key="disable_all_update_emails"
								value="1"
								<?php checked( ! empty( $settings['disable_all_update_emails'] ) ); ?>
							/>
							<strong><?php esc_html_e( 'Disable All Update Emails', 'wp-powersuite' ); ?></strong>
						</label>
						<p class="description"><?php esc_html_e( 'Disables all automatic update notification emails (core, plugins, and themes), including core availability and debug emails.', 'wp-powersuite' ); ?></p>

						<div class="wp-powersuite-disable-update-emails-individual">
							<p><strong><?php esc_html_e( 'Or disable individually:', 'wp-powersuite' ); ?></strong></p>

							<label class="wp-powersuite-disable-update-emails-option">
								<input type="checkbox"
									name="disable_core_updates"
									class="wp-powersuite-advanced-setting"
									data-setting-key="disable_core_updates"
									value="1"
									<?php checked( ! empty( $settings['disable_core_updates'] ) ); ?>
									<?php disabled( ! empty( $settings['disable_all_update_emails'] ) ); ?>
								/>
								<?php esc_html_e( 'Disable WordPress Core Update Emails', 'wp-powersuite' ); ?>
							</label>

							<label class="wp-powersuite-disable-update-emails-option">
								<input type="checkbox"
									name="disable_plugin_updates"
									class="wp-powersuite-advanced-setting"
									data-setting-key="disable_plugin_updates"
									value="1"
									<?php checked( ! empty( $settings['disable_plugin_updates'] ) ); ?>
									<?php disabled( ! empty( $settings['disable_all_update_emails'] ) ); ?>
								/>
								<?php esc_html_e( 'Disable Plugin Update Emails', 'wp-powersuite' ); ?>
							</label>

							<label class="wp-powersuite-disable-update-emails-option">
								<input type="checkbox"
									name="disable_theme_updates"
									class="wp-powersuite-advanced-setting"
									data-setting-key="disable_theme_updates"
									value="1"
									<?php checked( ! empty( $settings['disable_theme_updates'] ) ); ?>
									<?php disabled( ! empty( $settings['disable_all_update_emails'] ) ); ?>
								/>
								<?php esc_html_e( 'Disable Theme Update Emails', 'wp-powersuite' ); ?>
							</label>
						</div>
					</td>
				</tr>
			</tbody>
		</table>

		<p class="description wp-powersuite-disable-update-emails-note">
			<strong><?php esc_html_e( 'Note:', 'wp-powersuite' ); ?></strong>
			<?php esc_html_e( 'This only disables notification emails. WordPress will still perform automatic updates if enabled. Core emails include post-update notices, “update available” alerts, and development debug summaries. Admin update notices in the dashboard are not affected.', 'wp-powersuite' ); ?>
		</p>
	</form>
</div>

<style>
.wp-powersuite-disable-update-emails-individual {
	margin-top: 20px;
}
.wp-powersuite-disable-update-emails-option {
	display: block;
	margin-top: 10px;
}
.wp-powersuite-disable-update-emails-note {
	margin-top: 20px;
	padding: 10px;
	background: #e7f3ff;
	border-left: 4px solid #2271b1;
}
</style>
