<?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;
        $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 ' . $cardClasses);

        ob_start();
        ?>
        <div 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 flex-1 flex items-start justify-between gap-2">
                <div class="min-w-0 flex-1">
                    <?= $bodyHeaderHtml ?>
                    <?= $bodyFooterHtml ?>
                </div>
                <?php if ($actionsHtml): ?>
                <div class="flex-shrink-0">
                    <?= $actionsHtml ?>
                </div>
                <?php endif; ?>
            </div>
            <?= $scripts ?>
        </div>
        <?php
        return ob_get_clean();
    }
}
