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

use Components\Base;
use Components\Button;

$pageKey = 'sitebeez_integration_code';

$code = '';
$error = '';

try {
    $corePath = defined('MH_DIR_CONFIG') ? (string)MH_DIR_CONFIG : '';
    if ($corePath !== '') {
        require_once(MH_DIR_CONFIG . 'config_sitebeez/classes/sitebeez_core.php');
    }

    if (!class_exists('sitebeez_core', false)) {
        $error = mh_lng('UI_SITEBEEZ_INTEGRATION_CODE_ERROR', 'SiteBeez integration code is not available in this runtime.');
    } elseif (mh_cfg('MAILBEEZ_NEWSLETTER_SUBSCRIBER_SUPPORT') != 'True') {
        // Legacy module provides a specific hint constant; fall back to a generic message if it is not loaded.
        $code = defined('MAILBEEZ_CONFIG_SITEBEEZ_JS_WARNING_HINT')
            ? (string)MAILBEEZ_CONFIG_SITEBEEZ_JS_WARNING_HINT
            : (string)mh_lng('UI_SITEBEEZ_INTEGRATION_CODE_WARNING', 'Newsletter subscriber support is not enabled.');
    } else {
        $obj = new sitebeez_core();
        $code = (string)$obj->getJavaScriptCode();
    }
} catch (\Throwable $e) {
    $error = mh_lng('UI_SITEBEEZ_INTEGRATION_CODE_ERROR', 'SiteBeez integration code is not available in this runtime.');
}

ob_start();
?>
<div class="space-y-4">
    <div>
        <h3 class="text-lg font-semibold">
            <?= Base::h(mh_lng('UI_SITEBEEZ_INTEGRATION_CODE_MODAL_TITLE', 'Site signup integration')); ?>
        </h3>
        <p class="text-sm text-gray-600 dark:text-gray-300">
            <?= Base::h(mh_lng('UI_SITEBEEZ_INTEGRATION_CODE_MODAL_DESC', 'Copy & paste this JavaScript snippet into your website.')); ?>
        </p>
    </div>

    <?php if ($error !== '') { ?>
        <div class="rounded-md border border-red-200 bg-red-50 p-3 text-sm text-red-700 dark:border-red-900/50 dark:bg-red-900/20 dark:text-red-200">
            <?= Base::h($error); ?>
        </div>
    <?php } ?>

    <div class="space-y-2">
        <div class="flex items-center gap-2">
            <?php
            echo Button::render(mh_lng('UI_COPY', 'Copy'), [
                'variant' => 'secondary',
                'size' => 'sm',
                'attrs' => [
                    'id' => 'sitebeez-copy-btn',
                    'type' => 'button',
                ],
            ]);
            ?>
            <span id="sitebeez-copy-status" class="text-sm text-gray-500 dark:text-gray-400"></span>
        </div>

        <textarea
            id="sitebeez-integration-code"
            class="w-full min-h-96 font-mono text-xs rounded-md border border-divider/60 bg-white p-3 shadow-xs dark:bg-black/20 dark:border-muted/20"
            spellcheck="false"
            readonly
        ><?= Base::h($code, false); ?></textarea>
    </div>

    <script>
        (function () {
            var btn = document.getElementById('sitebeez-copy-btn');
            var ta = document.getElementById('sitebeez-integration-code');
            var status = document.getElementById('sitebeez-copy-status');
            if (!btn || !ta) return;

            var copiedText = <?= json_encode(mh_lng('UI_COPIED', 'Copied.')); ?>;
            var failedText = <?= json_encode(mh_lng('UI_COPY_FAILED', 'Copy failed. Please copy manually.')); ?>;

            btn.addEventListener('click', function () {
                try {
                    var text = ta.value || '';
                    if (navigator.clipboard && navigator.clipboard.writeText) {
                        navigator.clipboard.writeText(text).then(function () {
                            if (status) status.textContent = copiedText;
                        }).catch(function () {
                            if (status) status.textContent = failedText;
                        });
                    } else {
                        ta.focus();
                        ta.select();
                        var ok = document.execCommand('copy');
                        if (status) status.textContent = ok ? copiedText : failedText;
                    }
                } catch (e) {
                    if (status) status.textContent = failedText;
                }
            });
        })();
    </script>
</div>
<?php
$content = ob_get_clean();

if ((int)mh_request('partial', 0) === 1) {
    echo $content;
    return;
}

echo layout($pageKey, $content);
