<?php
/**
 * Page de réglages Core Version Switcher.
 *
 * @package CVS
 * @license GPL-2.0-or-later
 * @var array       $settings
 * @var string      $context
 * @var bool        $is_readonly
 * @var string      $zip_url
 * @var bool|null   $zip_ok
 * @var array       $log
 * @var array       $presets
 * @var array       $releases
 * @var array       $diagnostics
 * @var array       $insights
 * @var string      $rollback
 * @var string      $latest
 * @var array       $compat
 * @var array       $checksum
 * @var int         $pin_expires
 * @var string      $access
 * @var string      $access_label
 */

defined( 'ABSPATH' ) || exit;

$plugin        = CVS_Plugin::instance();
$is_network    = ( 'network' === ( $context ?? 'site' ) );
$current_ver   = get_bloginfo( 'version' );
$target        = CVS_Settings::sanitize_version( $settings['target_version'] ?? '' );
$save_url      = $is_network
	? network_admin_url( 'edit.php?action=cvs_save_network_settings' )
	: admin_url( 'admin-post.php' );
$refresh_url   = CVS_Admin::action_url( $is_network ? 'cvs_refresh_releases_network' : 'cvs_refresh_releases', $is_network );
$preset_url    = CVS_Admin::action_url( $is_network ? 'cvs_apply_preset_network' : 'cvs_apply_preset', $is_network );
$clear_url     = CVS_Admin::action_url( $is_network ? 'cvs_clear_target_network' : 'cvs_clear_target', $is_network );
$lock_url      = CVS_Admin::action_url( $is_network ? 'cvs_clear_updater_lock_network' : 'cvs_clear_updater_lock', $is_network );
$export_url    = $is_network
	? network_admin_url( 'edit.php?action=cvs_export_network&_wpnonce=' . wp_create_nonce( 'cvs_export_settings' ) )
	: admin_url( 'admin-post.php?action=cvs_export_settings&_wpnonce=' . wp_create_nonce( 'cvs_export_settings' ) );
