<?php

namespace App\Http\Middleware;

use App\Models\Configuration;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
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
    {
        $companyName = Configuration::where('settings', 'company_name')->value('value') ?? 'SCA PANEL';
        $companyLogoPath = Configuration::where('settings', 'company_logo')->value('value');
        $companyLogo = $companyLogoPath ? Storage::disk('public')->url($companyLogoPath) : null;

        return [
            ...parent::share($request),
            'auth' => [
                'user' => $request->user(),
            ],
            'appSettings' => [
                'companyName' => $companyName,
                'companyLogo' => $companyLogo,
            ],
            'flash' => [
                'success' => $request->session()->get('success'),
                'error' => $request->session()->get('error'),
            ],
        ];
    }
}
