<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
    protected $rootView = 'app';

    public function version(Request $request): ?string
    {
        return parent::version($request);
    }

    public function share(Request $request): array
    {
        return [
            ...parent::share($request),
            'auth' => [
                'user' => $request->user() ? [
                    'id' => $request->user()->id,
                    'surname' => $request->user()->surname,
                    'firstname' => $request->user()->firstname,
                    'othername' => $request->user()->othername,
                    'full_name' => $request->user()->full_name,
                    'email' => $request->user()->email,
                    'role' => $request->user()->role,
                    'designation' => $request->user()->designation,
                    'department' => $request->user()->department ? [
                        'id' => $request->user()->department->id,
                        'name' => $request->user()->department->name,
                    ] : null,
                    'avatar' => $request->user()->avatar,
                    'ippis_no' => $request->user()->ippis_no,
                ] : null,
            ],
            'flash' => [
                'success' => fn () => $request->session()->get('success'),
                'error' => fn () => $request->session()->get('error'),
                'warning' => fn () => $request->session()->get('warning'),
                'info' => fn () => $request->session()->get('info'),
            ],
        ];
    }
}
