<?php

/**
 * Kelas utama plugin
 */
class SecureAuth_Authenticator_2FA {

    /**
     * Loader untuk mendaftarkan semua hooks
     *
     * @var SecureAuth_Authenticator_2FA_Loader
     */
    protected $loader;

    /**
     * Nama plugin
     *
     * @var string
     */
    protected $plugin_name;

    /**
     * Versi plugin
     *
     * @var string
     */
    protected $version;

    /**
     * Inisialisasi plugin dan mendaftarkan hooks
     */
    public function __construct() {
        $this->plugin_name = 'secureauth-authenticator-2fa';
        $this->version = SA2FA_VERSION;

        $this->load_dependencies();
        $this->define_admin_hooks();
        $this->define_public_hooks();
    }

    /**
     * Load semua dependencies yang dibutuhkan plugin
     */
    private function load_dependencies() {
        // Loader untuk mendaftarkan hooks
        require_once SA2FA_PLUGIN_DIR . 'includes/class-secureauth-authenticator-2fa-loader.php';
        
        // Admin class untuk fungsi admin
        require_once SA2FA_PLUGIN_DIR . 'admin/class-secureauth-authenticator-2fa-admin.php';
        
        // Public class untuk fungsi publik
        require_once SA2FA_PLUGIN_DIR . 'public/class-secureauth-authenticator-2fa-public.php';

        $this->loader = new SecureAuth_Authenticator_2FA_Loader();
    }
    
    /**
     * Mendaftarkan semua hooks terkait admin
     */
    private function define_admin_hooks() {
        $plugin_admin = new SecureAuth_Authenticator_2FA_Admin($this->plugin_name, $this->version);
        
        // Tambahkan menu admin
        $this->loader->add_action('admin_menu', $plugin_admin, 'add_plugin_admin_menu');
        
        // Tambahkan field 2FA di profil pengguna
        $this->loader->add_action('show_user_profile', $plugin_admin, 'add_2fa_user_profile_fields');
        $this->loader->add_action('edit_user_profile', $plugin_admin, 'add_2fa_user_profile_fields');
        
        // Simpan field 2FA di profil pengguna
        $this->loader->add_action('personal_options_update', $plugin_admin, 'save_2fa_user_profile_fields');
        $this->loader->add_action('edit_user_profile_update', $plugin_admin, 'save_2fa_user_profile_fields');

        add_filter('sanitize_text_field', function($value) {
            return is_null($value) ? '' : $value;
        }, 0);
    }
    
    /**
     * Mendaftarkan semua hooks terkait publik
     */
    private function define_public_hooks() {
        $plugin_public = new SecureAuth_Authenticator_2FA_Public($this->plugin_name, $this->version);
        
        // Tambahkan form 2FA di halaman login
        $this->loader->add_action('login_form', $plugin_public, 'add_2fa_login_form');
        
        // Verifikasi 2FA saat login
        $this->loader->add_filter('authenticate', $plugin_public, 'verify_2fa_login', 50, 3);
    }
    
    /**
     * Jalankan plugin
     */
    public function run() {
        $this->loader->run();
    }
    
    /**
     * Nama plugin
     *
     * @return string
     */
    public function get_plugin_name() {
        return $this->plugin_name;
    }
    
    /**
     * Loader plugin
     *
     * @return SecureAuth_Authenticator_2FA_Loader
     */
    public function get_loader() {
        return $this->loader;
    }
    
    /**
     * Versi plugin
     *
     * @return string
     */
    public function get_version() {
        return $this->version;
    }
}