<?php
/**
 * BaseCloud Boost — Page Cache 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="cache_enabled,cache_mobile_separate,cache_logged_in,cache_query_strings,cache_vary_by_country">

		<div class="bcboost-card bcboost-card--accent-green">
			<h3><span class="bcboost-section-icon">📄</span> <?php esc_html_e( 'Page Cache Settings', 'basecloud-boost' ); ?></h3>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Enable Page Cache', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Serve pre-cached HTML files to visitors, bypassing WordPress and PHP for maximum speed.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="cache_enabled" value="1" <?php checked( ! empty( $options['cache_enabled'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="cache_lifetime"><?php esc_html_e( 'Cache Lifetime (seconds)', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'How long to keep cached pages. Default: 604800 (7 days) — Boost purges automatically whenever content changes, so expiry only matters for pages where nothing changed. Set to 0 for no expiry.', 'basecloud-boost' ); ?></span>
				<input type="number" id="cache_lifetime" name="cache_lifetime" class="bcboost-input" value="<?php echo (int) ( $options['cache_lifetime'] ?? 604800 ); ?>" min="0" max="2592000" style="margin-top:10px;">
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Separate Cache for Mobile', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Create separate cache files for mobile devices. Useful if your theme has a separate mobile layout.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="cache_mobile_separate" value="1" <?php checked( ! empty( $options['cache_mobile_separate'] ) ); ?>>
					<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( 'Cache for Logged-In Users', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc">
						<?php esc_html_e( 'Enable caching for logged-in users. Not recommended if users see personalized content.', 'basecloud-boost' ); ?>
						<span class="bcboost-field-warning">⚠️ <?php esc_html_e( 'May expose private content.', 'basecloud-boost' ); ?></span>
					</span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="cache_logged_in" value="1" <?php checked( ! empty( $options['cache_logged_in'] ) ); ?>>
					<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( 'Cache Pages with Query Strings', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Cache URLs that contain query parameters (e.g., ?ref=123). May increase cache size significantly.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="cache_query_strings" value="1" <?php checked( ! empty( $options['cache_query_strings'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>
		</div>

		<div class="bcboost-card bcboost-card--accent-green">
			<h3><span class="bcboost-section-icon">🌍</span> <?php esc_html_e( 'Geolocation &amp; Currency', 'basecloud-boost' ); ?></h3>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Separate Cache per Country', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc">
						<?php esc_html_e( 'Store a separate cached copy of each page per visitor country, so geolocation pricing / currency switching keeps working from cache instead of freezing one country’s prices for everyone.', 'basecloud-boost' ); ?>
						<br>
						<?php esc_html_e( 'Best with a country header (Cloudflare CF-IPCountry, AWS CloudFront, or an nginx/Apache GeoIP module). Without one, your theme can supply the country via the BCBOOST_resolve_country filter.', 'basecloud-boost' ); ?>
					</span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="cache_vary_by_country" value="1" <?php checked( ! empty( $options['cache_vary_by_country'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="vary_cookies"><?php esc_html_e( 'Cache Variants by Cookie', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'Cookie names that should each get their own cached variant (one per line). Common currency-switcher cookies are already handled automatically. Example: my_currency, my_region', 'basecloud-boost' ); ?></span>
				<textarea id="vary_cookies" name="vary_cookies" class="bcboost-textarea bcboost-input--wide" style="margin-top:10px;" placeholder="aelia_cs_selected_currency&#10;woocs_current_currency"><?php echo esc_textarea( $options['vary_cookies'] ?? '' ); ?></textarea>
			</div>
		</div>

		<div class="bcboost-card">
			<h3><span class="bcboost-section-icon">🚫</span> <?php esc_html_e( 'Cache Exclusions', 'basecloud-boost' ); ?></h3>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="exclude_urls"><?php esc_html_e( 'Exclude URLs', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'One URL pattern per line. Supports partial matches. Example: /checkout, /my-account, /cart', 'basecloud-boost' ); ?></span>
				<textarea id="exclude_urls" name="exclude_urls" class="bcboost-textarea bcboost-input--wide" style="margin-top:10px;"><?php echo esc_textarea( $options['exclude_urls'] ?? '' ); ?></textarea>
			</div>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="exclude_cookies"><?php esc_html_e( 'Exclude by Cookie', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'Cookie name patterns that bypass cache. One per line.', 'basecloud-boost' ); ?></span>
				<textarea id="exclude_cookies" name="exclude_cookies" class="bcboost-textarea bcboost-input--wide" style="margin-top:10px;"><?php echo esc_textarea( $options['exclude_cookies'] ?? '' ); ?></textarea>
			</div>
		</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>
