<?php

namespace WPD_Platform;

use WPD_Platform\Services\LoginService;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Authentication
 *
 * Handles platform authentication functionality including auto-login and login failure messages.
 *
 * @package WPD_Platform
 * @since 1.0.0
 */
class Authentication {

	/**
	 * Initialize authentication hooks.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'template_redirect', [ LoginService::instance(), 'do_autologin' ], 999 );
		add_action( 'login_message', [ $this, 'do_login_fail' ], - 1 );
	}

	/**
	 * Display custom login failure message.
	 *
	 * @since 1.0.0
	 * @param string $message The original login message
	 * @return string Modified login message
	 */
	public function do_login_fail( $message ) {
		if ( empty( $message ) && isset( $_GET['token-expired'] ) ) {
			return '<div class="notice notice-error" style="background: #27272a; padding: 12px 16px; color: #fafafa; font-weight: 400; border-left: 4px solid #ef4444; border-radius: 4px;">' .
				   '<strong>' . esc_html__( 'Login link expired', 'dollie' ) . '</strong><br>' .
				   esc_html__( 'This one-time login link has already been used. Open your Dollie dashboard to generate a new one.', 'dollie' ) .
				   '</div>';
		}

		return $message;
	}

}
