<?php
/**
 * Admin View: File Integrity Monitor
 * @package GuardianShield
 */
if ( ! defined( 'ABSPATH' ) ) exit;
?>
<div class="wrap gs-wrap">
	<div class="gs-header">
		<div class="gs-header__logo">
			<span class="dashicons dashicons-media-code"></span>
			<h1><?php esc_html_e( 'File Integrity Monitor', 'guardian-shield' ); ?></h1>
		</div>
		<div class="gs-header__actions">
			<button class="gs-btn gs-btn--secondary" id="gs-build-baseline">
				📸 <?php esc_html_e( 'Rebuild Baseline', 'guardian-shield' ); ?>
			</button>
		</div>
	</div>

	<div class="gs-card gs-card--info">
		<div class="gs-card__body">
			<p>
				⏱ <?php
				/* translators: %s = date of last check */
				printf( esc_html__( 'Last integrity check: %s', 'guardian-shield' ), '<strong>' . esc_html( $last_check ) . '</strong>' );
				?>
			</p>
		</div>
	</div>

	<div class="gs-card">
		<div class="gs-card__header">
			<h2><?php esc_html_e( 'Modified / New / Deleted Files', 'guardian-shield' ); ?></h2>
		</div>
		<div class="gs-card__body">
			<table class="gs-table gs-table--striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'File', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Type', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Status', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Detected', 'guardian-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( empty( $changes ) ) : ?>
						<tr>
							<td colspan="4" class="gs-empty">
								✅ <?php esc_html_e( 'No unauthorized file changes detected.', 'guardian-shield' ); ?>
							</td>
						</tr>
					<?php else : foreach ( $changes as $c ) : ?>
					<tr class="<?php echo $c['status'] === 'modified' ? 'gs-row--warning' : ( $c['status'] === 'deleted' ? 'gs-row--danger' : '' ); ?>">
						<td>
							<code><?php echo esc_html( basename( $c['file_path'] ) ); ?></code>
							<small class="gs-path"><?php echo esc_html( dirname( $c['file_path'] ) ); ?></small>
						</td>
						<td><span class="gs-type-badge"><?php echo esc_html( $c['file_type'] ); ?></span></td>
						<td>
							<?php $cls = [ 'modified' => 'yellow', 'new' => 'blue', 'deleted' => 'red' ][ $c['status'] ] ?? ''; ?>
							<span class="gs-badge gs-badge--<?php echo esc_attr( $cls ); ?>">
								<?php echo esc_html( ucfirst( $c['status'] ) ); ?>
							</span>
						</td>
						<td class="gs-td--time"><?php echo esc_html( gs_human_time_diff( $c['last_checked'] ) ); ?></td>
					</tr>
					<?php endforeach; endif; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>

<script>
(function($){
	$('#gs-build-baseline').on('click', function(){
		if (!confirm(gs_admin.strings.confirm_baseline)) return;
		const $btn = $(this).prop('disabled', true).text('⏳ Building…');
		$.post(gs_admin.ajax_url, {
			action: 'gs_build_baseline',
			nonce: gs_admin.nonce
		}, function(res){
			$btn.prop('disabled', false).text('📸 <?php echo esc_js( __( 'Rebuild Baseline', 'guardian-shield' ) ); ?>');
			if (res.success) {
				alert('✅ ' + res.data.message);
				location.reload();
			}
		});
	});
})(jQuery);
</script>
