<?php
// pages/components/newsletter_item_created_success.php
require_once BEEZUI_ROOT . '/components/index.php';

use Components\Base;
use Components\Router;
use Components\Button;
use Components\Models\NewsletterItem;
use Components\Support\Htmx;



$id = (int)\mh_get('newsletter_item_id', 0, 'int');
$campaignId = (int)\mh_get('campaign_id', 0, 'int');

/** @var NewsletterItem $newsletterItem */
$newsletterItem = ($id > 0 && class_exists(NewsletterItem::class))
    ? NewsletterItem::find($id)
    : null;

if (!$newsletterItem) {
    dd('Newsletter item not found: ID=' . $id . ', Campaign ID=' . $campaignId);
}

$mb = $newsletterItem->mbNewsletter();
$rootId = $newsletterItem->resolveFlowRootId();
$pageMode = $newsletterItem->getPageMode();
$editorRoutes = $mb->getNewsletterEditorRoutes($id, $rootId, $pageMode);
$r = $editorRoutes['editor'] ?? [];

$editorUrl = $r['url'] ?? Router::url('email_editor', ['module' => 'mb_newsletter', 'newsletter_id' => $id]);
$modalId = Htmx::resolvePanelId();

ob_start();
?>
<div class="text-center" id="config-modal-body">
    <script>
        (function() {
            var m = document.getElementById('<?= Base::h($modalId, false); ?>');
            if (m) {
                var title = <?= json_encode(\mh_lng('UI_NEWSLETTER_CREATED_TITLE', 'Newsletter Created')); ?>;
                <?php if ($pageMode === \Components\Newsletter\PageConfig::MODE_AUTOMATION): ?>
                title = <?= json_encode(\mh_lng('UI_AUTOMATION_CREATED_TITLE', 'Automation Created')); ?>;
                <?php endif; ?>
                m.setAttribute('data-title', title);
                m.setAttribute('data-size', '2xl');
            }
        })();
    </script>

    <?= \Components\Newsletter\WizardSteps::renderStepper(\Components\Newsletter\WizardSteps::STEP_CONFIRMATION); ?>

    <div class="p-8 pt-0">
        <div class="size-20 rounded-full bg-green-100 text-green-600 flex items-center justify-center mx-auto mb-6">
            <svg class="size-10" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
            </svg>
        </div>

        <h2 class="text-2xl font-bold mb-2">
            <?php if ($pageMode === \Components\Newsletter\PageConfig::MODE_AUTOMATION): ?>
                <?= \mh_lng('UI_AUTOMATION_CREATED_SUCCESS', 'Automation created successfully!'); ?>
            <?php else: ?>
                <?= \mh_lng('UI_NEWSLETTER_CREATED_SUCCESS', 'Newsletter created successfully!'); ?>
            <?php endif; ?>
        </h2>
        <p class="text-secondary mb-8"><?= \mh_lng('UI_NEWSLETTER_CREATED_NEXT_STEPS', 'What would you like to do next?'); ?></p>

        <div class="flex flex-col gap-3 max-w-sm mx-auto">
            <?php
            // Action: Edit
            $editLabel = ($pageMode === \Components\Newsletter\PageConfig::MODE_AUTOMATION)
                ? \mh_lng('UI_BUTTON_EDIT_AUTOMATION', 'Edit Automation')
                : \mh_lng('UI_BUTTON_EDIT_NEWSLETTER', 'Edit Newsletter');

            echo Button::render($editLabel, [
                'variant' => 'primary',
                'size' => 'lg',
                'class' => 'inline!',
                'attrs' => [
                    'command' => 'show-modal',
                    'commandfor' => $modalId,
                    'data-title' => $r['title'] ?? \mh_lng('MAILBEEZ_BUTTON_EDITOR_LABEL', 'Editor'),
                    'data-size' => $r['size'] ?? 'full',
                    'data-body-class' => 'p-2!',
                    'data-scroll-hints' => ($r['scroll_hints'] ?? false) ? 'true' : 'false',
                    'hx-get' => $editorUrl,
                    'hx-target' => '#' . $modalId . '-body',
                    'hx-swap' => 'innerHTML',
                    'hx-push-url' => 'false',
                ],
            ]);

            // Action: Back
            echo Button::render(\mh_lng('UI_BUTTON_IMPROVE_FLOW', 'Back to Campaign'), [
                'variant' => 'ghost',
                'size' => 'md',
                'class' => 'inline!',
                'attrs' => ['command' => 'close', 'commandfor' => $modalId],
            ]);
            ?>
        </div>
    </div>
</div>
<?php
echo ob_get_clean();
