<?php
/**
 * Front-end sign-in-by-phone form.
 *
 * @package TextTango
 */

declare( strict_types=1 );

namespace TextTango\Otp;

use TextTango\Support\Settings;

defined( 'ABSPATH' ) || exit;

/**
 * Renders the two-step phone form and loads its assets.
 *
 * Available as the `[texttango_phone_login]` shortcode, and optionally on
 * `wp-login.php` itself.
 */
final class LoginForm {

	/**
	 * Shortcode tag.
	 */
	public const SHORTCODE = 'texttango_phone_login';

	/**
	 * Script and style handle.
	 */
	private const HANDLE = 'texttango-frontend';

	/**
	 * Register hooks.
	 */
	public function register(): void {
		add_shortcode( self::SHORTCODE, array( $this, 'render_shortcode' ) );

		if ( self::shows_on_wp_login() ) {
			add_action( 'login_enqueue_scripts', array( $this, 'enqueue_assets' ) );
			add_action( 'login_form', array( $this, 'render_on_login_screen' ) );
		}
	}

	/**
	 * Whether the form is added to wp-login.php.
	 */
	public static function shows_on_wp_login(): bool {
		return LoginController::is_enabled() && (bool) Settings::get( 'otp_on_wp_login', true );
	}

	/**
	 * Render the shortcode.
	 *
	 * @param array<string, mixed>|string $atts Shortcode attributes.
	 * @return string
	 */
	public function render_shortcode( $atts = array() ): string {
		$atts = shortcode_atts(
			array(
				'redirect' => '',
				'title'    => '',
			),
			is_array( $atts ) ? $atts : array(),
			self::SHORTCODE
		);

		if ( ! LoginController::is_enabled() ) {
			return '';
		}

		if ( is_user_logged_in() ) {
			return '<p class="texttango-ui">' . esc_html__( 'You are already signed in.', 'texttango' ) . '</p>';
		}

		$this->enqueue_assets();

		ob_start();
		$this->render( (string) $atts['redirect'], (string) $atts['title'] );

		return (string) ob_get_clean();
	}

	/**
	 * Render the form beneath the standard login fields.
	 */
	public function render_on_login_screen(): void {
		if ( ! LoginController::is_enabled() ) {
			return;
		}

		echo '<div class="texttango-login-divider"></div>';
		$this->render( '', __( 'Or sign in with your phone', 'texttango' ) );
	}

	/**
	 * Output the markup.
	 *
	 * @param string $redirect Destination after signing in.
	 * @param string $title    Optional heading.
	 */
	private function render( string $redirect, string $title ): void {
		load_template(
			TEXTTANGO_PATH . 'templates/frontend/login-form.php',
			false,
			array(
				'redirect' => $redirect,
				'title'    => $title,
				'length'   => OtpService::CODE_LENGTH,
			)
		);
	}

	/**
	 * Enqueue the compiled stylesheet and the script.
	 */
	public function enqueue_assets(): void {
		wp_enqueue_style(
			self::HANDLE,
			TEXTTANGO_URL . 'assets/frontend/frontend.css',
			array(),
			TEXTTANGO_VERSION
		);

		wp_enqueue_script(
			self::HANDLE,
			TEXTTANGO_URL . 'assets/frontend/frontend.js',
			array(),
			TEXTTANGO_VERSION,
			true
		);

		/*
		 * A nonce and display strings only. Nothing here reveals whether any
		 * given number has an account (spec §5).
		 */
		wp_localize_script(
			self::HANDLE,
			'textTangoOtp',
			array(
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( LoginController::NONCE ),
				'i18n'    => array(
					'sending'      => __( 'Sending…', 'texttango' ),
					'verifying'    => __( 'Verifying…', 'texttango' ),
					'sendCode'     => __( 'Send code', 'texttango' ),
					'resend'       => __( 'Send a new code', 'texttango' ),
					'genericError' => __( 'Something went wrong. Please try again.', 'texttango' ),
					'needPhone'    => __( 'Enter your phone number.', 'texttango' ),
					'needCode'     => __( 'Enter the code we sent you.', 'texttango' ),
				),
			)
		);
	}
}
