<?php
/**
 * Front-end assets manager.
 *
 * Responsible for:
 * - registering CSS/JS assets with cache-busting (filemtime),
 * - conditionally enqueueing assets on front-end and login screens,
 * - exposing minimal data to JavaScript.
 */

declare(strict_types=1);
if (!defined('ABSPATH')) exit;

final class SESLP_Assets {
  private const STYLE_HANDLE = 'seslp-front';
  private const SCRIPT_HANDLE = 'seslp-front';

  /**
   * Register WordPress hooks for asset handling.
   *
   * @return void
   */
  public function register(): void {
    add_action('init', [$this, 'register_assets']);
    add_action('wp_enqueue_scripts', [$this, 'enqueue_front']);
    add_action('login_enqueue_scripts', [$this, 'enqueue_front']);
  }

  /**
   * Register styles and scripts using filemtime-based versioning.
   *
   * Falls back to plugin version when files are not found.
   *
   * @return void
   */
  public function register_assets(): void {
    $plugin   = SESLP_Plugin::instance();
    $css_rel  = 'assets/css/style.css';
    $js_rel   = 'assets/js/social-login.js';

    $css_path = $plugin->dir . $css_rel;
    $js_path  = $plugin->dir . $js_rel;

    $css_ver  = file_exists($css_path) ? (string) filemtime($css_path) : SESLP_Plugin::VERSION;
    $js_ver   = file_exists($js_path)  ? (string) filemtime($js_path)  : SESLP_Plugin::VERSION;

    wp_register_style(self::STYLE_HANDLE, $plugin->url . $css_rel, [], $css_ver);
    wp_register_script(self::SCRIPT_HANDLE, $plugin->url . $js_rel, ['jquery'], $js_ver, true);

    // Expose minimal bootstrap data and error messages for toast
    wp_localize_script('seslp-front', 'SESLP', [
      'ajaxUrl'       => admin_url('admin-ajax.php'),
      'version'       => SESLP_Plugin::VERSION,
      'errorMessages' => [
        'email_exists'                    => __('This email is already registered on this site. Please sign in with the originally linked method (username/password or the same social provider).', 'simple-easy-social-login-oauth-login'),
        'invalid_state'                   => __('Your session has expired or the login attempt was invalid. Please try again.', 'simple-easy-social-login-oauth-login'),
        'invalid_nonce'                   => __('Your session has expired or the login attempt was invalid. Please try again.', 'simple-easy-social-login-oauth-login'),
        'token_exchange_failed'           => __('Failed to obtain access token from the provider. Please try again.', 'simple-easy-social-login-oauth-login'),
        'oauth_exception'                 => __('A social login error occurred while communicating with the provider. Please try again.', 'simple-easy-social-login-oauth-login'),
        'oauth_failed'                    => __('Social login failed. Please try again.', 'simple-easy-social-login-oauth-login'),
        'invalid_provider'                => __('The requested social login provider is invalid.', 'simple-easy-social-login-oauth-login'),
        'provider_not_allowed'            => __('This social login provider is not allowed on this site.', 'simple-easy-social-login-oauth-login'),
        'config_missing'                  => __('This social login provider is not configured correctly. Please contact the site administrator.', 'simple-easy-social-login-oauth-login'),
        'email_missing'                   => __('We could not retrieve an email address from the provider. Please use another login method or contact the site administrator.', 'simple-easy-social-login-oauth-login'),
        'account_link_failed'             => __('We could not link your social account to this site. Please try again.', 'simple-easy-social-login-oauth-login'),
        'registration_disabled_by_plugin' => __('New account creation through social login is currently disabled on this site.', 'simple-easy-social-login-oauth-login'),
        '_default'                        => __('Social login failed. Please try again.', 'simple-easy-social-login-oauth-login'),
      ],
    ]);
  }

  /**
   * Conditionally enqueue front-end assets.
   *
   * Assets are loaded only when at least one provider is active,
   * unless overridden via filter.
   *
   * @return void
   */
  public function enqueue_front(): void {
    $has_active_provider = $this->has_active_provider();

    // Allow filters to override the decision
    $should = (bool) apply_filters('seslp_should_enqueue_assets', $has_active_provider);

    if (!$should) {
      return;
    }

    wp_enqueue_style(self::STYLE_HANDLE);
    wp_enqueue_script(self::SCRIPT_HANDLE);
  }

  /**
   * Determine whether at least one provider is properly configured.
   *
   * A provider is considered active when both client_id and client_secret exist.
   * If the providers registry is unavailable, assets are loaded by default.
   *
   * @return bool
   */
  private function has_active_provider(): bool {
    if (!class_exists('SESLP_Providers_Registry')) {
      return true;
    }

    $providers = SESLP_Providers_Registry::all();

    foreach ($providers as $slug => $class) {
      $client_id     = SESLP_Helpers::get_provider_option($slug, 'client_id', '');
      $client_secret = SESLP_Helpers::get_provider_option($slug, 'client_secret', '');

      if ($client_id !== '' && $client_secret !== '') {
        return true;
      }
    }

    return false;
  }
}