<?php
namespace Components\Ui;

use Components\Base;
use Components\Button;

class EmptyState
{
    public static function render(array $options = []): string
    {
        $icon = (string)($options['icon'] ?? Base::SVG_TRIGGER);
        $iconColor = trim((string)($options['icon_color'] ?? 'bg-surface text-secondary'));

        $title = (string)($options['title'] ?? '');
        $text = (string)($options['text'] ?? '');

        $gridButtons = is_array($options['grid_buttons'] ?? null) ? $options['grid_buttons'] : [];
        $action = is_array($options['action'] ?? null) ? $options['action'] : [];

        $containerClass = trim((string)($options['class'] ?? 'mx-auto mt-8 max-w-xl rounded-3xl border border-dashed border-divider/70 bg-muted/20 px-8 py-14 text-center sm:px-12'));

        ob_start();
        ?>
        <div class="<?= Base::h($containerClass); ?>">
            <div class="mx-auto mb-5 inline-flex size-14 items-center justify-center rounded-xl shadow-sm ring-1 ring-divider/70 [&_svg]:size-8 <?= Base::h($iconColor); ?>">
                <?= $icon; ?>
            </div>

            <h3 class="text font-semibold tracking-tight text-primary">
                <?= Base::h($title); ?>
            </h3>

            <p class="mx-auto mt-3 max-w-2xl text-sm leading-6 text-secondary">
                <?= Base::h($text); ?>
            </p>

            <?php if (!empty($gridButtons)): ?>
                <div class="mt-5 grid grid-cols-1 gap-2 sm:grid-cols-3 sm:gap-3">
                    <?php foreach ($gridButtons as $button): ?>
                        <?php
                        if (is_array($button)) {
                            $label = (string)($button['label'] ?? '');
                            $attrs = is_array($button['attrs'] ?? null) ? $button['attrs'] : [];
                            $extraClass = trim((string)($button['class'] ?? ''));
                        } else {
                            $label = (string)$button;
                            $attrs = [];
                            $extraClass = '';
                        }

                        if ($label === '') {
                            continue;
                        }

                        if (!isset($attrs['type'])) {
                            $attrs['type'] = 'button';
                        }

                        $btnClass = 'inline-flex w-full items-center justify-center rounded-xl bg-surface px-3 py-2 text-xs text-secondary ring-1 ring-divider/70 transition-colors hover:bg-surface/80 sm:text-sm';
                        if ($extraClass !== '') {
                            $btnClass .= ' ' . $extraClass;
                        }

                        $attrs['class'] = trim((string)($attrs['class'] ?? '') . ' ' . $btnClass);
                        ?>
                        <button <?= Base::attrs($attrs); ?>>
                            <?= Base::h($label); ?>
                        </button>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

            <?php
            $actionLabel = (string)($action['label'] ?? '');
            if ($actionLabel !== ''):
                $actionOpts = $action;
                unset($actionOpts['label']);
                ?>
                <div class="mt-7">
                    <?= Button::render($actionLabel, $actionOpts); ?>
                </div>
            <?php endif; ?>
        </div>
        <?php
        return ob_get_clean();
    }

    /**
     * Render a simple, centered empty state with an icon in a circle.
     * Often used in panels or smaller content areas.
     */
    public static function simple(array $options = []): string
    {
        $icon = (string)($options['icon'] ?? Base::SVG_BELL);
        $title = (string)($options['title'] ?? '');
        $text = (string)($options['text'] ?? '');

        $containerClass = trim((string)($options['class'] ?? 'flex flex-col items-center justify-center py-5 text-center'));
        $iconContainerClass = trim((string)($options['icon_class'] ?? 'rounded-full bg-muted p-3 mb-4 text-secondary [&_svg]:size-8'));

        ob_start();
        ?>
        <div class="<?= Base::h($containerClass) ?>">
            <div class="<?= Base::h($iconContainerClass) ?>">
                <?= $icon ?>
            </div>
            <?php if ($title !== ''): ?>
                <h3 class="text-sm font-medium text-primary"><?= Base::h($title) ?></h3>
            <?php endif; ?>
            <?php if ($text !== ''): ?>
                <p class="text-xs text-secondary mt-1"><?= Base::h($text) ?></p>
            <?php endif; ?>
        </div>
        <?php
        return ob_get_clean();
    }

    /**
     * Render a positive variant of simple empty state.
     */
    public static function simpleSuccess(array $options = []): string
    {
        $defaults = [
            'icon' => Base::SVG_CHECK_CIRCLE,
            'class' => 'flex flex-col items-center justify-center py-5 text-center',
            'icon_class' => 'rounded-full  bg-muted p-3 mb-4 text-success-500 [&_svg]:size-8 ',
        ];

        return self::simple(array_merge($defaults, $options));
    }
}
