<?php
/**
 * Shared wp-login and core auth form layout rules (reCAPTCHA + Turnstile).
 *
 * @package WP_PowerSuite\Captcha\Shared
 * @since 1.5.0
 */

namespace WP_PowerSuite\Captcha\Shared;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

/**
 * Core WordPress auth screens: minimal rules so captcha blocks flow like other fields.
 */
final class LoginLayoutStyles {

	private const CUSTOMIZOR_MODULE_ID = 'login-page-customizor';

	/** Default #login width when a captcha module is active (core WP uses 320px). */
	private const DEFAULT_LOGIN_WIDTH_PX = 350;

	/**
	 * Core WP / Woo auth forms that use narrow field rails.
	 */
	public static function coreAuthFormSelectors(): string {
		return '#loginform,#registerform,#lostpasswordform,#resetpassform,'
			. 'form.woocommerce-form-login,form.woocommerce-form-register,form.woocommerce-ResetPassword';
	}

	/**
	 * Attach shared auth-form layout CSS to the core login stylesheet.
	 *
	 * @param array<string, mixed> $context Unused; kept for call-site compatibility.
	 */
	public static function enqueue( array $context = [] ): void {
		WidgetLayoutStyles::markCoreAuthLayoutEnqueued();

		if ( ! wp_style_is( 'login', 'registered' ) ) {
			self::enqueueStandalone( $context );

			return;
		}

		wp_enqueue_style( 'login' );
		wp_add_inline_style( 'login', self::getCss( $context ) );
	}

	/**
	 * Auth layout on front-end custom login URLs (no core login stylesheet).
	 *
	 * @param array<string, mixed> $context Unused.
	 */
	public static function enqueueStandalone( array $context = [] ): void {
		WidgetLayoutStyles::markCoreAuthLayoutEnqueued();

		$handle = 'wpps-core-auth-captcha-layout';
		if ( ! wp_style_is( $handle, 'registered' ) ) {
			wp_register_style( $handle, false, [], '1.0.3' );
		}
		wp_enqueue_style( $handle );
		wp_add_inline_style( $handle, self::getCss( $context ) );
	}

	/**
	 * Narrow front-end forms (e.g. Basic Forms) — not wp-login.php.
	 */
	public static function getCoreFormFitCss(): string {
		return WidgetLayoutStyles::getCoreFormFitCss();
	}

	/**
	 * #login container width: 350px for captcha modules unless Login Page Customizor sets box width (box_form_width).
	 */
	public static function resolveLoginContainerWidth(): string {
		if ( OptionStoreModuleSettingsRepository::isModuleEnabled( self::CUSTOMIZOR_MODULE_ID ) ) {
			$settings = OptionStoreModuleSettingsRepository::forModule( self::CUSTOMIZOR_MODULE_ID )->all( [] );
			if ( is_array( $settings ) ) {
				$form_width = isset( $settings['box_form_width'] ) ? trim( (string) $settings['box_form_width'] ) : '';
				$num        = $form_width !== '' ? (int) $form_width : 0;
				if ( $num > 0 ) {
					$unit = isset( $settings['box_form_width_unit'] ) && $settings['box_form_width_unit'] === '%' ? '%' : 'px';

					return $num . $unit;
				}
			}
		}

		return self::DEFAULT_LOGIN_WIDTH_PX . 'px';
	}

	/**
	 * wp-login.php and matching core auth forms — do not fight core float layout.
	 *
	 * @param array<string, mixed> $context Unused.
	 */
	public static function getCss( array $context = [] ): string {
		unset( $context );

		$f     = self::coreAuthFormSelectors();
		$width = esc_attr( self::resolveLoginContainerWidth() );

		return <<<CSS
#login{
	width:{$width};
	max-width:100%;
}
{$f} .wpps-captcha-cushion{
	clear:both;
	margin-bottom:16px;
	text-align:left;
}
CSS
			. WidgetLayoutStyles::getCoreFormFitCss();
	}
}
