<?php
/**
 * BaseCloud Boost — Tools
 *
 * @package BaseCloud\Boost
 */

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

$options = (array) get_option( BCBOOST_OPTIONS_KEY, [] );

// Show import result notice.
$import_result = isset( $_GET['BCBOOST_import'] ) ? sanitize_key( $_GET['BCBOOST_import'] ) : '';
?>
<div class="bcboost-wrap">
	<?php include __DIR__ . '/partials/header.php'; ?>

	<?php if ( $import_result === 'success' ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings imported successfully.', 'basecloud-boost' ); ?></p></div>
	<?php elseif ( $import_result === 'error' ) : ?>
		<div class="notice notice-error is-dismissible"><p><?php esc_html_e( 'Import failed. Please upload a valid BaseCloud Boost JSON settings file.', 'basecloud-boost' ); ?></p></div>
	<?php endif; ?>

	<!-- One-click PageSpeed preset -->
	<div class="bcboost-card bcboost-card--accent-purple">
		<h3><span class="bcboost-section-icon">⚡</span> <?php esc_html_e( 'Optimize for PageSpeed', 'basecloud-boost' ); ?></h3>
		<p class="bcboost-field-desc">
			<?php esc_html_e( 'Apply our recommended high-impact configuration in one click: page cache, CSS &amp; JS minify and combine, JavaScript defer plus delay-until-interaction, image lazy-load and WebP, GZIP, browser caching, and more. This is the safe profile we recommend for most sites and the fastest way to lift your Core Web Vitals. Your CDN, Cloudflare, webhook and other credentials are left untouched — you can fine-tune any setting afterwards.', 'basecloud-boost' ); ?>
		</p>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top:16px;" onsubmit="return confirm('<?php echo esc_js( __( 'Apply the recommended PageSpeed settings and clear the cache now?', 'basecloud-boost' ) ); ?>');">
			<?php wp_nonce_field( 'BCBOOST_apply_preset' ); ?>
			<input type="hidden" name="action" value="BCBOOST_apply_preset">
			<div class="bcboost-actions-row">
				<button type="submit" class="bcboost-btn bcboost-btn--primary">
					<span class="dashicons dashicons-superhero-alt"></span>
					<?php esc_html_e( 'Apply Recommended Settings', 'basecloud-boost' ); ?>
				</button>
			</div>
		</form>
		<p class="bcboost-field-desc" style="margin-top:12px;font-size:12px;opacity:.75;">
			<?php esc_html_e( 'After applying, wait a minute and re-run PageSpeed Insights. If your theme has an unusual layout, give the homepage a quick look — Combine CSS is the first switch to turn off if anything appears unstyled.', 'basecloud-boost' ); ?>
		</p>
	</div>

	<!-- Export -->
	<div class="bcboost-card bcboost-card--accent-blue">
		<h3><span class="bcboost-section-icon">📤</span> <?php esc_html_e( 'Export Settings', 'basecloud-boost' ); ?></h3>
		<p class="bcboost-field-desc"><?php esc_html_e( 'Download your current BaseCloud Boost settings as a JSON file. Use this to back up your configuration or transfer it to another site.', 'basecloud-boost' ); ?></p>
		<div class="bcboost-actions-row" style="margin-top:16px;">
			<button type="button" class="bcboost-btn bcboost-btn--primary" id="bcboost-export-settings">
				<span class="dashicons dashicons-download"></span>
				<?php esc_html_e( 'Export Settings', 'basecloud-boost' ); ?>
			</button>
		</div>
		<div id="bcboost-export-feedback" class="bcboost-feedback" style="display:none;margin-top:12px;"></div>
	</div>

	<!-- Import -->
	<div class="bcboost-card bcboost-card--accent-green">
		<h3><span class="bcboost-section-icon">📥</span> <?php esc_html_e( 'Import Settings', 'basecloud-boost' ); ?></h3>
		<p class="bcboost-field-desc"><?php esc_html_e( 'Upload a previously exported BaseCloud Boost JSON settings file to restore or copy a configuration. All current settings will be replaced.', 'basecloud-boost' ); ?></p>

		<form method="post" enctype="multipart/form-data" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top:16px;">
			<?php wp_nonce_field( 'BCBOOST_import_settings' ); ?>
			<input type="hidden" name="action" value="BCBOOST_import_settings">
			<div class="bcboost-field" style="max-width:480px;">
				<label class="bcboost-field-label" for="BCBOOST_settings_file"><?php esc_html_e( 'Settings File (.json)', 'basecloud-boost' ); ?></label>
				<input type="file" id="BCBOOST_settings_file" name="BCBOOST_settings_file" accept=".json,application/json" class="bcboost-input" style="margin-top:8px;">
			</div>
			<div class="bcboost-actions-row" style="margin-top:12px;">
				<button type="submit" class="bcboost-btn bcboost-btn--primary">
					<span class="dashicons dashicons-upload"></span>
					<?php esc_html_e( 'Import Settings', 'basecloud-boost' ); ?>
				</button>
			</div>
		</form>
	</div>

	<!-- WP-CLI -->
	<div class="bcboost-card">
		<h3><span class="bcboost-section-icon">💻</span> <?php esc_html_e( 'WP-CLI Commands', 'basecloud-boost' ); ?></h3>
		<p class="bcboost-field-desc"><?php esc_html_e( 'BaseCloud Boost registers a wp bcboost command group for managing the plugin from the command line. This is ideal for automated deployments and server maintenance scripts.', 'basecloud-boost' ); ?></p>

		<div style="margin-top:16px;">
			<table class="widefat striped" style="margin-top:0;">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Command', 'basecloud-boost' ); ?></th>
						<th><?php esc_html_e( 'Description', 'basecloud-boost' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<tr><td><code>wp bcboost cache clear</code></td><td><?php esc_html_e( 'Purge the entire page cache.', 'basecloud-boost' ); ?></td></tr>
					<tr><td><code>wp bcboost cache clear-url &lt;url&gt;</code></td><td><?php esc_html_e( 'Purge cache for a single URL.', 'basecloud-boost' ); ?></td></tr>
					<tr><td><code>wp bcboost cache warm</code></td><td><?php esc_html_e( 'Crawl and warm the entire cache in the background.', 'basecloud-boost' ); ?></td></tr>
					<tr><td><code>wp bcboost cache status</code></td><td><?php esc_html_e( 'Show cache statistics (size, file count, hit rate).', 'basecloud-boost' ); ?></td></tr>
					<tr><td><code>wp bcboost db cleanup [--task=&lt;name&gt;]</code></td><td><?php esc_html_e( 'Run database cleanup tasks (revisions, transients, spam, trash).', 'basecloud-boost' ); ?></td></tr>
					<tr><td><code>wp bcboost settings export [--file=&lt;path&gt;]</code></td><td><?php esc_html_e( 'Export plugin settings to a JSON file.', 'basecloud-boost' ); ?></td></tr>
					<tr><td><code>wp bcboost settings import &lt;file&gt;</code></td><td><?php esc_html_e( 'Import plugin settings from a JSON file.', 'basecloud-boost' ); ?></td></tr>
				</tbody>
			</table>
		</div>
	</div>
</div>

<?php // Export settings script is enqueued via wp_add_inline_script in Admin_Controller. ?>
