<?php
/**
 * User helper functions.
 *
 * @package Awesomemotive\Rewardswp\Helpers
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Helpers;

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

use WP_User;

/**
 * Get user IP address.
 *
 * @since 1.0.0
 *
 * @return string The user IP address.
 */
function get_user_ip(): string {
	return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0' ) );
}

/**
 * Retrieve the first user's name.
 *
 * @since 1.0.0
 *
 * @param int $user_id The user ID.
 *
 * @return string|null
 */
function get_user_first_name( int $user_id ): ?string {
	$user = get_user( $user_id );

	if ( ! is_a( $user, WP_User::class ) ) {
		return null;
	}

	if ( ! empty( $user->first_name ) ) {
		return $user->first_name;
	}

	// Fallback in case the first_name is not available.
	$name_parts = parse_name( $user->display_name );

	if ( is_email( $name_parts['first_name'] ) || empty( $name_parts['first_name'] ) ) {
		return null; // Avoid weirdness by returning emails instead of a real name.
	}

	return $name_parts['first_name'];
}

/**
 * Retrieve the last user's name (surname).
 *
 * @since 1.0.0
 *
 * @param int $user_id The user ID.
 *
 * @return string|null
 */
function get_user_last_name( int $user_id ): ?string {
	$user = get_user( $user_id );

	if ( ! is_a( $user, WP_User::class ) ) {
		return null;
	}

	if ( ! empty( $user->last_name ) ) {
		return $user->last_name;
	}

	// Fallback in case the first_name is not available.
	$name_parts = parse_name( $user->display_name );

	if ( is_email( $name_parts['last_name'] ) || empty( $name_parts['last_name'] ) ) {
		return null; // Avoid weirdness by returning emails instead of a real name.
	}

	return $name_parts['last_name'];
}

/**
 * Retrieve the user's full name.
 *
 * @param int|null $user_id Optional user ID. Default 0 (current user)
 *
 * @return string
 */
function get_user_full_name( int $user_id ): string {
	return sprintf(
		'%s %s',
		get_user_first_name( $user_id ),
		get_user_last_name( $user_id )
	);
}

/**
 * Retrieve the user's name parts by given user's ID.
 *
 * @param int|null $user_id Optional user ID. Default 0 (current user)
 *
 * @return array
 */
function get_user_name_parts( int $user_id ): array {
	return [
		'first_name' => get_user_first_name( $user_id ),
		'last_name'  => get_user_last_name( $user_id ),
	];
}

/**
 * Log in a user programmatically.
 *
 * @since 1.1.0
 *
 * @param int  $user_id The user ID to log in.
 * @param bool $remember Whether to remember the user login. Default true.
 *
 * @return bool True on success, false on failure.
 */
function login_user_programmatically( int $user_id, bool $remember = true ): bool {
	$user = get_user_by( 'id', $user_id );

	if ( ! $user instanceof WP_User ) {
		return false;
	}

	// Set the current user
	wp_set_current_user( $user_id );

	// Sync $_COOKIE so wp_get_session_token() works in the same request.
	// wp_set_auth_cookie() only sends Set-Cookie headers; $_COOKIE is not updated.
	add_action( 'set_logged_in_cookie', function ( $logged_in_cookie ) {
		$_COOKIE[ LOGGED_IN_COOKIE ] = $logged_in_cookie;
	} );

	// Set the authentication cookie
	wp_set_auth_cookie( $user_id, $remember );

	return true;
}
