<?php

namespace App\Http\Middleware;

use App\Models\CtsSyncLog;
use App\Models\Setting;
use Illuminate\Http\Request;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
    /**
     * The root template that is loaded on the first page visit.
     *
     * @var string
     */
    protected $rootView = 'app';

    /**
     * Determine the current asset version.
     */
    public function version(Request $request): ?string
    {
        return parent::version($request);
    }

    /**
     * Define the props that are shared by default.
     *
     * @return array<string, mixed>
     */
    public function share(Request $request): array
    {
        return [
            ...parent::share($request),
            'auth' => [
                'user' => $request->user() ? [
                    'id' => $request->user()->id,
                    'name' => $request->user()->name,
                    'email' => $request->user()->email,
                    'avatar' => $request->user()->avatar,
                ] : null,
            ],
            'flash' => [
                'success' => $request->session()->get('success'),
                'error' => $request->session()->get('error'),
            ],
            'last_synced_at' => CtsSyncLog::latest()->first()?->synced_at,
            'app_name' => config('app.name'),
            'macroSettings' => [
                'name' => Setting::get('macro_name'),
                'uuid' => Setting::get('macro_uuid'),
                'collectionName' => Setting::get('macro_collection_name', '--MAIN--'),
                'collectionUuid' => Setting::get('macro_collection_uuid', '8D02FC57-83F8-4042-9B90-81C229728426'),
            ],
            'namenseinblenderMacro' => [
                'name' => Setting::get('namenseinblender_macro_name'),
                'uuid' => Setting::get('namenseinblender_macro_uuid'),
                'collection_name' => Setting::get('namenseinblender_macro_collection_name', '--MAIN--'),
                'collection_uuid' => Setting::get('namenseinblender_macro_collection_uuid'),
            ],
            'currentKeyVisual' => Setting::get('current_key_visual'),
            'currentBackground' => Setting::get('current_background'),
        ];
    }
}
