<?php

declare(strict_types=1);

namespace Builder\Core\Foundation\Dashboard\Widgets;

use Builder\Core\Foundation\Dashboard\Enums\WidgetWidth;
use Closure;

/**
 * A numeric counter widget: shows a number, label, and optional icon.
 *
 * Example:
 *   new CounterWidget('total-users', 'Total Users', fn() => User::count(), 'users')
 */
class CounterWidget extends Widget
{
    protected WidgetWidth $width = WidgetWidth::Third;

    public function __construct(
        private readonly string $key,
        private readonly string $label,
        private readonly Closure $valueResolver,
        private readonly ?string $icon = null,
    ) {}

    public function getKey(): string
    {
        return $this->key;
    }

    public function getLabel(): string
    {
        return $this->label;
    }

    public function getComponent(): string
    {
        return 'CounterWidget';
    }

    /** @return array<string, mixed> */
    public function getData(): array
    {
        return [
            'value' => ($this->valueResolver)(),
            'label' => $this->label,
            'icon' => $this->icon,
        ];
    }
}
