<?php
namespace Components\Data\Live;

use Components\Base;
use Components\Data\Contracts\AddonActionsProvider;

class LiveAddonActionsProvider implements AddonActionsProvider
{
    public function getActionsForModule(string $module): array
    {
        $actions = [];

        // Customer Groups config action from legacy (main_mailbeez.php:522)
        // Show only when the Filter Group add-on is active.
        try {
            if (function_exists('mh_cfg') && mh_cfg('MAILBEEZ_FILTER_GROUP_STATUS') === 'True') {
                // Build legacy URL via LegacyRouter mapping (see components/Legacy/legacy_routes.php)
                if (class_exists('Components\\Legacy\\LegacyRouter')) {
                    /** @var class-string $router */
                    $router = 'Components\\Legacy\\LegacyRouter';
                    // Compose URL and wrap with the iframe loader endpoint per UI guidelines
                    $legacyUrl = $router::url('filterCheckGroupConfig', ['m' => $module]);
                    $iframeUrl = $legacyUrl;

                    $actions[] = [
                        'group' => 'setup',
                        'label' => Base::lng('BUTTON_CONFIG_GROUPS', 'Configure Customer Groups'),
                        'icon' => Base::SVG_OUTLINE_USER_GROUP,
                        'variant' => 'ghost',
                        'size' => 'sm',
                        'attrs' => [
                            'command' => 'show-modal',
                            'commandfor' => 'config-modal',
                            'type'  => 'legacy',
                            'data-title' => \mh_lng('BUTTON_STYLING', 'edit styling'),

                            'data-size' => 'full-margin',
                            // Load iframe wrapper
                            'data-url' => $iframeUrl,
                            'hx-target' => '#config-modal-body',
                            'hx-swap' => 'innerHTML',
                        ],
                    ];

                }
            }
        } catch (\Throwable $e) {
            // Fail-safe: do not break UI if env not ready
        }

        // Analytics action: Opens report_analytics_advanced pre-filtered for the given module
        try {
            if (class_exists('Components\\Legacy\\LegacyRouter')) {
                /** @var \Components\Legacy\LegacyRouter $router */
                $router = 'Components\\Legacy\\LegacyRouter';
                $actions[] = [
                    'group' => 'other',
                    'label' => Base::lng('MAILBEEZ_BUTTON_ANALYTICS_LABEL', 'Analytics'),
                    'icon' => Base::SVG_OUTLINE_CHART_PIE,
                    'attrs' => [
                        'command' => 'show-modal',
                        'commandfor' => 'config-modal',
                        'data-title' => Base::lng('MAILBEEZ_BUTTON_ANALYTICS_LABEL', 'Analytics'),
                        'data-size' => 'full',
                        'data-url' => $router::url('reportAnalyticsAdvanced', ['selected_module' => $module]),
                        'hx-target' => '#config-modal-body',
                        'hx-swap' => 'innerHTML',
                    ],
                ];
            }
        } catch (\Throwable $e) {
            // Fail-safe
        }

        return $actions;
    }

    public function getGlobalActions(): array
    {
        return [];
    }
}
