<?php
// pages/components/trigger_module_flow.php
// Reusable flow panel for trigger module cards.
// Expects: $modKey, $isOn, $m (optional), $expandPatternBg, $expandPatternBgType

use Components\Base;
use Components\Router;
use Components\Ui\Card;
use Components\Ui\FlowHelpers;

$modKey = isset($modKey) ? (string)$modKey : '';
$isOn = isset($isOn) ? (bool)$isOn : false;
$m = isset($m) && is_array($m) ? $m : ['key' => $modKey];
$expandPatternBg = isset($expandPatternBg) ? (bool)$expandPatternBg : true;
$expandPatternBgType = isset($expandPatternBgType) ? (string)$expandPatternBgType : 'dots';

?>
<?php $viewMode = \Components\ConfigStore::getViewMode('trigger'); ?>
<div id="flow-<?php echo Base::h($modKey); ?>"
     x-cloak
     x-show="openMap['<?php echo Base::h($modKey); ?>']"
     x-collapse.duration.180ms
     :class="openMap['<?php echo Base::h($modKey); ?>'] ? 'transform-gpu will-change-[height]' : 'transform-cpu'"
     @click.stop
     hx-get="<?php echo Router::url('trigger_module', ['module' => $modKey, 'partial' => 1, 'flow_only' => 1]); ?>"
     hx-trigger="<?php echo Base::h($modKey); ?>:expandable:refresh from:body"
     hx-target="#flow-<?php echo Base::h($modKey); ?>"
     hx-select="#flow-<?php echo Base::h($modKey); ?>"
     hx-swap="outerHTML"
     hx-disinherit="*"
     data-no-global-loader="1"
     x-data="Object.assign(<?= Base::h(FlowHelpers::alpineData('smooth', $viewMode), false); ?>, { localViewMode: '<?= Base::h($viewMode) ?>' })"
     x-init="<?= Base::h(FlowHelpers::viewModeInit($modKey, 'trigger'), false) ?>"
     @resize.window.debounce.120="if (openMap['<?php echo Base::h($modKey); ?>']) update()"
     @mouseover.debounce.120="if (openMap['<?php echo Base::h($modKey); ?>']) update()"
     @htmx:before-request="$event.detail.elt === $el ? refreshing = true : null"
     @htmx:after-request="$event.detail.elt === $el ? refreshing = false : null"
     class="overflow-hidden transition-[transform,opacity] duration-180 ease-out motion-reduce:transition-none flex flex-col">
    <div class="flex flex-col h-full">
        <!-- Teleport View Mode Toggle to Card Header -->
        <template x-teleport="[id='view-mode-toggle-target-<?php echo Base::h($modKey); ?>']">
            <div class="mx-4" x-show="openMap['<?php echo Base::h($modKey); ?>']">
                <?= FlowHelpers::renderViewModeToggle([
                    'edit_url' => Router::url('module_config', ['module' => $modKey, 'partial' => 1, 'drawer' => 1, 'mode' => 'edit']),
                    'details_url' => Router::url('analytics', ['module' => $modKey]),
                    'compact' => true,
                    'open_key' => $modKey,
                ]); ?>
            </div>
        </template>

        <div class="<?= Card::flowClasses() ?> flex-1 transform-gpu will-change-[transform,opacity] <?php echo $isOn ? '' : ''; ?>"
             :class="localViewMode === 'performance' ? '!bg-none !border-none' : ''">

            <?= FlowHelpers::renderBackground($modKey, $expandPatternBgType, $viewMode); ?>

            <div x-show="refreshing" x-cloak
                 class="pointer-events-none absolute inset-0 z-50 flex items-center justify-center bg-white/50 dark:bg-gray-900/50 transition-opacity duration-100 ease-in-out">
                <div class="flex items-center gap-2 text-secondary">
                    <?= \Components\Base::SVG_SPINNER; ?>
                </div>
            </div>

            <div class="h-full" x-show="localViewMode === 'journey'" x-cloak <?php if ($viewMode !== 'journey') echo 'style="display: none;"'; ?> x-init="$nextTick(() => htmx.process($el))">
                    <?= FlowHelpers::renderScrollHints(); ?>
                    <?= FlowHelpers::renderRefreshButton($modKey, 0, $modKey); ?>

                    <!-- Inner horizontal scroller: content scrolls beneath the fixed buttons; scrollbar stays here -->
                    <div x-ref="scroller" @scroll.passive="update()" class="relative overflow-x-auto overflow-y-hidden transform-gpu will-change-transform">
                        <div class="inline-block relative z-10 p-8">
                            <?php
                            try {
                                echo \mailbeez_pro::renderFlowStatic($m, true);
                            } catch (\Throwable $e) {
                                echo '<div class="text-xs text-red-600">' . Base::h($e->getMessage()) . '</div>';
                            }
                            ?>
                        </div>
                    </div>
                </div>

            <?php
            $perfOptions = ['lazy' => true, 'title' => true];
            if (($m['group'] ?? '') === 'review_feedback') {
                $perfOptions['context'] = \Components\Ui\PerformanceDashboard::CONTEXT_TRIGGER_TYPE_REVIEW;
            }
            ?>
            <?= FlowHelpers::renderPerformancePanel(
                Router::url('trigger_module', ['module' => $modKey, 'partial' => 1, 'view_mode' => 'performance']),
                'loading-perf-' . $modKey,
                \Components\Ui\PerformanceDashboard::render($modKey, 0, $perfOptions),
                $modKey,
                $viewMode
            ); ?>
        </div>
    </div>
</div>