<?php

namespace App\Providers;

use App\Livewire\FileUpload;
use App\Livewire\ImageUploader;
use App\Livewire\PdfViewer;
use App\Livewire\ProjectShow;
use App\Livewire\Toolbar;
use App\View\Components\Multiselect;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Validator;
use Livewire\Livewire;

class AppServiceProvider extends ServiceProvider
{

    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        // Configuración de componentes Blade
        Blade::componentNamespace('App\\View\\Components', 'icons');
        Blade::component('multiselect', Multiselect::class);

        // Registro de componentes Livewire
        Livewire::component('project-show', ProjectShow::class);
        Livewire::component('file-upload', FileUpload::class);
        Livewire::component('toolbar', Toolbar::class);
        Livewire::component('image-uploader', ImageUploader::class);
        Livewire::component('pdf-viewer', PdfViewer::class);

        // Validación personalizada
        Validator::extend('max_upload_size', function ($attribute, $value, $parameters, $validator) {
            $maxSize = env('MAX_UPLOAD_SIZE', 51200); // 50MB por defecto
            $totalSize = array_reduce($value, function($sum, $file) {
                return $sum + $file->getSize();
            }, 0);
            
            return $totalSize <= ($maxSize * 1024);
        });
        
    }
}
