<?php

use Leantime\Core\Events\EventDispatcher;
use Leantime\Domain\Auth\Services\Auth;
use Leantime\Domain\Plugins\Services\Registration;
use Leantime\Domain\Widgets\Models\Widget;

// Initialize plugin registration
$registration = app()->makeWith(Registration::class, ['pluginId' => 'AllTasks']);

// Register language files
$registration->registerLanguageFiles(['en-US', 'es-ES']);

// Register plugin views namespace
app('view')->addNamespace('alltasks', APP_ROOT . '/app/Plugins/AllTasks/Templates');

// Register the AllTasks widget (only visible to administrators)
EventDispatcher::add_filter_listener(
    'leantime.domain.widgets.services.widgets.__construct.availableWidgets',
    function ($widgets) {
        $auth = app()->make(Auth::class);
        
        if ($auth->userIsAtLeast('admin')) {
            $widgets['alltasks'] = app()->make(Widget::class, [
                'id' => 'alltasks',
                'name' => 'alltasks.widget.title',
                'description' => 'alltasks.widget.description',
                'widgetUrl' => BASE_URL . '/hx/AllTasks/AllTasks/get',
                'gridHeight' => 30,
                'gridWidth' => 8,
                'gridMinHeight' => 16,
                'gridMinWidth' => 4,
                'gridX' => 0,
                'gridY' => 37,
                'alwaysVisible' => false,
                'noTitle' => false,
                'fixed' => false,
            ]);
        }
        
        return $widgets;
    }
);
