<?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
    {
        $user = $request->user();

        return array_merge(parent::share($request), [
            'auth' => [
                'user' => $user ? [
                    'id' => $user->id,
                    'firstName' => $user->first_name ?? '',
                    'lastName' => $user->last_name ?? '',
                    'username' => $user->username,
                    'roleKey' => $user->role_key,
                    'schoolId' => $user->school_id,
                ] : null,
            ],
            'flash' => [
                'success' => fn () => $request->session()->get('success'),
                'error' => fn () => $request->session()->get('error'),
                'temp_password' => fn () => $request->session()->get('temp_password'),
            ],
        ]);
    }
}
