<?php

namespace App\Http\Middleware;

use App\Enums\ForexImportStagingStatus;
use App\Models\ForexImportStagingItem;
use App\Models\InvestmentSetting;
use App\Support\Impersonation;
use App\Support\MobileUserAgent;
use Illuminate\Http\Request;
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
     */
    public function version(Request $request): ?string
    {
        return parent::version($request);
    }

    /**
     * Define the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     *
     * @return array<string, mixed>
     */
    public function share(Request $request): array
    {
        return [
            ...parent::share($request),
            'name' => config('app.name'),
            'auth' => [
                'user' => $request->user(),
            ],
            'isMobile' => MobileUserAgent::matches($request),
            'sidebarOpen' => $request->hasCookie('sidebar_state')
                ? $request->cookie('sidebar_state') === 'true'
                : ! MobileUserAgent::matches($request),
            'adminNav' => static function () use ($request): array {
                $user = $request->user();
                if ($user === null || ! $user->is_admin) {
                    return [
                        'forex_import_pending' => false,
                        'adjustments_enabled' => false,
                    ];
                }

                $hasPendingForexImport = ForexImportStagingItem::query()
                    ->where('status', ForexImportStagingStatus::Pending)
                    ->exists();

                return [
                    'forex_import_pending' => $hasPendingForexImport,
                    'adjustments_enabled' => (bool) config('app.allow_adjustments'),
                ];
            },
            'impersonation' => static function () use ($request): array {
                if (! Impersonation::isActive($request)) {
                    return [
                        'active' => false,
                        'readonly' => false,
                        'impersonator' => null,
                    ];
                }

                $impersonator = Impersonation::impersonator($request);
                $user = $request->user();

                return [
                    'active' => true,
                    'readonly' => Impersonation::isReadOnly($request),
                    'impersonator' => $impersonator !== null
                        ? [
                            'id' => $impersonator->id,
                            'name' => $impersonator->name,
                            'email' => $impersonator->email,
                        ]
                        : null,
                    'user' => $user !== null
                        ? [
                            'id' => $user->id,
                            'name' => $user->name,
                            'email' => $user->email,
                        ]
                        : null,
                ];
            },
            'kyc' => static function () use ($request): array {
                if ($request->user() === null) {
                    return [
                        'investor_portal_unlocked' => false,
                        'status_value' => null,
                        'rejection_reason' => null,
                        'require_kyc_for_invest' => true,
                        'require_kyc_for_withdraw' => true,
                    ];
                }

                $gates = InvestmentSetting::current();

                return [
                    'investor_portal_unlocked' => $request->user()->investorPortalUnlocked(),
                    'status_value' => $request->user()->kyc_status->value,
                    'rejection_reason' => $request->user()->kyc_rejection_reason,
                    'require_kyc_for_invest' => $gates->require_kyc_to_invest,
                    'require_kyc_for_withdraw' => $gates->require_kyc_to_withdraw,
                ];
            },
        ];
    }
}
