<?php
/**
 * Handles WP_User related Activities
 *
 * @link       https://personalizewp.com/
 * @since      3.2.0 Merged into PWP
 * @since      1.0.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Tracking
 */

namespace PersonalizeWP\Tracking;

use PersonalizeWP\Tracking\Base;
use PersonalizeWP\Model\Contact;
use PersonalizeWP\Model\Activity;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
 * Defines comments functionality
 *
 * This class defines all code to add commenters to PWP contacts
 */
class User extends Base {

	/**
	 * Execute hooks
	 */
	public function setup() {
		add_action( 'login_form', array( $this, 'add_hidden_input_field' ), 10, 0 );
		add_action( 'woocommerce_login_form_end', array( $this, 'add_hidden_input_field' ), 10, 0 );
		add_action( 'wp_login', array( $this, 'add_user_to_pwp_on_login' ), 10, 2 );
		// Immediately add the JS for the input, as the normal FE action 'wp_footer' doesn't exist in this context.
		add_action( 'login_footer', array( $this, 'add_input_field_js' ) );

		// Only track profile updates if recording personal information.
		if ( ! $this->anonymise_profiles ) {
			add_action( 'show_user_profile', array( $this, 'add_hidden_input_field' ), 10, 0 );
			// Immediately add the JS for the input, as the normal FE action 'wp_footer' doesn't exist in this context.
			add_action( 'show_user_profile', array( $this, 'add_input_field_js' ) );
			add_action( 'profile_update', array( $this, 'update_user' ), 10, 3 );
		}
	}

	/**
	 * Update Contact when a user logs in.
	 *
	 * @param string   $user_login Username.
	 * @param \WP_User $user       WP_User object of the logged-in user.
	 *
	 * @return void
	 */
	public function add_user_to_pwp_on_login( $user_login, $user ) {

		$uid = $this->get_uid();
		// Sanity check.
		if ( empty( $uid ) ) {
			return;
		}

		// We know exactly who the user is, so we'll create it as such.
		$user_data = array(
			'is_verified' => true, // This forces an update to the Visitor profile.
			'first_name'  => $user->first_name,
			'last_name'   => $user->last_name,
			'email'       => $user->user_email,
			'meta_input'  => array(
				'wp_user_id'   => $user->ID,
				'wp_user_role' => $user->roles,
			),
		);

		// Get the Contact record, which might be an Anon user
		$contact = Contact::find_by_uid( $uid, $user_data );
		// Safety check to ensure existence.
		if ( ! $contact ) {
			return;
		}

		// add activity record for login
		Activity::record(
			$contact->uid,
			array(
				'activity_type' => 'login',
			)
		);
	}

	/**
	 * Update the Contact record if the WP user profile is updated
	 *
	 * @param int      $user_id       User ID.
	 * @param \WP_User $old_user_data Object containing user's data prior to update.
	 * @param array    $userdata      The raw array of data passed to wp_insert_user().
	 *
	 * @return void
	 */
	public function update_user( $user_id, $old_user_data, $userdata ) {

		$uid = $this->get_uid();
		// Sanity check.
		if ( empty( $uid ) ) {
			return;
		}

		// Check for ourselves
		if ( get_current_user_id() !== $user_id ) {
			return;
		}

		$contact_arr = array(
			// We know exactly who the user is, so mark it as such, if not already
			'is_verified' => true, // This forces an update to the Visitor profile.
			'first_name'  => empty( $userdata['first_name'] ) ? '' : sanitize_text_field( $userdata['first_name'] ),
			'last_name'   => empty( $userdata['last_name'] ) ? '' : sanitize_text_field( $userdata['last_name'] ),
			'email'       => sanitize_email( $userdata['user_email'] ),
			'meta_input'  => array(
				'wp_user_id'   => $old_user_data->ID,
				'wp_user_role' => $old_user_data->roles,
			),
		);

		// Finding will immediately update the contact as we're passing data.
		$contact = Contact::find_by_uid( $uid, $contact_arr );
		// Safety check to ensure existence.
		if ( ! $contact ) {
			return;
		}
		// add an activity log?
	}
}
