<?php

declare(strict_types=1);

use Builder\Core\BuilderServiceProvider;
use Builder\Core\Extensions\Dashboard\Widgets\SystemInfoWidget;
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;

it('SystemInfoWidget extends Widget', function (): void {
    $widget = new SystemInfoWidget(new Registry);
    expect($widget)->toBeInstanceOf(Widget::class);
});

it('SystemInfoWidget key is system-info', function (): void {
    expect((new SystemInfoWidget(new Registry))->getKey())->toBe('system-info');
});

it('SystemInfoWidget label is System Information', function (): void {
    expect((new SystemInfoWidget(new Registry))->getLabel())->toBe('System Information');
});

it('SystemInfoWidget component is SystemInfoWidget', function (): void {
    expect((new SystemInfoWidget(new Registry))->getComponent())->toBe('SystemInfoWidget');
});

it('SystemInfoWidget width is half', function (): void {
    expect((new SystemInfoWidget(new Registry))->getWidth())->toBe(WidgetWidth::Half);
});

it('SystemInfoWidget isHideable returns true', function (): void {
    expect((new SystemInfoWidget(new Registry))->isHideable())->toBeTrue();
});

it('SystemInfoWidget getData includes builder version', function (): void {
    $data = (new SystemInfoWidget(new Registry))->getData();

    expect($data['builder_version'])->toBe(BuilderServiceProvider::CURRENT_VERSION);
});

it('SystemInfoWidget getData includes php version', function (): void {
    $data = (new SystemInfoWidget(new Registry))->getData();

    expect($data['php_version'])->toBe(PHP_VERSION);
});

it('SystemInfoWidget getData includes laravel version', function (): void {
    $data = (new SystemInfoWidget(new Registry))->getData();

    expect($data)->toHaveKey('laravel_version')
        ->and($data['laravel_version'])->toBeString()->not->toBeEmpty();
});

it('SystemInfoWidget getData includes modules list', function (): void {
    $registry = new Registry;
    $registry->register(
        JsModule::make()->setKey('my-module')->setLabel('My JsModule'),
    );

    $data = (new SystemInfoWidget($registry))->getData();

    expect($data['modules'])->toHaveCount(1)
        ->and($data['modules'][0]['key'])->toBe('my-module')
        ->and($data['modules'][0]['label'])->toBe('My JsModule');
});

it('SystemInfoWidget getData includes module version and author', function (): void {
    $registry = new Registry;
    $registry->register(
        JsModule::make()->setKey('my-module')->setLabel('My JsModule')
            ->setVersion('1.2.3')
            ->setAuthor('Acme Corp'),
    );

    $data = (new SystemInfoWidget($registry))->getData();

    expect($data['modules'][0]['version'])->toBe('1.2.3')
        ->and($data['modules'][0]['author'])->toBe('Acme Corp');
});

it('SystemInfoWidget getData includes null version and author when not set', function (): void {
    $registry = new Registry;
    $registry->register(
        JsModule::make()->setKey('my-module')->setLabel('My JsModule'),
    );

    $data = (new SystemInfoWidget($registry))->getData();

    expect($data['modules'][0]['version'])->toBeNull()
        ->and($data['modules'][0]['author'])->toBeNull();
});

it('SystemInfoWidget is registered by BuilderServiceProvider', function (): void {
    $registry = app(Registry::class);

    $keys = array_map(fn (Widget $w) => $w->getKey(), $registry->widgets());

    expect($keys)->toContain('system-info');
});
