<?php

namespace App\Http\Middleware;

use App\Models\Configuration;
use App\Models\exchangeRate;
use App\Models\MuhandisinsProfit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
    /**
     * The root template that's loaded on the first page visit.
     *
     * @see https://inertiajs.com/server-side-setup#root-template
     * @var string
     */
    protected $rootView = 'app';

    /**
     * Determines the current asset version.
     *
     * @see https://inertiajs.com/asset-versioning
     * @param Request $request
     * @return string|null
     */
    public function version(Request $request): ?string
    {
        return parent::version($request);
    }

    /**
     * Defines the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     * @param Request $request
     * @return array
     */
    public function share(Request $request): array
    {
        return array_merge(parent::share($request), [
            'flash' => [
                'message' => fn() => $request->session()->get('message'),
                'success' => fn() => $request->session()->get('success'),
                'error' => fn() => $request->session()->get('error'),
                'status' => fn() => $request->session()->get('status'),
            ],
            'current_user' => fn() => Auth::user()
                ? Auth::user()->only('id', 'name', 'email', 'email_verified_at', 'created_at', 'updated_at', 'is_engineer', 'is_firm', 'profile_photo_url', 'profile_photo_path', 'tag_line', 'wallet', 'allNotifications', 'unreadMessageCount')
                : null,
            'pusher' => [
                'key' => env('PUSHER_APP_KEY'),
            ],
            'stripe' => [
                'key' => (string)env(env('STRIPE_MODE')==='PROD'? 'STRIPE_KEY_PROD': 'STRIPE_KEY'),
            ],
            'algolia' => [
                'id' => env('ALGOLIA_APP_ID'),
                'key' => env('ALGOLIA_CLIENT_SECRET'),
            ],
            'exchange_rates' => exchangeRate::formattedRates(),
            'return_data' => fn() => $request->session()->get('return_data'),
            'minimum_withdrawal_amount' => fn() => Configuration::where('key', 'minimum_withdrawal_amount')->first()->value,
            'muhandisin_cut_tiers' => fn() => MuhandisinsProfit::all(),
        ]);
    }
}
