<?php
/**
 * Adds a "Capsquery Facial Authentication" section to the user's profile edit screen.
 */

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

class Capsq_Profile {

	public function __construct() {
		add_action( 'show_user_profile', array( $this, 'render_section' ) );
		add_action( 'edit_user_profile', array( $this, 'render_section' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	/**
	 * Only load enrollment assets on the profile page for users.
	 */
	public function enqueue_assets( $hook ) {
		if ( 'profile.php' !== $hook && 'user-edit.php' !== $hook ) {
			return;
		}

		$user_id = $this->get_profile_user_id( $hook );
		

		if ( (int) get_current_user_id() !== (int) $user_id ) {
			return;
		}

		wp_enqueue_script(
			'face-api',
			CAPSQ_PLUGIN_URL . 'assets/vendor/face-api/face-api.min.js',
			array(),
			capsq_asset_version( 'assets/vendor/face-api/face-api.min.js' ),
			true
		);

		wp_enqueue_style(
			'capsq-style',
			CAPSQ_PLUGIN_URL . 'assets/css/capsq-style.css',
			array(),
			capsq_asset_version( 'assets/css/capsq-style.css' )
		);

		wp_enqueue_script(
			'capsq-enroll',
			CAPSQ_PLUGIN_URL . 'assets/js/capsq-enroll.js',
			array( 'face-api', 'jquery' ),
			capsq_asset_version( 'assets/js/capsq-enroll.js' ),
			true
		);

		wp_localize_script(
			'capsq-enroll',
			'CAPSQ_DATA',
			array(
				'restUrl'   => esc_url_raw( rest_url( 'capsquery-facial-authentication/v1' ) ),
				'nonce'     => wp_create_nonce( 'wp_rest' ),
				'modelsUrl'  => esc_url_raw( rest_url( 'capsquery-facial-authentication/v1/models' ) ),
				'hasFace'   => (bool) get_user_meta( $user_id, '_capsq_face_descriptor', true ),
				'hasPin'    => (bool) get_user_meta( $user_id, '_capsq_pin_hash', true ),
			)
		);
	}

	public function render_section( $user ) {
		$profile_user_id = (int) $user->ID;
		$is_own_profile  = (int) get_current_user_id() === $profile_user_id;

		?>
		<h2><?php esc_html_e( 'Capsquery Facial Authentication', 'capsquery-facial-authentication' ); ?></h2>
		<?php

		if ( ! $is_own_profile ) {
			?>
			<p class="description">
				<?php esc_html_e( 'Each user must enroll their own face from their profile page.', 'capsquery-facial-authentication' ); ?>
			</p>
			<?php
			return;
		}

		if ( ! Capsq_Helpers::is_enabled() ) {
			?>
			<div class="notice notice-warning inline"><p><?php esc_html_e( 'Capsquery Facial Authentication is currently disabled by the site administrator.', 'capsquery-facial-authentication' ); ?></p></div>
			<?php
			return;
		}

		$has_face = (bool) get_user_meta( $profile_user_id, '_capsq_face_descriptor', true );
		$has_pin_hash = (bool) get_user_meta( $profile_user_id, '_capsq_pin_hash', true );
		?>
		<table class="form-table" role="presentation">
			<tr>
				<th><label><?php esc_html_e( 'Enrolled face', 'capsquery-facial-authentication' ); ?></label></th>
				<td>
					<div id="capsq-enroll-wrap">
						<p id="capsq-status">
							<?php
								if ( $has_face ) {
									echo '<span style="color:green;">&#10003; ' . esc_html__( 'Face enrolled', 'capsquery-facial-authentication' ) . '</span>';
								} else {
									echo '<span style="color:#a00;">' . esc_html__( 'No face enrolled yet', 'capsquery-facial-authentication' ) . '</span>';
								}
							?>
						</p>

						<div id="capsq-camera-box" style="display:none; max-width:480px;">
							<video id="capsq-video" width="480" height="360" autoplay muted playsinline></video>
							<canvas id="capsq-overlay" width="480" height="360" style="position:absolute;"></canvas>
						</div>

						<p>
							<button type="button" class="button" id="capsq-start-camera"><?php esc_html_e( 'Open Camera', 'capsquery-facial-authentication' ); ?></button>
							<button type="button" class="button button-primary" id="capsq-capture-face" style="display:none;"><?php esc_html_e( 'Capture Face', 'capsquery-facial-authentication' ); ?></button>
							<button type="button" class="button" id="capsq-remove-face" style="<?php echo $has_face ? '' : 'display:none;'; ?>"><?php esc_html_e( 'Remove Enrolled Face', 'capsquery-facial-authentication' ); ?></button>
						</p>
						<p class="description"><?php esc_html_e( 'Look straight at the camera in good lighting, then click Capture Face. This stores an encrypted mathematical representation of your face — not a photo.', 'capsquery-facial-authentication' ); ?></p>
					</div>
				</td>
			</tr>
			<tr>
				<th><label for="capsq-pin"><?php esc_html_e( 'Login PIN', 'capsquery-facial-authentication' ); ?></label></th>
				<td>
					<p id="capsq-pin-status">
						<?php
							if ( $has_pin_hash ) {
								echo '<span style="color:green;">&#10003; ' . esc_html__( 'Pin Set', 'capsquery-facial-authentication' ) . '</span>';
							} else {
								echo '<span style="color:#a00;">' . esc_html__( 'No pin set yet', 'capsquery-facial-authentication' ) . '</span>';
							}
						?>
					</p>
					<input type="password" id="capsq-pin" inputmode="numeric" pattern="[0-9]*" maxlength="8" class="regular-text" placeholder="<?php esc_attr_e( '4-8 digit PIN', 'capsquery-facial-authentication' ); ?>" autocomplete="new-password" />
					<input type="password" id="capsq-pin-confirm" inputmode="numeric" pattern="[0-9]*" maxlength="8" class="regular-text" placeholder="<?php esc_attr_e( 'Confirm PIN', 'capsquery-facial-authentication' ); ?>" autocomplete="new-password" />
					<p>
						<button type="button" class="button button-primary" id="capsq-save-pin"><?php esc_html_e( 'Save PIN', 'capsquery-facial-authentication' ); ?></button>
					</p>
					<p class="description"><?php esc_html_e( 'Required as a second step after a successful face match (a face match alone never logs you in).', 'capsquery-facial-authentication' ); ?></p>
				</td>
			</tr>
		</table>
		<?php
	}

	private function get_profile_user_id( $hook ) {
		if ( 'user-edit.php' === $hook && isset( $_GET['user_id'] ) ) {
			return absint( $_GET['user_id'] );
		}
		return get_current_user_id();
	}
}
