<x-layouts.app title="AI Settings">
    @php
        $user = auth()->user();
        $tenant = $user->tenant;
        $settings = $tenant->aiSetting()->firstOrCreate(
            ['tenant_id' => $tenant->getKey()],
            [
                'ai_enabled' => true,
                'allowed_features' => config('ai.feature_defaults'),
                'default_language' => 'en',
                'tone_style' => 'professional',
                'human_approval_required' => true,
            ]
        );

        $aiSettingsData = [
            'companyName' => $tenant->company_name,
            'csrfToken' => csrf_token(),
            'endpoints' => [
                'settings' => url('/api/settings/ai'),
                'snippets' => url('/api/settings/ai/knowledge-snippets'),
            ],
            'settings' => [
                'ai_enabled' => (bool) $settings->ai_enabled,
                'allowed_features' => array_values($settings->allowed_features ?? []),
                'default_language' => $settings->default_language,
                'tone_style' => $settings->tone_style,
                'max_daily_ai_requests' => $settings->max_daily_ai_requests,
                'human_approval_required' => (bool) $settings->human_approval_required,
                'ai_data_retention_days' => $settings->ai_data_retention_days,
                'model_override' => $settings->model_override,
            ],
            'snippets' => \App\Domains\AI\Models\AiKnowledgeSnippet::query()
                ->where('tenant_id', $tenant->getKey())
                ->latest()
                ->get()
                ->map(fn ($snippet) => [
                    'id' => $snippet->id,
                    'type' => $snippet->type,
                    'title' => $snippet->title,
                    'content_text' => $snippet->content_text,
                    'language_code' => $snippet->language_code,
                    'status' => $snippet->status,
                ])->all(),
        ];
    @endphp

    <div id="ai-settings-app" data-ai-settings='@json($aiSettingsData)'></div>
</x-layouts.app>
