<?php

namespace App\Http\Middleware;

use App\Models\User;
use Inertia\Middleware;
use Illuminate\Http\Request;
use App\Domain\Subscriptions\Service\SubscriptionService;
use Illuminate\Database\Eloquent\Builder;

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  \Illuminate\Http\Request  $request
     * @return string|null
     */
    public function version(Request $request)
    {
        return parent::version($request);
    }

    /**
     * Defines the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function share(Request $request)
    {
        $with_sidebar = 0;

        if ($request->routeIs('*.layoutsidebar.*') || $request->routeIs('feeds.layoutsidebar')) {
            $with_sidebar = 1;
        }

        return array_merge(parent::share($request), [
            'auth' => function () use ($request) {
                return [
                    'user' => $request->user() ? [
                        'id' => $request->user()->id,
                        'name' => $request->user()->name,
                        'username' => $request->user()->username,
                        'first_name' => $request->user()->first_name,
                        'last_name' => $request->user()->last_name,
                        'email' => $request->user()->email,
                        'user_char' => $request->user()->user_char,
                        'color' => $request->user()->color,
                        'photo_path' => $request->user()->photo_path,
                        'private' => $request->user()->private,
                        'banner_path' => $request->user()->banner_path,
                    ] : null,
                ];
            },
            'balance' => function () use ($request) {
                if($request->user()){
                    return SubscriptionService::calculate($request->user()->id);
                }
                return 0;
            },
            'leaders' => function () use ($with_sidebar) {
                if( $with_sidebar ){
                    $leaders = collect();
                    if(nova_get_setting('vote_paid_mode')){
                        foreach (SubscriptionService::leaders() as $leader) {
                            if($leader->vote_count){
                                $lUser = User::find($leader->user_id);
                                $lUser->name = $lUser->name;
                                $lUser->countVote = $leader->vote_count;
                                $leaders[] = $lUser;
                            }
                        }
                    }else{
                        $leaders =  SubscriptionService::freeLeaders();
                    }
                    return $leaders;
                }
            },
            'is_notify' => function () use ($request) {
                if($request->user()){
                    return $request->user()->unreadNotifications()->count();
                }
                return 0;
            },
            'message_reading_count' => function () use ($request) {
                if($request->user()){
                    return $request->user()->chat_rooms()->whereHas('latestMessage', function (Builder $query) use($request) {
                        $query->where('is_reading', false)->where('user_id', '<>', $request->user()->id);
                    })->count();
                }
                return 0;
            },
            'sidebar_layout' => $with_sidebar,
            'paid_mode' => function () {
                return nova_get_setting('vote_paid_mode');
            },
            'flash' => function () use ($request) {
                return [
                    'success' => $request->session()->get('success'),
                    'error' => $request->session()->get('error'),
                    'status' => $request->session()->get('status'),
                ];
            },
        ]);
    }
}
