<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'wp_powersuite_is_license_valid' ) || ! wp_powersuite_is_license_valid() ) {
	$license_page_url = admin_url( 'admin.php?page=powersuite-license' );
	?>
	<div class="wp-powersuite-module-settings wpps-settings-panel" data-module-id="<?php echo esc_attr( $module_id ?? 'system-summary' ); ?>">
		<div class="notice notice-warning inline" style="margin:0;">
			<p>
				<?php esc_html_e( 'System Summary requires an active license. Activate your license to show the dashboard health widget.', 'wp-powersuite' ); ?>
				<a href="<?php echo esc_url( $license_page_url ); ?>"><?php esc_html_e( 'Activate license', 'wp-powersuite' ); ?></a>
			</p>
		</div>
	</div>
	<?php
	return;
}

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'system-summary';
}

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

// Use flat keys (section_environment, section_site_health, etc.) like disable-dashboard-widgets
// so checkboxes use the standard Ajax path that correctly saves both checked and unchecked
$default_sections = [
	'environment'  => true,
	'site_health'  => true,
	'performance' => true,
	'security'     => true,
	'database'     => true,
	'updates'      => true,
	'powersuite'   => true,
];
$has_saved = array_key_exists( 'section_environment', $settings ) || array_key_exists( 'sections', $settings );
$sections  = [];
foreach ( $default_sections as $key => $default_val ) {
	$flat_key = 'section_' . $key;
	$val      = $settings[ $flat_key ] ?? ( isset( $settings['sections'][ $key ] ) ? $settings['sections'][ $key ] : null );
	if ( ! $has_saved && $val === null ) {
		$sections[ $key ] = $default_val;
	} else {
		$sections[ $key ] = ( $val === true || $val === 1 || $val === '1' );
	}
}

$compact_mode = ! empty( $settings['compact_mode'] );

$section_labels = [
	'environment'  => __( 'Environment', 'wp-powersuite' ),
	'site_health'  => __( 'Site Health', 'wp-powersuite' ),
	'performance' => __( 'Performance', 'wp-powersuite' ),
	'security'     => __( 'Security', 'wp-powersuite' ),
	'database'     => __( 'Database', 'wp-powersuite' ),
	'updates'      => __( 'Updates', 'wp-powersuite' ),
	'powersuite'   => __( 'WP PowerSuite', 'wp-powersuite' ),
];
?>

<div class="wp-powersuite-module-settings" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<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" role="presentation">
			<tbody>
				<tr>
					<th scope="row"><?php esc_html_e( 'Visible sections', 'wp-powersuite' ); ?></th>
					<td>
						<p class="description" style="margin-bottom: 10px;"><?php esc_html_e( 'Choose which sections to show in the System Summary dashboard widget. Sections can still be collapsed/expanded on the dashboard.', 'wp-powersuite' ); ?></p>
						<fieldset>
							<?php foreach ( $section_labels as $key => $label ) : ?>
								<?php $checked = ! empty( $sections[ $key ] ); ?>
								<label style="display: block; margin-bottom: 8px;">
									<input type="checkbox"
										name="section_<?php echo esc_attr( $key ); ?>"
										class="wp-powersuite-advanced-setting"
										data-setting-key="section_<?php echo esc_attr( $key ); ?>"
										value="1"
										<?php checked( $checked ); ?>
									/>
									<?php echo esc_html( $label ); ?>
								</label>
							<?php endforeach; ?>
						</fieldset>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Compact mode', 'wp-powersuite' ); ?></th>
					<td>
						<label>
							<input type="checkbox"
								name="compact_mode"
								class="wp-powersuite-advanced-setting"
								data-setting-key="compact_mode"
								value="1"
								<?php checked( $compact_mode ); ?>
							/>
							<?php esc_html_e( 'Use compact layout (smaller text and spacing)', 'wp-powersuite' ); ?>
						</label>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
