<?php
/**
 * File: class-saveregistration.php
 *
 *
 * @since 2024-07-30
 * @license GPL-3.0-or-later
 *
 * @package mareike/
 */

namespace Bdp\Modules\Registration\Controllers;

use WP_User;

class SaveRegistration {
	public static function execute($user_id) {
		if (!empty($_POST['first_name'])) {
			update_user_meta($user_id, 'first_name', sanitize_text_field($_POST['first_name']));
		}
		if (!empty($_POST['last_name'])) {
			update_user_meta($user_id, 'last_name', sanitize_text_field($_POST['last_name']));
		}

		// Assign the 'Standarduser' role to the new user
		$user = new WP_User($user_id);
		$user->set_role('user');

		// Send confirmation email
		$user = get_userdata($user_id);
		$code = sha1($user->user_registered);
		update_user_meta($user_id, 'activation_code', $code);

		$activation_link = add_query_arg(array('key' => $code, 'user' => $user_id), get_site_url() . '/wp-login.php');

		wp_mail($user->user_email, 'Bitte bestätige deine Anmeldung', 'Hallo, bitte bestätige deine Anmeldung über den folgenden Link: ' . $activation_link);

		// Notify admin
		wp_mail(get_option('admin_email'), 'New User Registration', 'A new user has registered: ' . PHP_EOL .
		                                                            'Username: ' . $user->user_login . PHP_EOL .
		                                                            'First name: ' . $user->first_name . PHP_EOL .
		                                                            'Last name: ' . $user->last_name . PHP_EOL .
																	'E-Mail: ' . $user->user_email);
	}

	public static function activate_user() {
		if (isset($_GET['key']) && isset($_GET['user'])) {
			$user_id = intval($_GET['user']);
			$activation_code = get_user_meta($user_id, 'activation_code', true);

			if ($activation_code === $_GET['key']) {
				delete_user_meta($user_id, 'activation_code');
				wp_redirect(home_url('/wp-login.php?checkemail=registered'));
				exit;
			}
		}
	}

	public static function check_user_activation($user, $username, $password) {
		if (!is_a($user, 'WP_User')) {
			return null;
		}
		$user_id = $user->ID;
		$activation_code = get_user_meta($user_id, 'activation_code', true);

		if ($activation_code) {
			return new WP_Error('not_activated', __('ERROR: You need to activate your account. Please check your email.', 'kompass'));
		}

		return $user;
	}

	public static function display_custom_message() {
		if (isset($_GET['checkemail']) && $_GET['checkemail'] === 'registered') {
			echo '<div class="custom-message" style="text-align:center; margin:20px auto; padding:10px; background-color:#e0f7fa; border:1px solid #00796b; border-radius:5px; max-width:600px;">
                <p style="font-size:16px; color:#00796b;">Vielen Dank für deine Registrierung. Bitte überprüfen deine E-Mails, um deine Registrierung zu bestätigen.</p>
              </div>';
		}

		if ((isset($_GET['action']) && $_GET['action'] === 'laostpassword') ||
		    isset($_GET['checkemail']) && $_GET['checkemail'] === 'confirm'
		){
			echo '<div class="custom-message" style="text-align:center; margin:20px auto; padding:10px; background-color:#e0f7fa; border:1px solid #00796b; border-radius:5px; max-width:600px;">
                <p style="font-size:16px; color:#00796b;">Insofern der Account existiert, hast du soeben weitere Anweisungen per E-Mail erhalten.</p>
              </div>';
		}

		if (isset($_GET['action']) && $_GET['action'] === 'resetpass') {
			echo '<div class="custom-message" style="text-align:center; margin:20px auto; padding:10px; background-color:#e0f7fa; border:1px solid #00796b; border-radius:5px; max-width:600px;">
                <p style="font-size:16px; color:#00796b;">Dein Passwort wurde erfolgreich geändert.</p>
              </div>';
		}
	}
}