<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;
use Inertia\Middleware;

// ══════════════════════════════════════════════════════════════════
//  HandleInertiaRequests
//
//  This file controls what data is shared with EVERY Vue page
//  automatically — without needing to pass it manually each time.
//
//  Think of it as global props available in every Vue component.
//
//  What we share:
//    auth.user  → the logged-in user's safe public data
//                 (never password, never raw email unless needed)
//    flash      → success and error messages from redirects
//    locale     → current language (en or ar) for Vue i18n
// ══════════════════════════════════════════════════════════════════

class HandleInertiaRequests extends Middleware
{
    // The root Blade view that loads the Vue app
    protected $rootView = 'app';

    // Asset versioning for cache busting
    public function version(Request $request): ?string
    {
        return parent::version($request);
    }

    // Shared props — available in every Vue page automatically
    public function share(Request $request): array
    {
        return [
            ...parent::share($request),

            // ── Authenticated user ─────────────────────────────
            // Only safe fields — no password, no tokens
            // Email is included here because the logged-in user
            // needs to see their own email in their profile
            'auth' => [
                'user' => $request->user() ? [
                    'id'              => $request->user()->id,
                    'name'            => $request->user()->name,
                    'nickname'        => $request->user()->nickname,
                    'email'           => $request->user()->email,
                    'role'            => $request->user()->role,
                    'profession'      => $request->user()->profession,
                    'experience_level'=> $request->user()->experience_level,
                    'avatar'          => $request->user()->avatar,
                    'theme'           => $request->user()->theme,           // ← ADD
                    'locale'          => $request->user()->language,        // ← ADD (maps DB column → JS key)
                    'language'        => $request->user()->language,   
                    'show_real_name'  => $request->user()->show_real_name,
                    'is_active'       => $request->user()->is_active,
                    'notify_jobs'     => $request->user()->notify_jobs,
                    'notify_freelance'=> $request->user()->notify_freelance,
                    'notify_forum'    => $request->user()->notify_forum,
                    'notify_surveys'  => $request->user()->notify_surveys,
                    'notify_documents'=> $request->user()->notify_documents,
                ] : null,
            ],

            // ── Flash messages ─────────────────────────────────
            // Passed from controllers via redirect()->with('success', ...)
            'flash' => [
                'success' => $request->session()->get('success'),
                'error'   => $request->session()->get('error'),
                'warning' => $request->session()->get('warning'),
                'info'    => $request->session()->get('info'),
            ],

            // ── Current locale ─────────────────────────────────
            'locale' => app()->getLocale(),

            // ── Auth translations (for guest auth pages) ────────
            'translations' => [
                'auth' => Lang::get('auth'),
            ],
        ];
    }
}