<?php
/**
 * BaseCloud Boost — File Optimization Settings
 *
 * @package BaseCloud\Boost
 */

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

$options = (array) get_option( BCBOOST_OPTIONS_KEY, [] );
?>
<div class="bcboost-wrap">
	<?php include __DIR__ . '/partials/header.php'; ?>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<?php wp_nonce_field( 'BCBOOST_save_settings' ); ?>
		<input type="hidden" name="action" value="BCBOOST_save_settings">
		<input type="hidden" name="BCBOOST_tab_checkboxes" value="minify_html,minify_css,combine_css,lazy_css,minify_js,minify_inline_js,combine_js,defer_js,delay_js,delay_js_safe_mode<?php echo defined( 'ELEMENTOR_VERSION' ) ? ',elementor_speed,elementor_inline_post_css,elementor_drop_unused_icons,elementor_settings_advisor,elementor_dom_warnings' : ''; ?>">

		<div class="bcboost-card bcboost-card--accent-purple">
			<h3><span class="bcboost-section-icon">📝</span> <?php esc_html_e( 'HTML Optimization', 'basecloud-boost' ); ?></h3>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Minify HTML', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Remove unnecessary whitespace and comments from HTML output.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="minify_html" value="1" <?php checked( ! empty( $options['minify_html'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>
		</div>

		<div class="bcboost-card bcboost-card--accent-blue">
			<h3><span class="bcboost-section-icon">🎨</span> <?php esc_html_e( 'CSS Optimization', 'basecloud-boost' ); ?></h3>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Minify CSS', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Remove whitespace and comments from CSS files to reduce file size.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="minify_css" value="1" <?php checked( ! empty( $options['minify_css'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Combine CSS Files', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc">
						<?php esc_html_e( 'Concatenate all CSS files into a single request to reduce HTTP overhead.', 'basecloud-boost' ); ?>
						<span class="bcboost-field-warning">⚠️ <?php esc_html_e( 'May cause issues with some page builders. Test thoroughly.', 'basecloud-boost' ); ?></span>
					</span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="combine_css" value="1" <?php checked( ! empty( $options['combine_css'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Async-Load Non-Critical CSS (Lazy CSS)', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc">
						<?php esc_html_e( 'Loads stylesheets without blocking the first paint (media="print" + onload swap). This is the biggest mobile First-Contentful-Paint lever on CSS-heavy sites. When Combine CSS is also on, the single combined bundle is what loads asynchronously — one non-blocking request. Per-page builder CSS (Elementor post-*.css, Divi et-cache) always stays render-blocking.', 'basecloud-boost' ); ?>
						<?php
						$bcb_ccss_cls        = '\BaseCloud\Boost\Optimizer\Critical_CSS';
						$bcb_ccss_available  = class_exists( $bcb_ccss_cls ) && $bcb_ccss_cls::any_css_configured();
						$bcb_ccss_on         = ! empty( $options['critical_css_enabled'] );
						$bcb_ccss_stale_flag = class_exists( $bcb_ccss_cls ) ? $bcb_ccss_cls::get_stale() : null;
						if ( ! $bcb_ccss_on || ! $bcb_ccss_available ) :
							$bcb_reason = ! $bcb_ccss_on
								? __( 'the "Enable Critical CSS" toggle is off (Advanced → Critical CSS)', 'basecloud-boost' )
								: __( 'no critical CSS is stored yet — fill in a template or the site-wide fallback (Advanced → Critical CSS)', 'basecloud-boost' );
							?>
							<span class="bcboost-field-warning">🔒 <?php
							printf(
								/* translators: %s: why the async gate is closed */
								esc_html__( 'Currently INACTIVE because %s. To avoid breaking page-builder layouts (the async-without-critical bug: headings/overlays render unstyled until the bundle arrives), Async CSS only takes effect on pages covered by Critical CSS. Until then CSS loads normally even with this on.', 'basecloud-boost' ),
								esc_html( $bcb_reason )
							);
							?></span>
						<?php else :
							$bcb_ccss_tpls = array_keys( array_filter(
								$bcb_ccss_cls::get_store()['templates'],
								static function ( $e ) { return ! empty( $e['css'] ); }
							) );
							?>
							<span class="bcboost-field-desc" style="display:block;color:#00a32a;">✔ <?php
							if ( ! empty( $bcb_ccss_tpls ) ) {
								printf(
									/* translators: %s: template list */
									esc_html__( 'Active on pages covered by Critical CSS. Templates stored: %s. Other pages fall back to the site-wide CSS if set, otherwise their CSS stays render-blocking (safe).', 'basecloud-boost' ),
									esc_html( implode( ', ', $bcb_ccss_tpls ) )
								);
							} else {
								esc_html_e( 'Active via the site-wide fallback Critical CSS (no per-template CSS stored yet — Advanced → Per-Template Critical CSS gives better coverage on builder sites).', 'basecloud-boost' );
							}
							?></span>
							<?php if ( null !== $bcb_ccss_stale_flag ) : ?>
								<span class="bcboost-field-warning">⚠️ <?php esc_html_e( 'Critical CSS is flagged possibly stale (theme/builder CSS changed) — review it in Advanced → Per-Template Critical CSS before trusting async delivery.', 'basecloud-boost' ); ?></span>
							<?php endif; ?>
						<?php endif; ?>
					</span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="lazy_css" value="1" <?php checked( ! empty( $options['lazy_css'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="exclude_css"><?php esc_html_e( 'Exclude CSS Files', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'Partial URL patterns for CSS files to exclude from minification/combining. One per line.', 'basecloud-boost' ); ?></span>
				<textarea id="exclude_css" name="exclude_css" class="bcboost-textarea bcboost-input--wide" style="margin-top:10px;"><?php echo esc_textarea( $options['exclude_css'] ?? '' ); ?></textarea>
			</div>
		</div>

		<div class="bcboost-card bcboost-card--accent-green">
			<h3><span class="bcboost-section-icon">⚡</span> <?php esc_html_e( 'JavaScript Optimization', 'basecloud-boost' ); ?></h3>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Minify JavaScript', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Remove whitespace and comments from JS files.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="minify_js" value="1" <?php checked( ! empty( $options['minify_js'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Minify Inline Scripts (advanced)', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc">
						<?php esc_html_e( 'Also minify inline <script> blocks embedded in the page HTML. After gzip this saves almost nothing, while every theme, page-builder and form initialisation script is rewritten — the risk far outweighs the reward. Leave off unless you know exactly why you need it.', 'basecloud-boost' ); ?>
						<span class="bcboost-field-warning">⚠️ <?php esc_html_e( 'Risky: can break sliders, forms and builder widgets if an inline script is mangled. Off by default.', 'basecloud-boost' ); ?></span>
					</span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="minify_inline_js" value="1" <?php checked( ! empty( $options['minify_inline_js'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Combine JavaScript Files', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc">
						<?php esc_html_e( 'Merge JavaScript files into a single request.', 'basecloud-boost' ); ?>
						<span class="bcboost-field-warning">⚠️ <?php esc_html_e( 'May break scripts with execution-order dependencies.', 'basecloud-boost' ); ?></span>
					</span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="combine_js" value="1" <?php checked( ! empty( $options['combine_js'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Defer JavaScript Loading', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Run scripts through an order-safe loader at DOMContentLoaded instead of during parsing — faster first paint. Inline and external scripts run in their original order, so sliders, page-builder widgets and localized config keep working. When Delay JS is also on, Delay takes over the timing.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="defer_js" value="1" <?php checked( ! empty( $options['defer_js'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Delay JavaScript Execution', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Hold JavaScript until the first user interaction (move, tap, scroll, key), with a fallback for passive visitors. The biggest Total-Blocking-Time / PageSpeed win. With Safe Mode on (below), only third-party scripts are held, so the site cannot break.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="delay_js" value="1" <?php checked( ! empty( $options['delay_js'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Safe Mode — delay third-party scripts only (recommended)', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Only defer/delay known third-party scripts — analytics, tag managers, ad pixels and external chat widgets — and never touch your own JavaScript (sliders, testimonials, forms, Elementor/Divi, the theme). This keeps most of the speed gain while making Defer/Delay JS safe by design, so it cannot break interactive elements. Turn OFF only for advanced, hands-on tuning (delays everything except the exclusion lists).', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="delay_js_safe_mode" value="1" <?php checked( ! isset( $options['delay_js_safe_mode'] ) || ! empty( $options['delay_js_safe_mode'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="delay_js_excludes"><?php esc_html_e( 'Delay JS Exclusions', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'Scripts matching these patterns will NOT be delayed. One per line. e.g. /wp-includes/, gtag, analytics', 'basecloud-boost' ); ?></span>
				<textarea id="delay_js_excludes" name="delay_js_excludes" class="bcboost-textarea bcboost-input--wide" style="margin-top:10px;"><?php echo esc_textarea( $options['delay_js_excludes'] ?? '' ); ?></textarea>
			</div>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="exclude_js"><?php esc_html_e( 'Exclude JavaScript Files', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'Partial URL patterns for JS files to exclude from minification/combining/defer. One per line.', 'basecloud-boost' ); ?></span>
				<textarea id="exclude_js" name="exclude_js" class="bcboost-textarea bcboost-input--wide" style="margin-top:10px;"><?php echo esc_textarea( $options['exclude_js'] ?? '' ); ?></textarea>
			</div>
		</div>

		<div class="bcboost-card bcboost-card--accent-purple">
			<h3><span class="bcboost-section-icon">🧩</span> <?php esc_html_e( 'Elementor Speed', 'basecloud-boost' ); ?></h3>

			<?php if ( ! defined( 'ELEMENTOR_VERSION' ) ) : ?>
				<div class="bcboost-field">
					<span class="bcboost-field-desc"><?php esc_html_e( 'Elementor is not active on this site, so these optimizations are standing by. When Elementor is installed, Boost can inline its per-page CSS (removing dozens of render-blocking requests), drop unused icon-font stylesheets, and recommend the fastest Elementor settings — all without ever touching Elementor\'s JavaScript.', 'basecloud-boost' ); ?></span>
				</div>
			<?php else : ?>

				<div class="bcboost-field bcboost-field--toggle">
					<label>
						<span class="bcboost-field-label"><?php esc_html_e( 'Enable Elementor Speed', 'basecloud-boost' ); ?></span>
						<span class="bcboost-field-desc"><?php esc_html_e( 'Master switch for the Elementor-specific optimizations below. Everything is CSS-side and advisory only — Elementor\'s own JavaScript, widgets and initialisation order are never delayed, deferred or removed, so menus, sliders, popups and forms keep working exactly as before.', 'basecloud-boost' ); ?></span>
					</label>
					<label class="bcboost-toggle">
						<input type="checkbox" name="elementor_speed" value="1" <?php checked( ! empty( $options['elementor_speed'] ) ); ?>>
						<span class="bcboost-toggle-slider"></span>
					</label>
				</div>

				<div class="bcboost-field bcboost-field--toggle">
					<label>
						<span class="bcboost-field-label"><?php esc_html_e( 'Inline Per-Page Elementor CSS', 'basecloud-boost' ); ?></span>
						<span class="bcboost-field-desc"><?php esc_html_e( 'Elementor loads a small render-blocking CSS file for every page, header, footer and popup template (often 5–25 per page). Boost embeds each file\'s rules directly into the HTML at the same position — identical styling, applied before first paint, minus all those blocking requests. Files that are too large or unreadable keep loading normally. The page cache is purged automatically when Elementor regenerates its CSS or a template is saved.', 'basecloud-boost' ); ?></span>
					</label>
					<label class="bcboost-toggle">
						<input type="checkbox" name="elementor_inline_post_css" value="1" <?php checked( ! isset( $options['elementor_inline_post_css'] ) || ! empty( $options['elementor_inline_post_css'] ) ); ?>>
						<span class="bcboost-toggle-slider"></span>
					</label>
				</div>

				<div class="bcboost-field bcboost-field--toggle">
					<label>
						<span class="bcboost-field-label"><?php esc_html_e( 'Remove Unused Icon CSS', 'basecloud-boost' ); ?></span>
						<span class="bcboost-field-desc">
							<?php esc_html_e( 'Elementor always loads its eicons stylesheet (and often Font Awesome) even on pages with no icons. Boost removes those stylesheet links only when the whole page provably contains no icon classes and no feature that draws icons at runtime — any lightbox, popup, video widget or carousel keeps them loaded. When in doubt, the stylesheets stay.', 'basecloud-boost' ); ?>
							<span class="bcboost-field-warning">⚠️ <?php esc_html_e( 'If an icon ever appears missing on a specific page, turn this off and report the page — the veto list is designed to make that impossible.', 'basecloud-boost' ); ?></span>
						</span>
					</label>
					<label class="bcboost-toggle">
						<input type="checkbox" name="elementor_drop_unused_icons" value="1" <?php checked( ! isset( $options['elementor_drop_unused_icons'] ) || ! empty( $options['elementor_drop_unused_icons'] ) ); ?>>
						<span class="bcboost-toggle-slider"></span>
					</label>
				</div>

				<div class="bcboost-field bcboost-field--toggle">
					<label>
						<span class="bcboost-field-label"><?php esc_html_e( 'Elementor Settings Advisor', 'basecloud-boost' ); ?></span>
						<span class="bcboost-field-desc"><?php esc_html_e( 'Read-only recommendations based on this site\'s actual Elementor configuration (font loading, Inline Font Icons, caching conflicts). Boost never changes an Elementor setting for you — apply them in Elementor\'s own settings screens.', 'basecloud-boost' ); ?></span>
					</label>
					<label class="bcboost-toggle">
						<input type="checkbox" name="elementor_settings_advisor" value="1" <?php checked( ! isset( $options['elementor_settings_advisor'] ) || ! empty( $options['elementor_settings_advisor'] ) ); ?>>
						<span class="bcboost-toggle-slider"></span>
					</label>
				</div>

				<div class="bcboost-field bcboost-field--toggle">
					<label>
						<span class="bcboost-field-label"><?php esc_html_e( 'Oversized-Page Warnings', 'basecloud-boost' ); ?></span>
						<span class="bcboost-field-desc"><?php esc_html_e( 'Elementor layouts can accumulate thousands of nested containers, and a huge DOM slows every tap and scroll (INP) no matter how fast assets load. Boost never rewrites or restructures your markup — automated div-surgery is how optimizers break Elementor sites — but with this on it flags pages whose HTML exceeds ~400 KB so you can slim them down in Elementor (fewer nested sections, or rebuild heavy sections with Elementor V4 atomic elements).', 'basecloud-boost' ); ?></span>
					</label>
					<label class="bcboost-toggle">
						<input type="checkbox" name="elementor_dom_warnings" value="1" <?php checked( ! isset( $options['elementor_dom_warnings'] ) || ! empty( $options['elementor_dom_warnings'] ) ); ?>>
						<span class="bcboost-toggle-slider"></span>
					</label>
				</div>

				<?php
				$bcb_ele_cls        = '\BaseCloud\Boost\Optimizer\Elementor_Optimizer';
				$bcb_ele_master_on  = ! empty( $options['elementor_speed'] );
				$bcb_ele_advisor_on = ! isset( $options['elementor_settings_advisor'] ) || ! empty( $options['elementor_settings_advisor'] );
				if ( $bcb_ele_master_on && $bcb_ele_advisor_on && class_exists( $bcb_ele_cls ) ) :
					$bcb_ele_items = $bcb_ele_cls::advisor_items();
					if ( ! empty( $bcb_ele_items ) ) :
						?>
						<div class="bcboost-field">
							<span class="bcboost-field-label"><?php esc_html_e( 'Advisor — this site\'s Elementor configuration', 'basecloud-boost' ); ?></span>
							<?php foreach ( $bcb_ele_items as $bcb_ele_item ) : ?>
								<?php if ( 'warn' === $bcb_ele_item['level'] ) : ?>
									<span class="bcboost-field-warning">⚠️ <?php echo esc_html( $bcb_ele_item['text'] ); ?></span>
								<?php elseif ( 'ok' === $bcb_ele_item['level'] ) : ?>
									<span class="bcboost-field-desc" style="display:block;color:var(--bcboost-green-ink);">✔ <?php echo esc_html( $bcb_ele_item['text'] ); ?></span>
								<?php else : ?>
									<span class="bcboost-field-desc" style="display:block;"><?php echo esc_html( $bcb_ele_item['text'] ); ?></span>
								<?php endif; ?>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>
				<?php elseif ( ! $bcb_ele_master_on ) : ?>
					<div class="bcboost-field">
						<span class="bcboost-field-desc"><?php esc_html_e( 'Enable Elementor Speed and save to see the Settings Advisor\'s recommendations for this site.', 'basecloud-boost' ); ?></span>
					</div>
				<?php endif; ?>

			<?php endif; ?>
		</div>

		<div class="bcboost-form-footer">
			<button type="submit" class="bcboost-btn bcboost-btn--primary">
				<span class="dashicons dashicons-saved"></span>
				<?php esc_html_e( 'Save Settings', 'basecloud-boost' ); ?>
			</button>
		</div>
	</form>
</div>
