<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title"><?php esc_html_e( 'Risk Scoring', 'za-creative-login-shield' ); ?></h1>
		</div>
		<div class="zacls-action-right">
			<button type="button" class="zacls-btn zacls-btn-primary" id="zacls-save-risk-settings"><?php esc_html_e( 'Save Settings', 'za-creative-login-shield' ); ?></button>
		</div>
	</div>
	<p class="zacls-page-description"><?php esc_html_e( 'Per-login risk scoring based on IP reputation, new device detection, geolocation changes, TOR/VPN detection, and failed attempt history.', 'za-creative-login-shield' ); ?></p>

	<?php
	global $wpdb;
	$recent_scores = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
		"SELECT rs.*, u.display_name
		FROM {$wpdb->prefix}zacls_risk_scores rs
		LEFT JOIN {$wpdb->users} u ON rs.user_id = u.ID
		ORDER BY rs.created_at DESC
		LIMIT 50",
		ARRAY_A
	); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	?>

	<div class="zacls-section-card">
		<div class="zacls-section-card-header">
			<span class="dashicons dashicons-admin-settings"></span>
			<h3><?php esc_html_e( 'Risk Score Settings', 'za-creative-login-shield' ); ?></h3>
		</div>
		<div class="zacls-section-card-body">
			<label><input type="checkbox" id="zacls-tor-detection" <?php checked( 'yes' === get_option( 'zacls_tor_detection', 'no' ) ); ?>> <?php esc_html_e( 'Enable TOR/VPN detection (checks login IPs against TOR exit node list)', 'za-creative-login-shield' ); ?></label>
		</div>
	</div>

	<div class="zacls-section-card">
		<div class="zacls-section-card-header">
			<span class="dashicons dashicons-info"></span>
			<h3><?php esc_html_e( 'How Risk Scoring Works', 'za-creative-login-shield' ); ?></h3>
		</div>
		<div class="zacls-section-card-body">
			<table class="widefat striped">
				<thead><tr><th><?php esc_html_e( 'Factor', 'za-creative-login-shield' ); ?></th><th><?php esc_html_e( 'Weight', 'za-creative-login-shield' ); ?></th><th><?php esc_html_e( 'Description', 'za-creative-login-shield' ); ?></th></tr></thead>
				<tbody>
					<tr><td><?php esc_html_e( 'IP Reputation', 'za-creative-login-shield' ); ?></td><td>10-40</td><td><?php esc_html_e( 'Blocked IPs get 40pts; high failed attempt rates from IP add 10-25pts', 'za-creative-login-shield' ); ?></td></tr>
					<tr><td><?php esc_html_e( 'New Device', 'za-creative-login-shield' ); ?></td><td>5-15</td><td><?php esc_html_e( 'New devices add 15pts; untrusted devices add 5pts', 'za-creative-login-shield' ); ?></td></tr>
					<tr><td><?php esc_html_e( 'Failed Attempts', 'za-creative-login-shield' ); ?></td><td>10-30</td><td><?php esc_html_e( '10+ failed attempts in an hour add 30pts; 3+ add 10pts', 'za-creative-login-shield' ); ?></td></tr>
					<tr><td><?php esc_html_e( 'TOR/VPN', 'za-creative-login-shield' ); ?></td><td>30</td><td><?php esc_html_e( 'TOR exit nodes add 30pts', 'za-creative-login-shield' ); ?></td></tr>
					<tr><td><?php esc_html_e( 'Geolocation Change', 'za-creative-login-shield' ); ?></td><td>20</td><td><?php esc_html_e( 'Login from a different country than previous adds 20pts', 'za-creative-login-shield' ); ?></td></tr>
				</tbody>
			</table>
			<p><strong><?php esc_html_e( 'Risk Levels:', 'za-creative-login-shield' ); ?></strong>
				<span class="zacls-status-badge status-success"><?php esc_html_e( 'Low (0-14)', 'za-creative-login-shield' ); ?></span>
				<span class="zacls-status-badge status-manual"><?php esc_html_e( 'Medium (15-39)', 'za-creative-login-shield' ); ?></span>
				<span class="zacls-status-badge status-failed"><?php esc_html_e( 'High (40-69)', 'za-creative-login-shield' ); ?></span>
				<span class="zacls-status-badge status-auto"><?php esc_html_e( 'Critical (70+)', 'za-creative-login-shield' ); ?></span>
			</p>
		</div>
	</div>

	<?php if ( ! empty( $recent_scores ) ) : ?>
		<div class="zacls-table-wrap">
			<table class="wp-list-table widefat fixed striped">
				<thead><tr>
					<th><?php esc_html_e( 'User', 'za-creative-login-shield' ); ?></th>
					<th><?php esc_html_e( 'IP', 'za-creative-login-shield' ); ?></th>
					<th><?php esc_html_e( 'Score', 'za-creative-login-shield' ); ?></th>
					<th><?php esc_html_e( 'Level', 'za-creative-login-shield' ); ?></th>
					<th><?php esc_html_e( 'Date', 'za-creative-login-shield' ); ?></th>
				</tr></thead>
				<tbody>
				<?php foreach ( $recent_scores as $s ) : ?>
					<tr>
						<td><?php echo esc_html( $s['display_name'] ?: '#' . $s['user_id'] ); ?></td>
						<td><code><?php echo esc_html( $s['ip'] ); ?></code></td>
						<td><strong><?php echo esc_html( $s['score'] ); ?></strong></td>
						<td><span class="zacls-status-badge <?php echo esc_attr( ZACLS_Risk_Scoring::get_level_class( $s['level'] ) ); ?>"><?php echo esc_html( $s['level'] ); ?></span></td>
						<td><?php echo esc_html( $s['created_at'] ); ?></td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	<?php endif; ?>
</div>

<script>
jQuery(function($) {
	$('#zacls-save-risk-settings').on('click', function() {
		$.post(zaclsData.ajaxUrl, {
			action: 'zacls_save_settings',
			nonce: zaclsData.nonce,
			settings: { zacls_tor_detection: $('#zacls-tor-detection').is(':checked') ? 'yes' : 'no' }
		}, function(response) {
			if (response.success) { alert(response.data.message); }
			else { alert(response.data.message || zaclsData.i18n.error); }
		});
	});
});
</script>
