<?php
/**
 * BaseCloud Boost — Webhook Settings View
 *
 * @package BaseCloud\Boost
 */

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

use BaseCloud\Boost\Core\Metrics_Collector;
use BaseCloud\Boost\Core\Webhook_Sender;

$options = (array) get_option( BCBOOST_OPTIONS_KEY, [] );
$metrics = new Metrics_Collector();
$stats   = $metrics->get_all();
$sender  = new Webhook_Sender();
$payload = $sender->build_payload();

$last_sent = $stats['last_webhook_sent']
	? human_time_diff( (int) $stats['last_webhook_sent'] ) . ' ' . __( 'ago', 'basecloud-boost' )
	: __( 'Never', 'basecloud-boost' );

$webhook_errors = (int) ( $stats['webhook_errors'] ?? 0 );
?>
<div class="bcboost-wrap">

	<?php require __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="webhook_enabled,webhook_on_purge">
		<?php
		$schedule_labels = [
			'hourly'     => __( 'Hourly', 'basecloud-boost' ),
			'twicedaily' => __( 'Twice Daily', 'basecloud-boost' ),
			'daily'      => __( 'Daily', 'basecloud-boost' ),
			'weekly'     => __( 'Weekly', 'basecloud-boost' ),
		];
		$current_schedule = $options['webhook_schedule'] ?? 'daily';
		?>

		<!-- Webhook Configuration -->
		<div class="bcboost-card bcboost-card--accent-purple">
			<h3><span class="bcboost-section-icon">🔗</span> <?php esc_html_e( 'Webhook Configuration', 'basecloud-boost' ); ?></h3>
			<p class="bcboost-card-desc"><?php esc_html_e( 'Send performance metrics to any webhook endpoint — integrate with Slack, Zapier, Make, or your own dashboard.', 'basecloud-boost' ); ?></p>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Enable Webhook', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Send a daily metrics payload to your configured webhook URL.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="webhook_enabled" value="1" <?php checked( ! empty( $options['webhook_enabled'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="webhook_url"><?php esc_html_e( 'Webhook URL', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'The HTTPS endpoint that will receive POST requests with metrics JSON.', 'basecloud-boost' ); ?></span>
				<input
					type="url"
					id="webhook_url"
					name="webhook_url"
					class="bcboost-input"
					placeholder="https://hooks.example.com/your-endpoint"
					value="<?php echo esc_attr( $options['webhook_url'] ?? '' ); ?>"
					autocomplete="off"
				>
			</div>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="webhook_secret"><?php esc_html_e( 'Signing Secret (optional)', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'A shared secret for HMAC-SHA256 payload signing. Sent as X-BCBoost-Signature header.', 'basecloud-boost' ); ?></span>
				<input
					type="password"
					id="webhook_secret"
					name="webhook_secret"
					class="bcboost-input"
					placeholder="<?php echo \BaseCloud\Boost\Core\Crypto::has_secret( (string) ( $options['webhook_secret'] ?? '' ) ) ? esc_attr__( 'Saved &amp; encrypted — leave blank to keep', 'basecloud-boost' ) : esc_attr__( 'Leave blank to skip signing', 'basecloud-boost' ); ?>"
					value=""
					autocomplete="new-password"
				>
			</div>

			<div class="bcboost-field bcboost-field--toggle">
				<label>
					<span class="bcboost-field-label"><?php esc_html_e( 'Fire on Cache Purge', 'basecloud-boost' ); ?></span>
					<span class="bcboost-field-desc"><?php esc_html_e( 'Also send metrics immediately when the full cache is purged.', 'basecloud-boost' ); ?></span>
				</label>
				<label class="bcboost-toggle">
					<input type="checkbox" name="webhook_on_purge" value="1" <?php checked( ! empty( $options['webhook_on_purge'] ) ); ?>>
					<span class="bcboost-toggle-slider"></span>
				</label>
			</div>
		</div>

		<!-- Delivery Schedule -->
		<div class="bcboost-card">
			<h3><span class="bcboost-section-icon">🕐</span> <?php esc_html_e( 'Delivery Schedule', 'basecloud-boost' ); ?></h3>
			<p class="bcboost-card-desc"><?php esc_html_e( 'How often WP-Cron should automatically send metrics to your webhook.', 'basecloud-boost' ); ?></p>

			<div class="bcboost-field">
				<label class="bcboost-field-label" for="webhook_schedule"><?php esc_html_e( 'Send Frequency', 'basecloud-boost' ); ?></label>
				<span class="bcboost-field-desc"><?php esc_html_e( 'Changes take effect immediately after saving.', 'basecloud-boost' ); ?></span>
				<select id="webhook_schedule" name="webhook_schedule" class="bcboost-input" style="max-width:240px;">
					<?php foreach ( $schedule_labels as $val => $label ) : ?>
						<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $current_schedule, $val ); ?>><?php echo esc_html( $label ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>
		</div>

		<!-- Webhook Status & Actions -->
		<div class="bcboost-card">
			<h3><span class="bcboost-section-icon">📡</span> <?php esc_html_e( 'Webhook Status', 'basecloud-boost' ); ?></h3>

			<div class="bcboost-webhook-status-row">
				<div class="bcboost-webhook-stat">
					<span class="bcboost-webhook-stat-label"><?php esc_html_e( 'Last Sent', 'basecloud-boost' ); ?></span>
					<span class="bcboost-webhook-stat-value"><?php echo esc_html( $last_sent ); ?></span>
				</div>
				<div class="bcboost-webhook-stat">
					<span class="bcboost-webhook-stat-label"><?php esc_html_e( 'Delivery Errors', 'basecloud-boost' ); ?></span>
					<span class="bcboost-webhook-stat-value <?php echo $webhook_errors > 0 ? 'bcboost-text-danger' : ''; ?>"><?php echo esc_html( $webhook_errors ); ?></span>
				</div>
				<div class="bcboost-webhook-stat">
					<span class="bcboost-webhook-stat-label"><?php esc_html_e( 'Schedule', 'basecloud-boost' ); ?></span>
					<span class="bcboost-webhook-stat-value"><?php echo esc_html( $schedule_labels[ $current_schedule ] ?? ucfirst( $current_schedule ) ); ?></span>
				</div>
			</div>

			<div class="bcboost-webhook-actions">
				<button type="button" class="bcboost-btn bcboost-btn--secondary" id="bcboost-webhook-test">
					<span class="dashicons dashicons-external"></span>
					<?php esc_html_e( 'Test Webhook', 'basecloud-boost' ); ?>
				</button>
				<button type="button" class="bcboost-btn bcboost-btn--secondary" id="bcboost-send-metrics-now">
					<span class="dashicons dashicons-cloud-upload"></span>
					<?php esc_html_e( 'Send Now', 'basecloud-boost' ); ?>
				</button>
			</div>
			<div id="bcboost-webhook-feedback" class="bcboost-feedback" style="display:none;margin-top:14px;"></div>
		</div>

		<!-- Payload Preview -->
		<div class="bcboost-card">
			<h3><span class="bcboost-section-icon">📋</span> <?php esc_html_e( 'Payload Preview', 'basecloud-boost' ); ?></h3>
			<p class="bcboost-card-desc"><?php esc_html_e( 'This is the JSON payload that will be POST-ed to your webhook.', 'basecloud-boost' ); ?></p>
			<pre class="bcboost-payload-preview"><code><?php echo esc_html( wp_json_encode( $payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ); ?></code></pre>
		</div>

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

	</form>
</div>
