<?php

namespace App\Http\Middleware;

use App\Models\Language;
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
     *
     * @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): array
    {
        $localeFields = ['locale_name', 'iso_code', 'name', 'localized_name'];
        $currentLocale = $request->session()->get('locale', null);
        if (is_null($currentLocale)) {
            $currentLocale = Language::where(['locale_name' => 'en', 'iso_code' => 'en_US'])->get($localeFields)[0]->toArray();
            $request->session()->put('locale', [
                'locale_name'    => $currentLocale['locale_name'],
                'iso_code'       => $currentLocale['iso_code'],
                'name'           => $currentLocale['name'],
                'localized_name' => $currentLocale['localized_name'],
            ]);
        }
        $localeFilePath = base_path("lang/{$currentLocale['locale_name']}.json");

        $notifications = [];
        $notificationsCount = count($notifications);
        $unreadNotifications = false;
        if (! is_null($request->user())) {
            $notifications = $request->user()->notifications;
            $notificationsCount = count($notifications);
            for ($i = 0; $i < $notificationsCount; $i++) {
                $newData = $notifications[$i]->data;
                $createdAt = carbon($notifications[$i]->created_at);
                $dateFormat = 'F j';
                if (!$createdAt->is(gmdate('Y'))) {
                    $dateFormat = 'F j, Y';
                }
                $newData['created_at_date'] = $createdAt->copy()->format($dateFormat);
                $newData['created_at_time'] = $createdAt->copy()->format('H:i');
                $notifications[$i]->data = $newData;
            }
            if (count($request->user()->unreadNotifications) > 0) {
                $unreadNotifications = true;
            }
        }

        $additionalData = [
            'appName' => config('app.name'),

            'availableLocales' => Language::get($localeFields),
            'currentLocale'    => $currentLocale,
            'language'         => translations($localeFilePath),

            'notifications'       => $notifications,
            'unreadNotifications' => $unreadNotifications,
        ];

        if ($request->user()->email === env('ADMIN_EMAIL')) {
            $additionalData['is_admin_user'] = true;
        }

        return array_merge(parent::share($request), $additionalData);
    }
}
