<?php
/**
 * Admin View: Firewall & IP Management
 * @package GuardianShield
 */
if ( ! defined( 'ABSPATH' ) ) exit;
?>
<div class="wrap gs-wrap">
	<div class="gs-header">
		<div class="gs-header__logo">
			<span class="dashicons dashicons-shield-alt"></span>
			<h1><?php esc_html_e( 'Firewall & IP Rules', 'guardian-shield' ); ?></h1>
		</div>
	</div>

	<!-- Add IP Rule -->
	<div class="gs-card">
		<div class="gs-card__header"><h2>➕ <?php esc_html_e( 'Add IP Rule', 'guardian-shield' ); ?></h2></div>
		<div class="gs-card__body">
			<div class="gs-inline-form" id="gs-add-ip-form">
				<input type="text" id="gs-ip-input" placeholder="<?php esc_attr_e( 'IP Address (e.g. 1.2.3.4)', 'guardian-shield' ); ?>" class="regular-text">
				<input type="text" id="gs-ip-reason" placeholder="<?php esc_attr_e( 'Reason (optional)', 'guardian-shield' ); ?>" class="regular-text">
				<button class="gs-btn gs-btn--danger gs-btn--sm" data-action="blacklist">🚫 <?php esc_html_e( 'Blacklist', 'guardian-shield' ); ?></button>
				<button class="gs-btn gs-btn--success gs-btn--sm" data-action="whitelist">✅ <?php esc_html_e( 'Whitelist', 'guardian-shield' ); ?></button>
			</div>
			<div id="gs-ip-result" class="gs-notice" style="display:none"></div>
		</div>
	</div>

	<!-- Current IP Rules -->
	<div class="gs-card">
		<div class="gs-card__header"><h2>📋 <?php esc_html_e( 'Current IP Rules', 'guardian-shield' ); ?></h2></div>
		<div class="gs-card__body">
			<table class="gs-table">
				<thead>
					<tr>
						<th><?php esc_html_e( 'IP Address', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Type', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Reason', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Expires', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Added', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Actions', 'guardian-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( empty( $ip_rules ) ) : ?>
						<tr><td colspan="6" class="gs-empty"><?php esc_html_e( 'No IP rules configured.', 'guardian-shield' ); ?></td></tr>
					<?php else : foreach ( $ip_rules as $rule ) : ?>
					<tr>
						<td><code><?php echo esc_html( $rule['ip_address'] ); ?></code></td>
						<td>
							<span class="gs-badge gs-badge--<?php echo $rule['rule_type'] === 'whitelist' ? 'green' : 'red'; ?>">
								<?php echo esc_html( ucfirst( $rule['rule_type'] ) ); ?>
							</span>
						</td>
						<td><?php echo esc_html( $rule['reason'] ?: '—' ); ?></td>
						<td><?php echo $rule['expires_at'] ? esc_html( $rule['expires_at'] ) : '∞'; ?></td>
						<td><?php echo esc_html( gs_human_time_diff( $rule['created_at'] ) ); ?></td>
						<td>
							<button class="gs-btn gs-btn--outline gs-btn--sm gs-remove-ip" data-ip="<?php echo esc_attr( $rule['ip_address'] ); ?>">
								🗑 <?php esc_html_e( 'Remove', 'guardian-shield' ); ?>
							</button>
						</td>
					</tr>
					<?php endforeach; endif; ?>
				</tbody>
			</table>
		</div>
	</div>

	<!-- Recent Firewall Events -->
	<div class="gs-card">
		<div class="gs-card__header"><h2>📊 <?php esc_html_e( 'Recent Firewall Events', 'guardian-shield' ); ?></h2></div>
		<div class="gs-card__body">
			<table class="gs-table gs-table--striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Time', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'IP', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Rule', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Action', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'URI', 'guardian-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( empty( $fw_events ) ) : ?>
						<tr><td colspan="5" class="gs-empty"><?php esc_html_e( 'No firewall events yet.', 'guardian-shield' ); ?></td></tr>
					<?php else : foreach ( $fw_events as $ev ) : ?>
					<tr>
						<td class="gs-td--time"><?php echo esc_html( gs_human_time_diff( $ev['created_at'] ) ); ?></td>
						<td><code><?php echo esc_html( $ev['ip_address'] ); ?></code></td>
						<td><span class="gs-type-badge"><?php echo esc_html( $ev['rule_matched'] ); ?></span></td>
						<td><span class="gs-badge gs-badge--<?php echo $ev['action'] === 'block' ? 'red' : 'green'; ?>"><?php echo esc_html( $ev['action'] ); ?></span></td>
						<td class="gs-td--uri"><?php echo esc_html( wp_trim_words( $ev['request_uri'], 8, '…' ) ); ?></td>
					</tr>
					<?php endforeach; endif; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>

<script>
(function($){
	$('#gs-add-ip-form button').on('click', function(){
		const ip     = $('#gs-ip-input').val().trim();
		const reason = $('#gs-ip-reason').val().trim();
		const action = $(this).data('action');
		if (!ip) { alert('<?php echo esc_js( __( 'Please enter an IP address.', 'guardian-shield' ) ); ?>'); return; }

		$.post(gs_admin.ajax_url, {
			action: 'gs_manage_ip',
			nonce: gs_admin.nonce,
			ip_action: action,
			ip: ip,
			reason: reason
		}, function(res){
			$('#gs-ip-result')
				.removeClass('gs-notice--success gs-notice--error')
				.addClass(res.success ? 'gs-notice--success' : 'gs-notice--error')
				.text(res.success ? '✅ ' + res.data : '❌ ' + (res.data || 'Error'))
				.show();
			if (res.success) setTimeout(() => location.reload(), 1500);
		});
	});

	$('.gs-remove-ip').on('click', function(){
		const ip = $(this).data('ip');
		if (!confirm('Remove IP rule for ' + ip + '?')) return;
		$.post(gs_admin.ajax_url, {
			action: 'gs_manage_ip',
			nonce: gs_admin.nonce,
			ip_action: 'remove',
			ip: ip
		}, function(res){
			if (res.success) location.reload();
		});
	});
})(jQuery);
</script>
