<?php

declare(strict_types=1);

namespace Numinix\SeekmodoWordPress\Admin;

use Numinix\SeekmodoWordPress\Gateway\CircuitBreaker;
use Numinix\SeekmodoWordPress\Mode\AutoPromoter;
use Numinix\SeekmodoWordPress\Mode\ModeFsm;

/**
 * Admin bar badge showing current connector mode + breaker state at
 * a glance. Colors:
 *
 *   - green     mode in (active|enforce) AND breaker closed
 *   - amber     mode == shadow (or active w/ shadow substate)
 *   - red       breaker open OR mode == off
 *
 * Click target opens the connector settings page.
 */
final class ModeBadge
{
    public static function register(): void
    {
        if (!function_exists('add_action')) {
            return;
        }
        add_action('admin_bar_menu', [self::class, 'addNode'], 90);
        add_action('admin_head',     [self::class, 'cssAdmin']);
        add_action('wp_head',        [self::class, 'cssFront']);
    }

    /**
     * @param mixed $wp_admin_bar
     */
    public static function addNode($wp_admin_bar): void
    {
        if (!is_object($wp_admin_bar) || !method_exists($wp_admin_bar, 'add_node')) {
            return;
        }
        if (!function_exists('current_user_can') || !current_user_can('manage_options')) {
            return;
        }

        $mode    = ModeFsm::current();
        $eff     = AutoPromoter::effectiveMode();
        $breaker = CircuitBreaker::state();
        $klass   = self::colorClass($mode, $eff, $breaker);

        $label = sprintf(
            '<span class="seekmodo-badge seekmodo-badge-%s">Seekmodo: %s</span>',
            esc_attr($klass),
            esc_html($mode . ($mode === ModeFsm::MODE_ACTIVE ? ":{$eff}" : ''))
        );

        $wp_admin_bar->add_node([
            'id'    => 'seekmodo-mode-badge',
            'title' => $label,
            'href'  => admin_url('options-general.php?page=' . SettingsPage::SLUG),
            'meta'  => ['title' => sprintf('Mode %s · breaker %s', $mode, $breaker)],
        ]);
    }

    public static function cssAdmin(): void
    {
        self::printCss();
    }

    public static function cssFront(): void
    {
        if (!is_admin_bar_showing()) {
            return;
        }
        self::printCss();
    }

    private static function printCss(): void
    {
        echo '<style>
            .seekmodo-badge { padding: 1px 8px; border-radius: 2px; font-weight: 500; }
            .seekmodo-badge-green { background: #00a32a; color: #fff; }
            .seekmodo-badge-amber { background: #dba617; color: #1d2327; }
            .seekmodo-badge-red   { background: #d63638; color: #fff; }
            .seekmodo-badge-grey  { background: #50575e; color: #fff; }
        </style>';
    }

    private static function colorClass(string $mode, string $effective, string $breaker): string
    {
        if ($breaker === CircuitBreaker::STATE_OPEN) {
            return 'red';
        }
        if ($mode === ModeFsm::MODE_OFF) {
            return 'grey';
        }
        if ($mode === ModeFsm::MODE_SHADOW || $effective === ModeFsm::MODE_SHADOW) {
            return 'amber';
        }
        if ($mode === ModeFsm::MODE_ACTIVE || $mode === ModeFsm::MODE_ENFORCE) {
            return 'green';
        }

        return 'grey';
    }
}
