<?php
/**
 * Gestione semplificata: numero di telefono + interruttore 2FA per utente.
 *
 * @package SPIDy_WP
 */

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

class SPIDy_WP_User_Store {

	const META_PHONE   = '_spidy_wp_phone';
	const META_ENABLED = '_spidy_wp_enabled';
	const META_METHOD  = '_spidy_wp_method';

	const METHOD_EMAIL     = 'email';
	const METHOD_SMS       = 'sms';
	const METHOD_BIOMETRIC = 'biometric';

	public static function get_method( int $user_id ): string {
		$method = get_user_meta( $user_id, self::META_METHOD, true );
		$valid  = array( self::METHOD_EMAIL, self::METHOD_SMS, self::METHOD_BIOMETRIC );
		return in_array( $method, $valid, true ) ? $method : self::METHOD_EMAIL;
	}

	public static function set_method( int $user_id, string $method ): bool {
		$valid = array( self::METHOD_EMAIL, self::METHOD_SMS, self::METHOD_BIOMETRIC );
		if ( ! in_array( $method, $valid, true ) ) {
			$method = self::METHOD_EMAIL;
		}
		return (bool) update_user_meta( $user_id, self::META_METHOD, $method );
	}

	public static function get_phone( int $user_id ): string {
		$phone = get_user_meta( $user_id, self::META_PHONE, true );
		return is_string( $phone ) ? $phone : '';
	}

	public static function set_phone( int $user_id, string $phone ): bool {
		$phone = preg_replace( '/[^0-9+ ]/', '', $phone );
		return (bool) update_user_meta( $user_id, self::META_PHONE, $phone );
	}

	public static function is_2fa_enabled( int $user_id ): bool {
		return '1' === get_user_meta( $user_id, self::META_ENABLED, true );
	}

	public static function set_2fa_enabled( int $user_id, bool $enabled ): bool {
		return (bool) update_user_meta( $user_id, self::META_ENABLED, $enabled ? '1' : '0' );
	}
}
