<?php

namespace GemBoards\Ajax;

use GemBoards\Classes\AbstractAjaxHandler;

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

class Auth extends AbstractAjaxHandler {

	public function __construct() {
		$this->actions = [
			'user_registration' => [
				'callback'             => [ $this, 'user_registration' ],
				'allow_visitor_action' => true,
				'fields'               => [
					'first_name' => 'string',
					'last_name'  => 'string',
					'email'      => 'email',
					'password'   => 'string',
				],
			],
			'user_login'        => [
				'callback'             => [ $this, 'user_login' ],
				'allow_visitor_action' => true,
				'fields'               => [
					'email'    => 'email',
					'password' => 'string',
					'remember' => 'boolean',
				],
			],
			'user_logout'       => [
				'callback' => [ $this, 'user_logout' ],
				'fields'   => [],
			],
		];
	}

	public function user_registration( array $payload ) {
		if ( ! isset( $payload['first_name'] ) ) {
			wp_send_json_error( [
				'message' => __( 'First name is required.', 'gemboards' ),
			] );
		}
		if ( ! isset( $payload['last_name'] ) ) {
			wp_send_json_error( [
				'message' => __( 'Last name is required.', 'gemboards' ),
			] );
		}
		if ( ! isset( $payload['email'] ) ) {
			wp_send_json_error( [
				'message' => __( 'Email is required.', 'gemboards' ),
			] );
		}
		if ( ! isset( $payload['password'] ) ) {
			wp_send_json_error( [
				'message' => __( 'Password is required.', 'gemboards' ),
			] );
		}

		$email = sanitize_email( $payload['email'] );
		if ( ! is_email( $email ) ) {
			wp_send_json_error( [
				'message' => __( 'Invalid email address.', 'gemboards' ),
			] );
		}

		if ( email_exists( $email ) ) {
			wp_send_json_error( [
				'message' => __( 'A user with this email already exists.', 'gemboards' ),
			] );
		}

		$username = sanitize_user( strstr( $email, '@', true ) );
		if ( username_exists( $username ) ) {
			$username .= '_' . wp_generate_password( 4, false );
		}

		$user_data = [
			'user_login' => $username,
			'user_pass'  => $payload['password'],
			'user_email' => $email,
			'first_name' => sanitize_text_field( $payload['first_name'] ),
			'last_name'  => sanitize_text_field( $payload['last_name'] ),
			'role'       => 'subscriber',
		];
		$user_id   = wp_insert_user( $user_data );
		if ( is_wp_error( $user_id ) ) {
			wp_send_json_error( [
				'message' => $user_id->get_error_message(),
			] );
		}

		wp_set_current_user( $user_id );
		wp_set_auth_cookie( $user_id );

		wp_send_json_success( [
			'message' => __( 'Registration successful.', 'gemboards' ),
			'user_id' => $user_id,
		] );
	}

	public function user_login( array $payload ) {
		if ( ! isset( $payload['email'] ) ) {
			wp_send_json_error( [
				'message' => __( 'Email is required.', 'gemboards' ),
			] );
		}
		if ( ! isset( $payload['password'] ) ) {
			wp_send_json_error( [
				'message' => __( 'Password is required.', 'gemboards' ),
			] );
		}

		$email = sanitize_email( $payload['email'] );
		if ( ! is_email( $email ) ) {
			wp_send_json_error( [
				'message' => __( 'Invalid email address.', 'gemboards' ),
			] );
		}

		$user = get_user_by( 'email', $email );
		if ( ! $user ) {
			wp_send_json_error( [
				'message' => __( 'No account found with this email.', 'gemboards' ),
			] );
		}

		$credentials = array(
			'user_login'    => $user->user_login,
			'user_password' => $payload['password'],
			'remember'      => isset( $payload['remember'] ),
		);

		$user = wp_signon( $credentials, is_ssl() );
		if ( is_wp_error( $user ) ) {
			wp_send_json_error( [
				'message' => __( 'Invalid credentials provided.', 'gemboards' ),
			] );
		}

		wp_set_current_user( $user->ID );

		wp_send_json_success([
			'user_id' => $user->ID,
		]);
	}

	public function user_logout() {
		wp_logout();
		wp_send_json_success();
	}
}
