<?php

namespace App\Http\Middleware;

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
    {
        $user = $request->user();
        
        $tenant = tenancy()->tenant;
        $appName = $tenant ? ($tenant->name ?? 'Star ERP') : 'Star ERP 中央後台';
        
        // 分享給 Blade View (給 app.blade.php 使用)
        \Illuminate\Support\Facades\View::share('appName', $appName);

        return [
            ...parent::share($request),
            'appName' => $appName,
            'auth' => [
                'user' => $user ? [
                    'id' => $user->id,
                    'name' => $user->name,
                    'email' => $user->email,
                    'username' => $user->username ?? null,
                    // 權限資料
                    'roles' => $user->getRoleNames(),
                    'role_labels' => $user->roles->pluck('display_name'),
                    'permissions' => $user->getAllPermissions()->pluck('name')->toArray(),
                ] : null,
            ],
            'flash' => [
                'success' => $request->session()->get('success'),
                'error' => $request->session()->get('error'),
                'new_token' => $request->session()->get('new_token'),
            ],
            'branding' => function () {
                $tenant = tenancy()->tenant;
                
                // 決定名稱顯示邏輯
                $fullName = $tenant ? ($tenant->name ?? 'Star ERP') : 'Star ERP 中央後台';
                $shortName = $tenant ? ($tenant->short_name ?? $fullName) : 'Start ERP';

                $logoUrl = null;
                if ($tenant && isset($tenant->branding['logo_path'])) {
                    $logoUrl = \Storage::url($tenant->branding['logo_path']);
                } elseif (!$tenant) {
                    $logoUrl = \Storage::url('defaults/logo.png');
                }

                $brandingData = [
                    'name' => $fullName,
                    'short_name' => $shortName,
                    'logo_url' => $logoUrl,
                    'primary_color' => $tenant->branding['primary_color'] ?? ($tenant ? '#01ab83' : '#4F46E5'),
                    'text_color' => $tenant->branding['text_color'] ?? '#1a1a1a',
                ];

                // 同步分享給 Blade View (給 app.blade.php 使用 Favicon)
                \Illuminate\Support\Facades\View::share('branding', $brandingData);

                return $brandingData;
            },
        ];
    }
}
