<?php
// components/Ui/CardUI.php
namespace Components\Ui;

use Components\Base;

/**
 * Generic Card UI component for BeezUI
 */
class CardUI extends Base
{
    /**
     * Render the base card structure
     *
     * @param array $options
     * @return string
     */
    public static function render(array $options): string
    {
        $cardClasses = $options['card_classes'] ?? '';
        $cardAttrs = $options['card_attrs'] ?? '';
        $previewContent = $options['preview_content'] ?? '';
        $bodyHeaderHtml = $options['body_header_html'] ?? '';
        $bodyFooterHtml = $options['body_footer_html'] ?? '';
        $actionsHtml = $options['actions_html'] ?? '';
        $previewClasses = $options['preview_classes'] ?? '';
        $previewAttrs = $options['preview_attrs'] ?? '';
        $scripts = $options['scripts'] ?? '';
        $isSmallClass = $options['is_small_class'] ?? "typeof columns !== 'undefined' && columns >= 5 ? 'is-small' : ''";
        $aspectRatio = $options['aspect_ratio'] ?? 'aspect-3/4';
        $noPreview = $options['no_preview'] ?? false;
        $interactive = $options['interactive'] ?? false;

        $tag = $options['tag'] ?? 'div';
        if (!isset($options['tag']) && $interactive && !empty($cardAttrs)) {
            if (strpos($cardAttrs, 'href=') !== false) {
                $tag = 'a';
            } else {
                $tag = 'button';
            }
        }

        $typeAttr = ($tag === 'button') ? 'type="button"' : '';
        $previewBaseClasses = $options['preview_base_classes'] ?? 'bg-gray-100 dark:bg-gray-900';
        $previewContainerClasses = $options['preview_container_classes'] ?? 'border border-divider/50';

        $containerClass = Card::baseContainerClass($interactive, 'group p-3 w-full h-full flex flex-col text-left ' . $cardClasses);

        ob_start();
        ?>
        <<?= $tag ?> <?= $typeAttr ?> class="<?= self::h($containerClass, false) ?>"
             <?php if ($isSmallClass): ?>:class="<?= $isSmallClass ?>"<?php endif; ?> <?= $cardAttrs ?>>
            
            <?php if (!$noPreview): ?>
            <div class="<?= self::h($aspectRatio, false) ?> <?= self::h($previewContainerClasses, false) ?> rounded-lg w-full flex-none <?= self::h($previewBaseClasses, false) ?> overflow-hidden relative <?= self::h($previewClasses, false) ?>"
                <?= $previewAttrs ?>>
                <?= $previewContent ?>
                <div class="absolute inset-0 bg-black/0 transition-colors pointer-events-none"></div>
            </div>
            <?php endif; ?>

            <div class="px-1 pt-3 <?= self::h($options['body_classes'] ?? 'flex-1 flex items-start justify-between gap-2', false) ?>">
                <div class="min-w-0 flex-1">
                    <?= $bodyHeaderHtml ?>
                    <?= $bodyFooterHtml ?>
                </div>
                <?php if ($actionsHtml): ?>
                <div class="flex-shrink-0">
                    <?= $actionsHtml ?>
                </div>
                <?php endif; ?>
            </div>
            <?= $scripts ?>
        </<?= $tag ?>>
        <?php
        return ob_get_clean();
    }
}