$import_action = $is_network ? 'cvs_import_network' : 'cvs_import_settings';
$import_url    = CVS_Admin::action_url( $import_action, $is_network );
$updates_url   = $is_network ? network_admin_url( 'update-core.php' ) : admin_url( 'update-core.php' );
$cvs_doc_url   = function_exists( 'volade_extension_doc_url' ) ? volade_extension_doc_url( CVS_SLUG ) : 'https://volade.com/fr/cms/wordpress/extensions/' . CVS_SLUG;
$release_url   = 'https://wordpress.org/download/releases/';
$security      = $insights['security'] ?? '';
$access        = $access ?? ( $plugin->volade ? $plugin->volade->get_access_level() : 'guest' );
$access_label  = $access_label ?? ( $plugin->volade ? $plugin->volade->get_access_level_label( $access ) : __( 'No account', 'core-version-switcher-by-volade' ) );
$compat        = $compat ?? array(
	'errors'   => array(),
	'warnings' => array(),
);
$checksum      = $checksum ?? array();
$pin_expires   = $pin_expires ?? 0;
?>
<div class="cvs-wrap">
	<?php require CVS_DIR . 'admin/views/partials/intro-core-version.php'; ?>

	<?php require CVS_DIR . 'admin/views/partials/access-summary.php'; ?>

	<div class="cvs-page-header">
		<div>
			<h1><?php esc_html_e( 'Core Version Switcher by Volade', 'core-version-switcher-by-volade' ); ?></h1>
			<p class="description">
				<?php
				if ( $is_network ) {
					esc_html_e( 'Network policy: subsites that inherit this configuration will see the pinned version on Dashboard → Updates.', 'core-version-switcher-by-volade' );
				} else {
					esc_html_e( 'Pin WordPress to any release — safer than WP Downgrade: version picker, security badges, diagnostics, rollback.', 'core-version-switcher-by-volade' );
				}
				?>
			</p>
		</div>
		<div class="cvs-header-actions">
			<form method="post" action="<?php echo esc_url( $refresh_url ); ?>" class="cvs-inline-form">
				<?php wp_nonce_field( 'cvs_refresh_releases' ); ?>
				<button type="submit" class="button button-secondary"><?php esc_html_e( 'Refresh release list', 'core-version-switcher-by-volade' ); ?></button>
			</form>
			<a class="button button-secondary" href="<?php echo esc_url( $cvs_doc_url ); ?>" target="_blank" rel="noopener">
				<?php esc_html_e( 'Documentation', 'core-version-switcher-by-volade' ); ?>
			</a>
		</div>
	</div>

	<?php foreach ( array( 'updated', 'cleared', 'imported', 'source_updated', 'preset_applied', 'lock_cleared', 'releases_refreshed' ) as $flag ) : ?>
		<?php if ( isset( $_GET[ $flag ] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
			<div class="notice notice-success is-dismissible"><p>
				<?php
				$messages = array(
					'updated'            => __( 'Settings saved.', 'core-version-switcher-by-volade' ),
					'cleared'            => __( 'Target version cleared.', 'core-version-switcher-by-volade' ),
					'imported'           => __( 'Configuration imported.', 'core-version-switcher-by-volade' ),
					'source_updated'     => __( 'Settings source updated.', 'core-version-switcher-by-volade' ),
					'preset_applied'     => __( 'Preset applied — open Dashboard → Updates to reinstall.', 'core-version-switcher-by-volade' ),
					'lock_cleared'       => __( 'Updater lock cleared.', 'core-version-switcher-by-volade' ),
					'releases_refreshed' => __( 'Release list refreshed from wordpress.org.', 'core-version-switcher-by-volade' ),
				);
				echo esc_html( $messages[ $flag ] ?? '' );
				?>
			</p></div>
		<?php endif; ?>
	<?php endforeach; ?>

	<?php if ( isset( $_GET['cvs_error'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-error is-dismissible"><p><?php echo esc_html( wp_unslash( $_GET['cvs_error'] ) ); ?></p></div>
	<?php endif; ?>

	<?php if ( isset( $_GET['import_error'] ) || isset( $_GET['preset_error'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="notice notice-error is-dismissible"><p><?php esc_html_e( 'Action failed — check your input.', 'core-version-switcher-by-volade' ); ?></p></div>
	<?php endif; ?>

	<?php if ( $is_readonly && ! $is_network ) : ?>
		<div class="notice notice-info">
			<p><?php esc_html_e( 'This site inherits core version settings from the multisite network.', 'core-version-switcher-by-volade' ); ?></p>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="cvs-inline-form">
				<input type="hidden" name="action" value="cvs_settings_source" />
				<input type="hidden" name="cvs_source" value="local" />
				<?php wp_nonce_field( 'cvs_settings_source' ); ?>
				<button type="submit" class="button button-primary"><?php esc_html_e( 'Customize for this site', 'core-version-switcher-by-volade' ); ?></button>
			</form>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $presets ) && ! $is_readonly ) : ?>
		<div class="cvs-card cvs-presets">
			<h2><?php esc_html_e( 'Quick picks', 'core-version-switcher-by-volade' ); ?></h2>
			<p class="description"><?php esc_html_e( 'One click to set the target — then open Dashboard → Updates and click Reinstall.', 'core-version-switcher-by-volade' ); ?></p>
			<div class="cvs-preset-grid">
				<?php foreach ( $presets as $slug => $preset ) : ?>
					<form method="post" action="<?php echo esc_url( $preset_url ); ?>" class="cvs-preset-card">
						<?php wp_nonce_field( 'cvs_apply_preset' ); ?>
						<input type="hidden" name="cvs_preset" value="<?php echo esc_attr( $slug ); ?>" />
						<div class="cvs-preset-icon"><?php echo esc_html( $preset['icon'] ); ?></div>
						<strong><?php echo esc_html( $preset['label'] ); ?></strong>
						<span class="cvs-preset-version"><?php echo esc_html( $preset['version'] ); ?></span>
						<p><?php echo esc_html( $preset['desc'] ); ?></p>
						<button type="submit" class="button button-secondary"><?php esc_html_e( 'Use this version', 'core-version-switcher-by-volade' ); ?></button>
					</form>
				<?php endforeach; ?>
			</div>
		</div>
	<?php endif; ?>

	<div class="cvs-grid">
		<div class="cvs-card cvs-card-status">
			<h2><?php esc_html_e( 'Environment', 'core-version-switcher-by-volade' ); ?></h2>
			<ul class="cvs-status-list">
				<li>
					<strong><?php esc_html_e( 'Installed', 'core-version-switcher-by-volade' ); ?></strong>
					<span class="cvs-badge"><?php echo esc_html( $current_ver ); ?></span>
				</li>
				<li>
					<strong><?php esc_html_e( 'wordpress.org latest', 'core-version-switcher-by-volade' ); ?></strong>
					<span class="cvs-badge"><?php echo esc_html( $latest ); ?></span>
				</li>
				<li>
					<strong><?php esc_html_e( 'Target pin', 'core-version-switcher-by-volade' ); ?></strong>
					<span class="cvs-badge <?php echo $target ? 'cvs-badge-active' : ''; ?>">
						<?php echo $target ? esc_html( $target ) : esc_html__( 'None', 'core-version-switcher-by-volade' ); ?>
					</span>
				</li>
				<?php if ( $pin_expires ) : ?>
					<li>
						<strong><?php esc_html_e( 'Pin expires', 'core-version-switcher-by-volade' ); ?></strong>
						<span class="cvs-muted"><?php echo esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $pin_expires ) ); ?></span>
					</li>
				<?php endif; ?>
				<?php if ( $rollback ) : ?>
					<li>
						<strong><?php esc_html_e( 'Last known good', 'core-version-switcher-by-volade' ); ?></strong>
						<span class="cvs-badge"><?php echo esc_html( $rollback ); ?></span>
					</li>
				<?php endif; ?>
				<?php if ( $target && $security ) : ?>
					<li>
						<strong><?php esc_html_e( 'Security', 'core-version-switcher-by-volade' ); ?></strong>
						<span class="cvs-security cvs-security-<?php echo esc_attr( $security ); ?>"><?php echo esc_html( ucfirst( $security ) ); ?></span>
					</li>
				<?php endif; ?>
				<?php if ( $target && $zip_url ) : ?>
					<li>
						<strong><?php esc_html_e( 'Package', 'core-version-switcher-by-volade' ); ?></strong>
						<?php if ( true === $zip_ok ) : ?>
							<span class="cvs-ok"><?php esc_html_e( 'ZIP OK', 'core-version-switcher-by-volade' ); ?></span>
						<?php elseif ( false === $zip_ok ) : ?>
							<span class="cvs-warn"><?php esc_html_e( 'ZIP missing', 'core-version-switcher-by-volade' ); ?></span>
						<?php else : ?>
							<span class="cvs-muted"><?php esc_html_e( 'Could not verify', 'core-version-switcher-by-volade' ); ?></span>
						<?php endif; ?>
					</li>
				<?php endif; ?>
			</ul>

			<?php if ( $target && version_compare( $current_ver, $target, '!=' ) ) : ?>
				<p class="cvs-cta">
					<a class="button button-primary button-hero" href="<?php echo esc_url( $updates_url ); ?>">
						<?php
						printf(
							/* translators: %s: version number */
							esc_html__( 'Reinstall WordPress %s', 'core-version-switcher-by-volade' ),
							esc_html( $target )
						);
						?>
					</a>
				</p>
				<p class="description"><?php esc_html_e( 'Uses the native WordPress updater — same as WP Downgrade, but with preflight checks.', 'core-version-switcher-by-volade' ); ?></p>
			<?php elseif ( $target && version_compare( $current_ver, $target, '==' ) ) : ?>
				<p class="cvs-ok"><?php esc_html_e( 'Target matches installed core. Clear the pin to resume normal updates.', 'core-version-switcher-by-volade' ); ?></p>
			<?php endif; ?>
		</div>

		<div class="cvs-card">
			<h2><?php esc_html_e( 'Target version', 'core-version-switcher-by-volade' ); ?></h2>

			<?php if ( $is_readonly ) : ?>
				<p><?php esc_html_e( 'Read-only — network policy is active on this site.', 'core-version-switcher-by-volade' ); ?></p>
			<?php else : ?>
				<form method="post" action="<?php echo esc_url( $save_url ); ?>" id="cvs-settings-form">
					<?php
					if ( $is_network ) {
						wp_nonce_field( 'cvs_save_network_settings' );
					} else {
						wp_nonce_field( 'cvs_save_settings' );
						echo '<input type="hidden" name="action" value="cvs_save_settings" />';
					}
					?>
					<table class="form-table" role="presentation">
						<tr>
							<th scope="row"><label for="cvs_target_version"><?php esc_html_e( 'WordPress version', 'core-version-switcher-by-volade' ); ?></label></th>
							<td>
								<input type="text" class="regular-text" id="cvs_target_version" name="cvs[target_version]" value="<?php echo esc_attr( $target ); ?>" placeholder="6.7.5" list="cvs-version-list" autocomplete="off" />
								<datalist id="cvs-version-list">
									<?php foreach ( (array) $releases as $row ) : ?>
										<option value="<?php echo esc_attr( $row['version'] ); ?>"><?php echo esc_html( $row['status'] ); ?></option>
									<?php endforeach; ?>
								</datalist>
								<p class="description">
									<?php esc_html_e( 'Type or pick from recent releases — no more copy-paste from the archive.', 'core-version-switcher-by-volade' ); ?>
									<a href="<?php echo esc_url( $release_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Full archive', 'core-version-switcher-by-volade' ); ?></a>
								</p>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="cvs_package_type"><?php esc_html_e( 'Package type', 'core-version-switcher-by-volade' ); ?></label></th>
							<td>
								<select id="cvs_package_type" name="cvs[package_type]">
									<option value="full" <?php selected( ( $settings['package_type'] ?? 'full' ), 'full' ); ?>><?php esc_html_e( 'Full (default)', 'core-version-switcher-by-volade' ); ?></option>
									<option value="no_content" <?php selected( ( $settings['package_type'] ?? '' ), 'no_content' ); ?>><?php esc_html_e( 'No content (wp-content untouched)', 'core-version-switcher-by-volade' ); ?></option>
								</select>
							</td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Safety', 'core-version-switcher-by-volade' ); ?></th>
							<td>
								<label><input type="checkbox" name="cvs[allow_insecure_targets]" value="1" <?php checked( ! empty( $settings['allow_insecure_targets'] ) ); ?> /> <?php esc_html_e( 'Allow insecure targets (wordpress.org flagged)', 'core-version-switcher-by-volade' ); ?></label><br />
								<label><input type="checkbox" name="cvs[show_insecure_in_picker]" value="1" <?php checked( ! empty( $settings['show_insecure_in_picker'] ) ); ?> /> <?php esc_html_e( 'Show insecure releases in version picker', 'core-version-switcher-by-volade' ); ?></label><br />
								<label><input type="checkbox" name="cvs[require_backup_ack]" value="1" <?php checked( ! empty( $settings['require_backup_ack'] ) ); ?> /> <?php esc_html_e( 'Require backup confirmation before saving a target', 'core-version-switcher-by-volade' ); ?></label>
								<?php if ( ! empty( $settings['require_backup_ack'] ) && ! $is_readonly ) : ?>
									<p><label><input type="checkbox" name="cvs[backup_ack]" value="1" /> <?php esc_html_e( 'I have a full backup (files + database)', 'core-version-switcher-by-volade' ); ?></label></p>
								<?php endif; ?>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="cvs_pin_expires_days"><?php esc_html_e( 'Auto-clear pin', 'core-version-switcher-by-volade' ); ?></label></th>
							<td>
								<input type="number" min="0" max="365" id="cvs_pin_expires_days" name="cvs[pin_expires_days]" value="<?php echo esc_attr( (int) ( $settings['pin_expires_days'] ?? 0 ) ); ?>" class="small-text" />
								<span class="description"><?php esc_html_e( 'days (0 = never)', 'core-version-switcher-by-volade' ); ?></span>
							</td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Notifications', 'core-version-switcher-by-volade' ); ?></th>
							<td>
								<label><input type="checkbox" name="cvs[email_on_switch]" value="1" <?php checked( ! empty( $settings['email_on_switch'] ) ); ?> /> <?php esc_html_e( 'Email admin after successful core switch', 'core-version-switcher-by-volade' ); ?></label>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="cvs_package_locale"><?php esc_html_e( 'Package locale', 'core-version-switcher-by-volade' ); ?></label></th>
							<td>
								<input type="text" class="regular-text" id="cvs_package_locale" name="cvs[package_locale]" value="<?php echo esc_attr( $settings['package_locale'] ?? '' ); ?>" placeholder="<?php echo esc_attr( get_locale() ); ?>" />
								<p class="description"><?php esc_html_e( 'Leave empty for site locale. Use fr_FR, de_DE, etc. for localized core ZIP.', 'core-version-switcher-by-volade' ); ?></p>
							</td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'While pinned', 'core-version-switcher-by-volade' ); ?></th>
							<td>
								<label><input type="checkbox" name="cvs[block_auto_core_updates]" value="1" <?php checked( ! empty( $settings['block_auto_core_updates'] ) ); ?> /> <?php esc_html_e( 'Block automatic core updates (recommended while a target is set)', 'core-version-switcher-by-volade' ); ?></label>
							</td>
						</tr>
						<?php if ( ! empty( $settings['show_warnings'] ) ) : ?>
							<tr>
								<th scope="row"><?php esc_html_e( 'Before you switch', 'core-version-switcher-by-volade' ); ?></th>
								<td>
									<ul class="cvs-warnings">
										<li><?php esc_html_e( 'Full backup (files + database) — DB schema is not rolled back.', 'core-version-switcher-by-volade' ); ?></li>
										<li><?php esc_html_e( 'Test on staging first when possible.', 'core-version-switcher-by-volade' ); ?></li>
										<li><?php esc_html_e( 'Insecure releases are flagged — use only for short diagnostics.', 'core-version-switcher-by-volade' ); ?></li>
									</ul>
								</td>
							</tr>
						<?php endif; ?>
						<?php if ( $plugin->has_feature( 'custom_download_url' ) ) : ?>
							<tr>
								<th scope="row"><?php esc_html_e( 'Custom ZIP URL', 'core-version-switcher-by-volade' ); ?></th>
								<td>
									<label><input type="checkbox" name="cvs[use_custom_url]" value="1" <?php checked( ! empty( $settings['use_custom_url'] ) ); ?> /> <?php esc_html_e( 'Override download URL (wordpress.org hosts only)', 'core-version-switcher-by-volade' ); ?></label>
									<br />
									<input type="url" class="large-text" name="cvs[custom_download_url]" value="<?php echo esc_attr( $settings['custom_download_url'] ?? '' ); ?>" />
								</td>
							</tr>
						<?php endif; ?>
						<?php if ( $plugin->has_feature( 'auto_clear_target' ) ) : ?>
							<tr>
								<th scope="row"><?php esc_html_e( 'After switch', 'core-version-switcher-by-volade' ); ?></th>
								<td>
									<label><input type="checkbox" name="cvs[auto_clear_target]" value="1" <?php checked( ! empty( $settings['auto_clear_target'] ) ); ?> /> <?php esc_html_e( 'Clear target automatically after successful reinstall', 'core-version-switcher-by-volade' ); ?></label>
								</td>
							</tr>
						<?php endif; ?>
					</table>
					<?php submit_button( __( 'Save target version', 'core-version-switcher-by-volade' ) ); ?>
				</form>

				<?php if ( $target ) : ?>
					<form method="post" action="<?php echo esc_url( $clear_url ); ?>" class="cvs-inline-form" onsubmit="return confirm('<?php echo esc_js( __( 'Clear the pinned version?', 'core-version-switcher-by-volade' ) ); ?>');">
						<?php wp_nonce_field( 'cvs_clear_target' ); ?>
						<button type="submit" class="button button-secondary"><?php esc_html_e( 'Clear target', 'core-version-switcher-by-volade' ); ?></button>
					</form>
				<?php endif; ?>
			<?php endif; ?>
		</div>
	</div>

	<?php if ( $target && ( ! empty( $compat['warnings'] ) || ! empty( $compat['errors'] ) ) ) : ?>
		<div class="cvs-card cvs-compat">
			<h2><?php esc_html_e( 'Plugin & theme preflight', 'core-version-switcher-by-volade' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Active plugins and theme headers vs your target — WP Downgrade does not check this.', 'core-version-switcher-by-volade' ); ?></p>
			<?php if ( ! empty( $compat['errors'] ) ) : ?>
				<ul class="cvs-compat-errors">
					<?php foreach ( $compat['errors'] as $line ) : ?>
						<li><?php echo esc_html( $line ); ?></li>
					<?php endforeach; ?>
				</ul>
			<?php endif; ?>
			<?php if ( ! empty( $compat['warnings'] ) ) : ?>
				<ul class="cvs-compat-warnings">
					<?php foreach ( $compat['warnings'] as $line ) : ?>
						<li><?php echo esc_html( $line ); ?></li>
					<?php endforeach; ?>
				</ul>
			<?php endif; ?>
			<?php if ( CVS_Compat::pcc_plugin_active() ) : ?>
				<p><a class="button button-secondary" href="<?php echo esc_url( CVS_Compat::pcc_admin_url() ); ?>"><?php esc_html_e( 'Run PHP Compatibility Checker', 'core-version-switcher-by-volade' ); ?></a></p>
			<?php else : ?>
				<p class="description"><?php esc_html_e( 'Tip: install PHP Compatibility Checker by Volade to scan PHP version fit before downgrading.', 'core-version-switcher-by-volade' ); ?></p>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $checksum ) && ! empty( $checksum['version'] ) ) : ?>
		<div class="cvs-card">
			<h2><?php esc_html_e( 'Last checksum check', 'core-version-switcher-by-volade' ); ?></h2>
			<p>
				<?php if ( ! empty( $checksum['ok'] ) ) : ?>
					<span class="cvs-ok"><?php esc_html_e( 'Core files match wordpress.org checksums (spot check).', 'core-version-switcher-by-volade' ); ?></span>
				<?php else : ?>
					<span class="cvs-warn"><?php esc_html_e( 'Checksum mismatch or unavailable — verify manually.', 'core-version-switcher-by-volade' ); ?></span>
				<?php endif; ?>
				<span class="cvs-muted"><?php echo esc_html( $checksum['time'] ?? '' ); ?></span>
			</p>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $diagnostics['checks'] ) ) : ?>
		<div class="cvs-card cvs-diagnostics">
			<h2><?php esc_html_e( 'Diagnostics', 'core-version-switcher-by-volade' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Why the Reinstall button might be missing — WP Downgrade only documents this in FAQ; we check it for you.', 'core-version-switcher-by-volade' ); ?></p>
			<ul class="cvs-diag-list">
				<?php foreach ( $diagnostics['checks'] as $check ) : ?>
					<li class="cvs-diag-<?php echo esc_attr( $check['status'] ); ?>">
						<span class="cvs-diag-label"><?php echo esc_html( $check['label'] ); ?></span>
						<span class="cvs-diag-msg"><?php echo esc_html( $check['message'] ); ?></span>
					</li>
				<?php endforeach; ?>
			</ul>
			<?php if ( get_option( 'core_updater.lock' ) ) : ?>
				<form method="post" action="<?php echo esc_url( $lock_url ); ?>" class="cvs-inline-form">
					<?php wp_nonce_field( 'cvs_clear_updater_lock' ); ?>
					<button type="submit" class="button"><?php esc_html_e( 'Clear stuck updater lock', 'core-version-switcher-by-volade' ); ?></button>
				</form>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<?php if ( ! $is_readonly ) : ?>
		<div class="cvs-card cvs-card-tools">
			<h2><?php esc_html_e( 'Import / export', 'core-version-switcher-by-volade' ); ?></h2>
			<p class="description"><?php esc_html_e( 'JSON backup of pin settings — free, no Volade account required.', 'core-version-switcher-by-volade' ); ?></p>
			<p>
				<a class="button" href="<?php echo esc_url( $export_url ); ?>">
					<?php esc_html_e( 'Export JSON', 'core-version-switcher-by-volade' ); ?>
				</a>
			</p>
			<form method="post" action="<?php echo esc_url( $import_url ); ?>" enctype="multipart/form-data">
				<?php wp_nonce_field( 'cvs_import_settings' ); ?>
				<input type="file" name="cvs_import_file" accept="application/json,.json" required />
				<?php submit_button( __( 'Import JSON', 'core-version-switcher-by-volade' ), 'secondary', 'submit', false ); ?>
			</form>
			<?php if ( $plugin->has_feature( 'switch_log' ) ) : ?>
				<p class="description"><?php esc_html_e( 'V+ Premium: extended switch log (50 entries).', 'core-version-switcher-by-volade' ); ?></p>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $log ) ) : ?>
		<div class="cvs-card">
			<h2><?php esc_html_e( 'Switch log', 'core-version-switcher-by-volade' ); ?></h2>
			<table class="widefat striped">
				<thead><tr><th><?php esc_html_e( 'Time', 'core-version-switcher-by-volade' ); ?></th><th><?php esc_html_e( 'Action', 'core-version-switcher-by-volade' ); ?></th><th><?php esc_html_e( 'Details', 'core-version-switcher-by-volade' ); ?></th></tr></thead>
				<tbody>
					<?php foreach ( array_reverse( $log ) as $entry ) : ?>
						<tr>
							<td><?php echo esc_html( $entry['time'] ?? '' ); ?></td>
							<td><?php echo esc_html( $entry['action'] ?? '' ); ?></td>
							<td><code><?php echo esc_html( wp_json_encode( array_diff_key( $entry, array_flip( array( 'time', 'action' ) ) ) ) ); ?></code></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	<?php endif; ?>
</div>
