<?php
/**
 * NimblyMail admin bar badge for the Developer Sandbox.
 *
 * @package NimblyMail\SMTP
 */

namespace NimblyMail\SMTP\Admin;

defined( 'ABSPATH' ) || exit;

use NimblyMail\SMTP\Options;
use WP_Admin_Bar;

/**
 * Class AdminBar
 *
 * Renders a red admin bar badge whenever the Developer Sandbox is active so
 * site admins cannot forget that no real emails are being delivered. Visible
 * on both wp-admin pages and the frontend when an admin is logged in.
 *
 * @since 1.0.0
 */
class AdminBar {

    /**
     * Style handle used to attach the sandbox badge CSS.
     *
     * Registered without a src and emitted via wp_add_inline_style() so the
     * tiny scoped CSS rides through the standard wp_enqueue_styles pipeline
     * (no inline <style> printed by the plugin directly).
     *
     * @since 1.0.0
     *
     * @var string
     */
    private const STYLE_HANDLE = 'nimblymail-smtp-sandbox-bar';

    /**
     * Register admin bar hooks.
     *
     * Hooks into admin_bar_menu at priority 999 so the badge always appends
     * after all other nodes. Styles are enqueued via wp_enqueue_scripts /
     * admin_enqueue_scripts so the CSS is emitted through the standard
     * wp_enqueue_styles pipeline on both frontend and backend contexts.
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function init(): void {
        add_action( 'admin_bar_menu', array( $this, 'register_node' ), 999 );
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
    }

    /**
     * Add the sandbox badge node to the admin bar.
     *
     * Bails silently when the current user lacks manage_options or when the
     * sandbox is not enabled. The node title intentionally contains a span so
     * the inline styles can target it precisely; WordPress permits safe HTML in
     * admin bar node titles.
     *
     * @since 1.0.0
     *
     * @param WP_Admin_Bar $wp_admin_bar The WordPress admin bar instance.
     *
     * @return void
     */
    public function register_node( WP_Admin_Bar $wp_admin_bar ): void {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        if ( ! Options::init()->get( 'sandbox', 'enabled' ) ) {
            return;
        }

        $mode = Options::init()->get( 'sandbox', 'mode' );

        if ( 'redirect' === $mode ) {
            $safe_address = Options::init()->get( 'sandbox', 'safe_address' );
            $tooltip      = sprintf(
                /* translators: %s: the redirect email address */
                __( 'Sandbox: Redirect — all email goes to %s.', 'nimblymail-smtp' ),
                $safe_address
            );
        } else {
            $tooltip = __( 'Sandbox: Log Only — emails are captured but not delivered.', 'nimblymail-smtp' );
        }

        $wp_admin_bar->add_node(
            array(
                'id'    => 'nimblymail-smtp-sandbox',
                'title' => '<span class="nimblymail-smtp-sandbox-badge">' . esc_html__( 'NimblyMail: Sandbox Active', 'nimblymail-smtp' ) . '</span>',
                'href'  => esc_url( admin_url( 'admin.php?page=nimblymail-smtp-settings#/settings/sandbox' ) ),
                'meta'  => array(
                    'title' => esc_attr( $tooltip ),
                ),
            )
        );
    }

    /**
     * Enqueue the sandbox admin bar badge styles.
     *
     * Registers a handle with no src and attaches the scoped CSS via
     * wp_add_inline_style() so the styles are emitted through WordPress'
     * standard wp_enqueue_styles pipeline rather than via a raw <style>
     * block in wp_head / admin_head. Bails when the user cannot manage
     * options or when the sandbox is not active so no asset is registered
     * for users / requests that would not use it.
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function enqueue_styles(): void {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        if ( ! Options::init()->get( 'sandbox', 'enabled' ) ) {
            return;
        }

        wp_register_style( self::STYLE_HANDLE, false, array(), NIMBLYMAIL_VERSION );
        wp_enqueue_style( self::STYLE_HANDLE );

        $css  = '#wp-admin-bar-nimblymail-smtp-sandbox > a { background-color: #d63638 !important; color: #fff !important; font-weight: 600; }';
        $css .= '#wp-admin-bar-nimblymail-smtp-sandbox:hover > a, #wp-admin-bar-nimblymail-smtp-sandbox.hover > a { background-color: #b32d2e !important; }';
        $css .= '#wp-admin-bar-nimblymail-smtp-sandbox .nimblymail-smtp-sandbox-badge { color: #fff; }';

        wp_add_inline_style( self::STYLE_HANDLE, $css );
    }
}
