<?php
/**
 * Polanger Authenticator Addon bootstrap.
 *
 * @package Polanger_Dashboard_Tools
 */

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

require_once __DIR__ . '/includes/class-authenticator-totp.php';
require_once __DIR__ . '/includes/class-authenticator-page.php';
require_once __DIR__ . '/includes/class-authenticator-provider.php';
require_once __DIR__ . '/includes/class-authenticator-enrollment.php';
require_once __DIR__ . '/includes/class-authenticator-profile.php';

add_filter(
  'pdt_register_addons',
  /**
   * Register addon card in Addons screen.
   *
   * @param array $addons Addons map.
   * @return array
   */
  function ( $addons ) {
    if ( ! is_array( $addons ) ) {
      $addons = array();
    }
    if ( ! isset( $addons['polanger-authenticator'] ) ) {
      $addons['polanger-authenticator'] = array(
        'title'       => __( 'Polanger Authenticator', 'polanger-admin-menu-manager' ),
        'description' => __( 'Adds Google/Microsoft Authenticator (TOTP) as a factor provider for the core 2FA flow. Policy and enforcement remain in the 2FA tab.', 'polanger-admin-menu-manager' ),
        'status'      => 'inactive',
        'slug'        => 'polanger-settings',
        'is_core'     => false,
      );
    }
    return $addons;
  }
);

/**
 * Main addon orchestrator.
 */
class PDT_Polanger_Authenticator {

  /**
   * TOTP service.
   *
   * @var PDT_Authenticator_TOTP
   */
  private $totp;

  /**
   * Settings tab renderer.
   *
   * @var PDT_Authenticator_Page
   */
  private $page;

  /**
   * Login provider integration.
   *
   * @var PDT_Authenticator_Provider
   */
  private $provider;

  /**
   * Enrollment flow handler.
   *
   * @var PDT_Authenticator_Enrollment
   */
  private $enrollment;

  /**
   * User profile section.
   *
   * @var PDT_Authenticator_Profile
   */
  private $profile;

  /**
   * Constructor.
   */
  public function __construct() {
    $this->totp = new PDT_Authenticator_TOTP();
    $this->page = new PDT_Authenticator_Page( $this->totp );
    $this->enrollment = new PDT_Authenticator_Enrollment( $this->totp );
    $this->profile = new PDT_Authenticator_Profile( $this->totp );
    $this->provider = new PDT_Authenticator_Provider( $this->totp, $this->enrollment );

    // Register authenticator as an available 2FA method
    add_filter( 'pdt_2fa_available_methods', array( $this, 'register_method' ) );

    // Render setup UI inside the method selector (when authenticator is selected)
    add_action( 'pdt_2fa_render_method_authenticator', array( $this->page, 'render_tab' ), 10, 2 );
    add_action( 'admin_init', array( $this->page, 'maybe_handle_actions' ) );
    add_action( 'admin_enqueue_scripts', array( $this->page, 'enqueue_assets' ) );

    // Login flow integration
    add_filter( 'pdt_2fa_pre_authenticate', array( $this->provider, 'maybe_start_totp_challenge' ), 10, 5 );
    add_filter( 'pdt_2fa_verify_submitted_factor', array( $this->provider, 'verify_submitted_totp' ), 10, 4 );
    add_action( 'login_init', array( $this->provider, 'maybe_render_totp_challenge' ), 5 );
    add_action( 'login_init', array( $this->provider, 'handle_email_fallback' ), 4 );
    
    // Enrollment flow (for users who need to set up authenticator)
    add_action( 'login_init', array( $this->enrollment, 'maybe_render_enrollment_page' ), 3 );
    
    // User profile section (each user manages their own 2FA)
    $this->profile->init();
  }

  /**
   * Register authenticator as available 2FA method.
   *
   * @param array $methods Available methods.
   * @return array
   */
  public function register_method( $methods ) {
    if ( ! is_array( $methods ) ) {
      $methods = array();
    }
    if ( ! in_array( 'authenticator', $methods, true ) ) {
      $methods[] = 'authenticator';
    }
    return $methods;
  }
}
