<?php

declare(strict_types=1);

namespace Builder\Core\Extensions\Dashboard\Widgets;

use Builder\Core\BuilderServiceProvider;
use Builder\Core\Foundation\Dashboard\Enums\WidgetWidth;
use Builder\Core\Foundation\Dashboard\Widgets\Widget;
use Builder\Core\Foundation\Definition\JsModule;
use Builder\Core\Foundation\Registry;

/**
 * Built-in system information widget.
 *
 * Displays Builder core version, PHP version, Laravel version,
 * and a list of all registered modules with their keys and labels.
 *
 * Registered automatically by BuilderServiceProvider.
 */
final class SystemInfoWidget extends Widget
{
    protected WidgetWidth $width = WidgetWidth::Half;

    /** Render last by default so user-registered widgets appear first. */
    protected int $order = 100;

    public function __construct(private readonly Registry $registry) {}

    public function getKey(): string
    {
        return 'system-info';
    }

    public function getLabel(): string
    {
        return 'System Information';
    }

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

    /** @return array<string, mixed> */
    public function getData(): array
    {
        $modules = array_map(
            fn (JsModule $module): array => [
                'key' => $module->getKey(),
                'label' => $module->getLabel() ?? $module->getKey(),
                'version' => $module->getVersion(),
                'author' => $module->getAuthor(),
                'author_url' => $module->getAuthorUrl(),
            ],
            $this->registry->all(),
        );

        return [
            'builder_version' => BuilderServiceProvider::CURRENT_VERSION,
            'php_version' => PHP_VERSION,
            'laravel_version' => app()->version(),
            'modules' => $modules,
        ];
    }
}
