<?php
declare(strict_types=1);

if (!defined('ABSPATH')) { exit; }

/**
 * Account (customer dashboard) element renderer.
 *
 * Phase 1, step 12. Renders the customer account block — settings
 * govern colors of the account UI (text, nav active state, button)
 * and the radius of the content card; the actual UI lives in
 * `templates/shortcodes/account.php`.
 *
 * Frontend includes the template directly; editor uses do_shortcode.
 * Frontend defaults `border_radius` to 4; editor to 10 — pre-extraction
 * drift. Unified to 4 (frontend production reality).
 *
 * Editor passes `preview_loggedout` global so the template can render
 * the logged-out auth shell instead of the dashboard; frontend doesn't.
 */
function impulse_element_render_account($el, array $settings, string $context): string {
    if (is_object($el)) $el = (array) $el;
    $elId     = preg_replace('/[^A-Za-z0-9_-]/', '', (string) ($el['id'] ?? '0'));
    $isEditor = ($context === 'editor');

    $maxWidth = intval($settings['section_max_width'] ?? 960);
    $bg       = (string) ($settings['bg_color']         ?? '#ffffff');
    $text     = (string) ($settings['text_color']       ?? '#1d2327');
    $navBg    = (string) ($settings['nav_active_bg']    ?? '#1d2327');
    $navColor = (string) ($settings['nav_active_color'] ?? '#ffffff');
    $btnBg    = (string) ($settings['btn_bg']           ?? '#1d2327');
    $btnColor = (string) ($settings['btn_color']        ?? '#ffffff');
    $radius   = intval($settings['border_radius']       ?? 4);

    $animAttrs = (!$isEditor && function_exists('impulse_get_animation_attrs'))
        ? impulse_get_animation_attrs($settings)
        : ['class' => '', 'style' => ''];
    $visClass = (!$isEditor && function_exists('impulse_get_visibility_classes'))
        ? impulse_get_visibility_classes($settings)
        : '';

    $acctId = 'acct-' . esc_attr((string) $elId);
    $sel    = '#' . $acctId;
    $parentId = intval($el['parent_id'] ?? 0);
    $maxStyle = $maxWidth > 0
        ? 'max-width:' . $maxWidth . 'px;' . ($parentId ? '' : 'margin-left:auto;margin-right:auto;')
        : '';

    $css  = $sel . ' .instinctor-account{color:' . esc_attr($text) . ';' . $maxStyle . '}';
    $css .= $sel . ' .instinctor-account-content{background:' . esc_attr($bg) . ';border-radius:' . $radius . 'px;}';
    $css .= $sel . ' .instinctor-account-auth{background:' . esc_attr($bg) . ';border-radius:' . $radius . 'px;' . $maxStyle . '}';
    $css .= $sel . ' .instinctor-account-nav a.active{background:' . esc_attr($navBg) . ';color:' . esc_attr($navColor) . ';}';
    $css .= $sel . ' .instinctor-btn,' . $sel . ' .instinctor-register-btn{background:' . esc_attr($btnBg) . ';color:' . esc_attr($btnColor) . ';}';
    if (function_exists('wp_add_inline_style')) {
        wp_add_inline_style('instinctor-page-inline', $css);
    }

    $GLOBALS['impulse_account_settings'] = $settings;
    if ($isEditor) {
        $GLOBALS['impulse_preview_loggedout'] = !empty($settings['preview_loggedout']);
    }

    $wrapCls = 'impulse-account-block';
    if (!empty($animAttrs['class'])) $wrapCls .= ' ' . $animAttrs['class'];
    if ($visClass !== '')            $wrapCls .= ' ' . $visClass;
    $wrapStyle = 'display:flex;justify-content:center;width:100%' . (!empty($animAttrs['style']) ? ';' . $animAttrs['style'] : '');

    ob_start();
    echo '<div id="' . esc_attr($acctId) . '" class="' . esc_attr($wrapCls) . '" style="' . esc_attr($wrapStyle) . '">';
    // Use the shortcode in both contexts. FrontendController registers
    // `instinctor_account` globally; do_shortcode is cleaner than
    // including the template by path (pre-extraction frontend did the
    // direct include, which couples this renderer to the template
    // location and creates a hidden dep on classes the template loads).
    if (function_exists('do_shortcode')) {
        echo do_shortcode('[instinctor_account]');
    } else {
        $tpl = INSTINCTOR_PLUGIN_DIR . 'templates/shortcodes/account.php';
        if (file_exists($tpl)) {
            include $tpl;
        }
    }
    echo '</div>';

    unset($GLOBALS['impulse_account_settings']);
    if ($isEditor) unset($GLOBALS['impulse_preview_loggedout']);

    return ob_get_clean();
}
