<x-layouts.app title="Dashboard">
    @php
        $user = auth()->user();
        $tenant = $user->tenant;
        $aiSetting = $tenant->aiSetting;
        $demoConversation = \App\Domains\Conversations\Models\Conversation::query()
            ->with('contact')
            ->where('tenant_id', $tenant->getKey())
            ->latest('last_message_at')
            ->first();

        $dashboardData = [
            'companyName' => $tenant->company_name,
            'fullName' => $user->full_name,
            'plan' => optional($tenant->plan)->name ?? 'Starter',
            'tenantStatus' => ucfirst($tenant->status),
            'branchesCount' => $tenant->branches()->count(),
            'usersCount' => $tenant->users()->count(),
            'aiEnabled' => (bool) optional($aiSetting)->ai_enabled,
            'aiFeatures' => array_values(optional($aiSetting)->allowed_features ?? []),
            'aiInteractionsCount' => \App\Domains\AI\Models\AiInteraction::query()
                ->where('tenant_id', $tenant->getKey())
                ->count(),
            'aiKnowledgeCount' => \App\Domains\AI\Models\AiKnowledgeSnippet::query()
                ->where('tenant_id', $tenant->getKey())
                ->where('status', 'active')
                ->count(),
            'dashboardEndpoint' => url('/api/reports/dashboard'),
            'onboardingCompleted' => ! is_null($tenant->onboarding_completed_at),
            'aiEndpoint' => $demoConversation
                ? url('/api/ai/conversations/'.$demoConversation->getKey().'/suggest-reply')
                : null,
            'aiDemoConversationLabel' => $demoConversation?->contact?->primary_name ?? null,
            'copilotEndpoint' => url('/api/ai/copilot/overview'),
            'inboxUrl' => route('inbox'),
            'ordersUrl' => route('orders'),
            'pipelineUrl' => route('pipeline'),
            'messagesEndpointBase' => url('/api/conversations'),
            'leadsEndpointBase' => url('/api/leads'),
            'quotesEndpoint' => url('/api/quotes'),
            'createOrderEndpoint' => url('/api/orders'),
            'canReplyConversations' => $user->can('conversations.reply'),
            'canMoveLeads' => $user->can('leads.move_stage'),
            'canManageQuotes' => $user->can('quotes.manage'),
            'canManageOrders' => $user->can('orders.manage'),
            'csrfToken' => csrf_token(),
        ];
    @endphp

    <div
        id="dashboard-app"
        data-dashboard='@json($dashboardData)'
    ></div>
</x-layouts.app>
