<?php
/**
 * Shared helpers:  settings, messaging.
 */

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

class Capsq_Helpers {

	const OPTION_ENABLED    = 'capsq_enabled';
	const OPTION_THRESHOLD  = 'capsq_match_threshold';

	/**
	 * Whether Capsquery Facial Authentication is enabled site-wide.
	 */
	public static function is_enabled() {
		return (bool) get_option( self::OPTION_ENABLED, true );
	}

	/**
	 * Match threshold (euclidean distance). Lower = stricter.
	 */
	public static function get_threshold() {
		$stored = get_option( self::OPTION_THRESHOLD, '' );
		if ( is_numeric( $stored ) ) {
			$value = (float) $stored;
			if ( $value >= 0.3 && $value <= 0.9 ) {
				return $value;
			}
		}
		return (float) CAPSQ_MATCH_THRESHOLD;
	}

	/**
	 * User IDs that are allowed to use Capsquery Facial Authentication .
	 *
	 * @return int[]
	 */
	public static function capsquery_get_user_ids() {
		$users = get_users(
			array(
				'fields' => 'ID',
			)
		);

		$all_user_ids = array();
		
		foreach ( $users as $user_id ) {			
			$all_user_ids[] = (int) $user_id;
		}

		return $all_user_ids;
	}


	/**
	 * REST permission: logged in, feature enabled.
	 */
	public static function rest_can_manage_own_face() {
		if ( ! is_user_logged_in() || ! self::is_enabled() ) {
			return false;
		}
		return  get_current_user_id();
	}

	/**
	 * REST permission: public match/verify when feature is enabled.
	 */
	public static function rest_can_attempt_login() {
		return self::is_enabled();
	}
}
