File size: 28,745 Bytes
9cf8f52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e3f82a
 
9cf8f52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import React, { useState } from 'react';
import { motion } from 'motion/react';
import { 
  getEnvStatus, 
  testSupabaseConnection, 
  testFirebaseConnection 
} from '../utils/envValidator';
import { encryptWithWebCrypto, decryptWithWebCrypto } from '../utils/encryption';
import { proxyFetch } from '../utils/apiProxy';
import { 
  Activity, ShieldCheck, Database, Key, CheckCircle, 
  XCircle, AlertTriangle, RefreshCw, Download, Play, 
  Lock, BrainCircuit, Terminal, Sparkles, HelpCircle 
} from 'lucide-react';

interface TestResult {
  name: string;
  category: 'database' | 'security' | 'intelligence';
  status: 'SUCCESS' | 'ERROR' | 'PENDING';
  message: string;
  latency?: number;
}

export default function ConnectionTest({ lang = 'ar' }: { lang?: 'ar' | 'en' }) {
  const [runningAll, setRunningAll] = useState(false);
  const [envStatus, setEnvStatus] = useState(() => getEnvStatus());
  const [tests, setTests] = useState<Record<string, TestResult>>({
    supabase: { name: 'Supabase Database', category: 'database', status: 'PENDING', message: lang === 'ar' ? 'بانتظار إجراء التحقق...' : 'Awaiting manual trigger...' },
    firebase: { name: 'Firebase Firestore', category: 'database', status: 'PENDING', message: lang === 'ar' ? 'بانتظار إجراء التحقق...' : 'Awaiting manual trigger...' },
    vault: { name: 'Secure Vault Encrypt', category: 'security', status: 'PENDING', message: lang === 'ar' ? 'بانتظار إجراء التحقق...' : 'Awaiting manual trigger...' },
    gemini: { name: 'Google Gemini API', category: 'intelligence', status: 'PENDING', message: lang === 'ar' ? 'بانتظار إجراء التحقق...' : 'Awaiting manual trigger...' },
    groq: { name: 'Groq Cloud LLM API', category: 'intelligence', status: 'PENDING', message: lang === 'ar' ? 'بانتظار إجراء التحقق...' : 'Awaiting manual trigger...' }
  });

  const [lastTested, setLastTested] = useState<string | null>(null);

  const refreshEnvStatus = () => {
    setEnvStatus(getEnvStatus());
  };

  const getEnvValue = (name: string): string => {
    const altName = name.replace('VITE_', '');
    return (window as any).__ENV?.[name] || (window as any).__ENV?.[altName] || (window as any).env?.[name] || (window as any).env?.[altName] || (import.meta as any).env?.[name] || (import.meta as any).env?.[altName] || '';
  };

  const runSupabaseTest = async (): Promise<TestResult> => {
    setTests(prev => ({
      ...prev,
      supabase: { ...prev.supabase, status: 'PENDING', message: lang === 'ar' ? 'جاري الاتصال والتحقق من الاستجابة...' : 'Pinging database endpoint...' }
    }));
    
    const res = await testSupabaseConnection();
    const result: TestResult = {
      name: 'Supabase Database',
      category: 'database',
      status: res.status === 'SUCCESS' ? 'SUCCESS' : 'ERROR',
      message: res.message,
      latency: res.latencyMs
    };

    setTests(prev => ({ ...prev, supabase: result }));
    return result;
  };

  const runFirebaseTest = async (): Promise<TestResult> => {
    setTests(prev => ({
      ...prev,
      firebase: { ...prev.firebase, status: 'PENDING', message: lang === 'ar' ? 'جاري طلب استعلام خادم Firestore...' : 'Pinging Firebase endpoint...' }
    }));

    const res = await testFirebaseConnection();
    const result: TestResult = {
      name: 'Firebase Firestore',
      category: 'database',
      status: res.status === 'SUCCESS' ? 'SUCCESS' : 'ERROR',
      message: res.message,
      latency: res.latencyMs
    };

    setTests(prev => ({ ...prev, firebase: result }));
    return result;
  };

  const runVaultTest = async (): Promise<TestResult> => {
    setTests(prev => ({
      ...prev,
      vault: { ...prev.vault, status: 'PENDING', message: lang === 'ar' ? 'جاري توليد مفتاح AES-GCM 256...' : 'Generating AES-GCM 256 crypt...' }
    }));

    const start = Date.now();
    try {
      const originalText = "Eissa-Master-Sentry-Verification-2026";
      const dummyPass = "Eissa2026";
      
      // 1. Test Encryption
      const encrypted = await encryptWithWebCrypto(originalText, dummyPass);
      if (!encrypted || !encrypted.includes(':')) {
        throw new Error('فشل تشفير مفاتيح الخزنة.');
      }

      // 2. Test Decryption
      const decrypted = await decryptWithWebCrypto(encrypted, dummyPass);
      if (decrypted !== originalText) {
        throw new Error('فشل فك التشفير: البيانات الناتجة مشوهة.');
      }

      const result: TestResult = {
        name: 'Secure Vault Encrypt',
        category: 'security',
        status: 'SUCCESS',
        message: lang === 'ar' 
          ? 'تشفير الخزنة يعمل بشكل ممتاز مع معيار AES-GCM 256.' 
          : 'Vault dynamic cryptography fully operational (AES-GCM 256).',
        latency: Date.now() - start
      };
      setTests(prev => ({ ...prev, vault: result }));
      return result;
    } catch (err: any) {
      const result: TestResult = {
        name: 'Secure Vault Encrypt',
        category: 'security',
        status: 'ERROR',
        message: lang === 'ar' 
          ? `فشل فحص برمجية الخزنة: ${err.message || String(err)}`
          : `Vault cryptography error: ${err.message || String(err)}`,
        latency: Date.now() - start
      };
      setTests(prev => ({ ...prev, vault: result }));
      return result;
    }
  };

  const runGeminiTest = async (): Promise<TestResult> => {
    setTests(prev => ({
      ...prev,
      gemini: { ...prev.gemini, status: 'PENDING', message: lang === 'ar' ? 'جاري فحص ترخيص محرك Gemini...' : 'Pinging Google Gemini API...' }
    }));

    const start = Date.now();
    const token = getEnvValue('VITE_GEMINI_API_KEY');
    if (!token) {
      const result: TestResult = {
        name: 'Google Gemini API',
        category: 'intelligence',
        status: 'ERROR',
        message: lang === 'ar' ? 'مفتاح Gemini مفقود! يرجى إعداده كمتغير بيئي.' : 'Gemini key is missing in active workspace variables.'
      };
      setTests(prev => ({ ...prev, gemini: result }));
      return result;
    }

    try {
      // Small real call to gather metadata (highly safe & CORS proxy resolved)
      const url = `https://generativelanguage.googleapis.com/v1beta/models?key=${token}`;
      const res = await proxyFetch(url);
      
      const latency = Date.now() - start;
      if (res.status === 200) {
        const result: TestResult = {
          name: 'Google Gemini API',
          category: 'intelligence',
          status: 'SUCCESS',
          message: lang === 'ar' ? 'تم مصادقة مفتاح Gemini بنجاح. القنوات ذكية ومفتوحة!' : 'Gemini validation resolved with code 200. Models online.',
          latency
        };
        setTests(prev => ({ ...prev, gemini: result }));
        return result;
      } else {
        const result: TestResult = {
          name: 'Google Gemini API',
          category: 'intelligence',
          status: 'ERROR',
          message: lang === 'ar' ? `رفضت Google المفتاح برمز (${res.status}).` : `Google gateway rejected token (HTTP status ${res.status}).`,
          latency
        };
        setTests(prev => ({ ...prev, gemini: result }));
        return result;
      }
    } catch (err: any) {
      const result: TestResult = {
        name: 'Google Gemini API',
        category: 'intelligence',
        status: 'ERROR',
        message: lang === 'ar' ? `فشل الاتصال: ${err.message || 'خطأ في جدار الحماية'}` : `Fetch error: ${err.message || 'CORS or offline'}`,
        latency: Date.now() - start
      };
      setTests(prev => ({ ...prev, gemini: result }));
      return result;
    }
  };

  const runGroqTest = async (): Promise<TestResult> => {
    setTests(prev => ({
      ...prev,
      groq: { ...prev.groq, status: 'PENDING', message: lang === 'ar' ? 'جاري فحص ترخيص خادم Groq السحابي...' : 'Pinging Groq Cloud API...' }
    }));

    const start = Date.now();
    const token = getEnvValue('VITE_GROQ_API_KEY');
    if (!token) {
      const result: TestResult = {
        name: 'Groq Cloud LLM API',
        category: 'intelligence',
        status: 'ERROR',
        message: lang === 'ar' ? 'المفتاح لم يتم تعيينه. يرجى إدخال VITE_GROQ_API_KEY.' : 'Groq API Key is not set.'
      };
      setTests(prev => ({ ...prev, groq: result }));
      return result;
    }

    try {
      // Probe Models API (lightweight endpoint check)
      const res = await proxyFetch('https://api.groq.com/openai/v1/models', {
        headers: { 'Authorization': `Bearer ${token}` }
      });

      const latency = Date.now() - start;
      if (res.status === 200) {
        const result: TestResult = {
          name: 'Groq Cloud LLM API',
          category: 'intelligence',
          status: 'SUCCESS',
          message: lang === 'ar' ? 'الاتصال بـ Groq نشط ومصرح للاستدعاء بنجاح.' : 'Groq gateway authentication successful. High-speed LLM online.',
          latency
        };
        setTests(prev => ({ ...prev, groq: result }));
        return result;
      } else {
        const result: TestResult = {
          name: 'Groq Cloud LLM API',
          category: 'intelligence',
          status: 'ERROR',
          message: lang === 'ar' ? `مفاتيح غير مصرحة في خادم Groq (${res.status}).` : `Groq rejected the authorization (HTTP status ${res.status}).`,
          latency
        };
        setTests(prev => ({ ...prev, groq: result }));
        return result;
      }
    } catch (err: any) {
      const result: TestResult = {
        name: 'Groq Cloud LLM API',
        category: 'intelligence',
        status: 'ERROR',
        message: lang === 'ar' ? `فشل المصافحة: CORS أو خادم غير متصل بالإنترنت.` : `Handshake exception: CORS restriction or destination model server is offline.`,
        latency: Date.now() - start
      };
      setTests(prev => ({ ...prev, groq: result }));
      return result;
    }
  };

  const runAllDiagnostics = async () => {
    setRunningAll(true);
    refreshEnvStatus();

    await runSupabaseTest();
    await runFirebaseTest();
    await runVaultTest();
    await runGeminiTest();
    await runGroqTest();

    setLastTested(new Date().toLocaleTimeString());
    setRunningAll(false);
  };

  const exportJSONResults = () => {
    const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(
      JSON.stringify({
        timestamp: new Date().toISOString(),
        environmentVariables: envStatus,
        connectionTests: tests
      }, null, 2)
    );
    const downloadAnchor = document.createElement('a');
    downloadAnchor.setAttribute("href", dataStr);
    downloadAnchor.setAttribute("download", `Sentry_Diagnostic_Report_${Date.now()}.json`);
    document.body.appendChild(downloadAnchor);
    downloadAnchor.click();
    downloadAnchor.remove();
  };

  return (
    <div className="w-full space-y-6" dir={lang === 'ar' ? 'rtl' : 'ltr'}>
      
      {/* Test Controls Banner */}
      <div className="relative overflow-hidden rounded-3xl bg-slate-900/60 border border-slate-800/80 p-6 backdrop-blur-xl shadow-2xl flex flex-col md:flex-row items-center justify-between gap-4">
        {/* Colorful Gradient Light effect */}
        <div className="absolute top-0 right-0 h-[120px] w-[200px] bg-gradient-to-tr from-cyan-400 via-indigo-600 to-purple-500 rounded-full blur-[90px] opacity-25 pointer-events-none" />
        
        <div className="space-y-1.5 z-10 text-center md:text-right">
          <h2 className="text-xl font-black bg-gradient-to-r from-red-200 via-indigo-200 to-cyan-200 bg-clip-text text-transparent flex items-center justify-center md:justify-start gap-2">
            <Activity className="h-5 w-5 text-cyan-400 animate-pulse" />
            <span>{lang === 'ar' ? 'لوحة الاختبارات والتشخيص الذاتي' : 'Sentry AI Active Diagnostic Console'}</span>
          </h2>
          <p className="text-xs text-slate-400 max-w-xl">
            {lang === 'ar' 
              ? 'قم بمراقبة وفحص اتصالات قواعد البيانات السحابية، وسلامة تراخيص ذكاء قوين الاصطناعي، ومزامنة المتغيرات البيئية من مكان واحد.' 
              : 'Execute end-to-end cloud database ping tests, verify cryptographic master key vaults, and inspect active Gemini and Groq API handshakes.'}
          </p>
          {lastTested && (
            <p className="text-[10px] font-mono text-cyan-500 font-semibold uppercase">
              {lang === 'ar' ? `آخر فحص نشط: ${lastTested}` : `Last telemetry probe: ${lastTested}`}
            </p>
          )}
        </div>

        <div className="flex flex-wrap gap-2.5 z-10 w-full md:w-auto justify-center">
          <button
            id="btn-run-all-diagnostics"
            type="button"
            onClick={runAllDiagnostics}
            disabled={runningAll}
            className="flex items-center gap-2 px-5 py-3 rounded-2xl bg-gradient-to-r from-indigo-500 via-purple-600 to-cyan-500 hover:scale-103 font-bold text-xs text-white transition shadow-lg shadow-indigo-600/20 active:scale-97 disabled:opacity-50"
          >
            <RefreshCw className={`h-4 w-4 ${runningAll ? 'animate-spin' : ''}`} />
            <span>{runningAll ? (lang === 'ar' ? 'جاري الفحص...' : 'Probing...') : (lang === 'ar' ? 'إجراء فحص شامل للقنوات' : 'Run Full Diagnostics')}</span>
          </button>

          <button
            id="btn-export-reports"
            type="button"
            onClick={exportJSONResults}
            className="flex items-center gap-1.5 px-4  py-3 rounded-2xl bg-slate-800 border border-slate-700 font-bold text-xs text-slate-200 hover:bg-slate-700/80 active:scale-97 transition"
          >
            <Download className="h-4 w-4 text-cyan-400" />
            <span>{lang === 'ar' ? 'تصدير كملف JSON' : 'Export Report'}</span>
          </button>
        </div>
      </div>

      {/* Main Grid: Variables list & Connection status */}
      <div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
        
        {/* Box 1: Env vars audit (Masqued panel) */}
        <div className="lg:col-span-5 bg-slate-900/40 border border-slate-800/80 rounded-3xl p-5 backdrop-blur-md space-y-4">
          <div className="flex items-center justify-between border-b border-slate-800/60 pb-3">
            <h3 className="text-sm font-black text-slate-200 flex items-center gap-1.5">
              <Key className="h-4 w-4 text-purple-400" />
              <span>{lang === 'ar' ? 'التحقق من متغيرات البيئة' : 'Active Keys & Environment Variables'}</span>
            </h3>
            <button
              onClick={refreshEnvStatus}
              className="text-xs text-indigo-400 hover:text-indigo-300 font-bold"
            >
              {lang === 'ar' ? 'تحديث' : 'Refresh'}
            </button>
          </div>

          <p className="text-[11px] text-slate-400 leading-relaxed">
            {lang === 'ar' 
              ? 'تُسحب هذه المفاتيح تلقائياً ومباشرة من إعدادات Hugging Face Secrets لضمان حماية خادم العميل القصوى وعدم كشف الرموز للمتصفح.'
              : 'Environment variables are loaded securely from your hosting Secrets context. Values are masked to protect authorization endpoints.'}
          </p>

          <div className="space-y-2.5">
            {envStatus.map((env, idx) => (
              <div 
                key={idx} 
                className="flex items-center justify-between p-3 rounded-xl bg-slate-950/50 border border-slate-800/50 hover:bg-slate-950/80 transition duration-200"
              >
                <div className="space-y-0.5">
                  <div className="flex items-center gap-1.5">
                    <span className="text-[11px] font-black font-mono text-slate-300">{env.keyName}</span>
                    {env.isOptional && (
                      <span className="text-[8px] bg-slate-800 text-slate-400 px-1 py-0.2 rounded font-mono font-bold">OPTIONAL</span>
                    )}
                  </div>
                  <span className="text-[10px] text-slate-500 font-mono tracking-wider">{env.maskedValue}</span>
                </div>

                <div className="flex items-center gap-1.5">
                  {env.isLoaded ? (
                    <span className="flex items-center gap-1 text-[10px] text-emerald-400 font-bold uppercase bg-emerald-500/5 border border-emerald-500/10 px-2 py-0.5 rounded-lg">
                      <CheckCircle className="h-3 w-3" />
                      <span>{lang === 'ar' ? 'جاهز' : 'Loaded'}</span>
                    </span>
                  ) : (
                    <span className={`flex items-center gap-1 text-[10px] font-bold uppercase px-2 py-0.5 rounded-lg ${
                      env.isOptional ? 'text-yellow-400 bg-yellow-500/5 border border-yellow-500/10' : 'text-rose-400 bg-rose-500/5 border border-rose-500/10'
                    }`}>
                      <AlertTriangle className="h-3 w-3 animate-pulse" />
                      <span>{lang === 'ar' ? 'غير متوفر' : 'Missing'}</span>
                    </span>
                  )}
                </div>
              </div>
            ))}
          </div>
        </div>

        {/* Box 2: Test Suites and Latency logs */}
        <div className="lg:col-span-7 bg-slate-900/40 border border-slate-800/80 rounded-3xl p-5 backdrop-blur-md space-y-4">
          <div className="flex items-center justify-between border-b border-slate-800/60 pb-3">
            <h3 className="text-sm font-black text-slate-200 flex items-center gap-1.5">
              <Terminal className="h-4 w-4 text-cyan-400" />
              <span>{lang === 'ar' ? 'فحوصات الاتصال السحابية النشطة' : 'Active Connection Guard Tests'}</span>
            </h3>
            <span className="text-[10px] text-indigo-400 font-bold bg-indigo-500/10 border border-indigo-500/20 px-2 py-0.5 rounded-full uppercase tracking-widest font-mono">
              TELEMETRY_SUITE
            </span>
          </div>

          <div className="space-y-3.5">
            
            {/* Supabase Box */}
            <div className="p-4 rounded-2xl bg-slate-950/40 border border-slate-800/80 flex flex-col md:flex-row md:items-center justify-between gap-3 hover:border-slate-800 hover:bg-slate-950/60 transition">
              <div className="flex items-start gap-3">
                <div className="p-2.5 rounded-xl bg-emerald-500/10 border border-emerald-500/20 text-emerald-400">
                  <Database className="h-5 w-5" />
                </div>
                <div className="space-y-1">
                  <h4 className="text-xs font-black text-slate-200 flex items-center gap-2">
                    <span>{lang === 'ar' ? 'قاعدة بيانات Supabase الموحدة' : 'Supabase Multi-Tenant Engine'}</span>
                    {tests.supabase.latency !== undefined && (
                      <span className="text-[9px] font-mono font-black text-cyan-400 bg-cyan-950/50 px-1.5 py-0.2 rounded">
                        {tests.supabase.latency}ms
                      </span>
                    )}
                  </h4>
                  <p className="text-[11px] text-slate-400 leading-relaxed max-w-sm">{tests.supabase.message}</p>
                </div>
              </div>
              <div className="flex items-center gap-2 md:justify-end">
                <StatusMarker status={tests.supabase.status} lang={lang} />
                <button
                  type="button"
                  onClick={runSupabaseTest}
                  className="p-2 rounded-xl bg-slate-800/80 border border-slate-700 hover:bg-slate-700 text-slate-200 transition"
                  title="Run Single Probe"
                >
                  <Play className="h-3 w-3 text-cyan-400 fill-current" />
                </button>
              </div>
            </div>

            {/* Firebase Box */}
            <div className="p-4 rounded-2xl bg-slate-950/40 border border-slate-800/80 flex flex-col md:flex-row md:items-center justify-between gap-3 hover:border-slate-800 hover:bg-slate-950/60 transition">
              <div className="flex items-start gap-3">
                <div className="p-2.5 rounded-xl bg-orange-500/10 border border-orange-500/20 text-orange-400">
                  <Database className="h-5 w-5" />
                </div>
                <div className="space-y-1">
                  <h4 className="text-xs font-black text-slate-200 flex items-center gap-2">
                    <span>{lang === 'ar' ? 'جسر البيانات السحابي Firebase Auth' : 'Firebase Firestore & OAuth'}</span>
                    {tests.firebase.latency !== undefined && (
                      <span className="text-[9px] font-mono font-black text-cyan-400 bg-cyan-950/50 px-1.5 py-0.2 rounded">
                        {tests.firebase.latency}ms
                      </span>
                    )}
                  </h4>
                  <p className="text-[11px] text-slate-400 leading-relaxed max-w-sm">{tests.firebase.message}</p>
                </div>
              </div>
              <div className="flex items-center gap-2 md:justify-end">
                <StatusMarker status={tests.firebase.status} lang={lang} />
                <button
                  type="button"
                  onClick={runFirebaseTest}
                  className="p-2 rounded-xl bg-slate-800/80 border border-slate-700 hover:bg-slate-700 text-slate-200 transition"
                  title="Run Single Probe"
                >
                  <Play className="h-3 w-3 text-cyan-400 fill-current" />
                </button>
              </div>
            </div>

            {/* Vault Box */}
            <div className="p-4 rounded-2xl bg-slate-950/40 border border-slate-800/80 flex flex-col md:flex-row md:items-center justify-between gap-3 hover:border-slate-800 hover:bg-slate-950/60 transition">
              <div className="flex items-start gap-3">
                <div className="p-2.5 rounded-xl bg-purple-500/10 border border-purple-500/20 text-purple-400">
                  <Lock className="h-5 w-5" />
                </div>
                <div className="space-y-1">
                  <h4 className="text-xs font-black text-slate-200 flex items-center gap-2">
                    <span>{lang === 'ar' ? 'خزنة التشفير AES-GCM 256' : 'AES Cryptographic Web Crypto Vault'}</span>
                    {tests.vault.latency !== undefined && (
                      <span className="text-[9px] font-mono font-black text-cyan-400 bg-cyan-950/50 px-1.5 py-0.2 rounded">
                        {tests.vault.latency}ms
                      </span>
                    )}
                  </h4>
                  <p className="text-[11px] text-slate-400 leading-relaxed max-w-sm">{tests.vault.message}</p>
                </div>
              </div>
              <div className="flex items-center gap-2 md:justify-end">
                <StatusMarker status={tests.vault.status} lang={lang} />
                <button
                  type="button"
                  onClick={runVaultTest}
                  className="p-2 rounded-xl bg-slate-800/80 border border-slate-700 hover:bg-slate-700 text-slate-200 transition"
                  title="Run Single Probe"
                >
                  <Play className="h-3 w-3 text-cyan-400 fill-current" />
                </button>
              </div>
            </div>

            {/* Gemini Box */}
            <div className="p-4 rounded-2xl bg-slate-950/40 border border-slate-800/80 flex flex-col md:flex-row md:items-center justify-between gap-3 hover:border-slate-800 hover:bg-slate-950/60 transition">
              <div className="flex items-start gap-3">
                <div className="p-2.5 rounded-xl bg-purple-500/15 border border-purple-500/25 text-purple-400">
                  <BrainCircuit className="h-5 w-5 animate-pulse" />
                </div>
                <div className="space-y-1">
                  <h4 className="text-xs font-black text-slate-200 flex items-center gap-2">
                    <span>{lang === 'ar' ? 'محرك Google Gemini وقسائم الذكاء' : 'Google Gemini AI Endpoint'}</span>
                    {tests.gemini.latency !== undefined && (
                      <span className="text-[9px] font-mono font-black text-cyan-400 bg-cyan-950/50 px-1.5 py-0.2 rounded">
                        {tests.gemini.latency}ms
                      </span>
                    )}
                  </h4>
                  <p className="text-[11px] text-slate-400 leading-relaxed max-w-sm">{tests.gemini.message}</p>
                </div>
              </div>
              <div className="flex items-center gap-2 md:justify-end">
                <StatusMarker status={tests.gemini.status} lang={lang} />
                <button
                  type="button"
                  onClick={runGeminiTest}
                  className="p-2 rounded-xl bg-slate-800/80 border border-slate-700 hover:bg-slate-700 text-slate-200 transition"
                  title="Run Single Probe"
                >
                  <Play className="h-3 w-3 text-cyan-400 fill-current" />
                </button>
              </div>
            </div>

            {/* Groq Box */}
            <div className="p-4 rounded-2xl bg-slate-950/40 border border-slate-800/80 flex flex-col md:flex-row md:items-center justify-between gap-3 hover:border-slate-800 hover:bg-slate-950/60 transition">
              <div className="flex items-start gap-3">
                <div className="p-2.5 rounded-xl bg-cyan-500/10 border border-cyan-500/20 text-cyan-400">
                  <BrainCircuit className="h-5 w-5" />
                </div>
                <div className="space-y-1">
                  <h4 className="text-xs font-black text-slate-200 flex items-center gap-2">
                    <span>{lang === 'ar' ? 'بوابة Groq Cloud فائقة السرعة' : 'Groq Cloud High-Speed Gateway'}</span>
                    {tests.groq.latency !== undefined && (
                      <span className="text-[9px] font-mono font-black text-cyan-400 bg-cyan-950/50 px-1.5 py-0.2 rounded">
                        {tests.groq.latency}ms
                      </span>
                    )}
                  </h4>
                  <p className="text-[11px] text-slate-400 leading-relaxed max-w-sm">{tests.groq.message}</p>
                </div>
              </div>
              <div className="flex items-center gap-2 md:justify-end">
                <StatusMarker status={tests.groq.status} lang={lang} />
                <button
                  type="button"
                  onClick={runGroqTest}
                  className="p-2 rounded-xl bg-slate-800/80 border border-slate-700 hover:bg-slate-700 text-slate-200 transition"
                  title="Run Single Probe"
                >
                  <Play className="h-3 w-3 text-cyan-400 fill-current" />
                </button>
              </div>
            </div>

          </div>
        </div>

      </div>

    </div>
  );
}

function StatusMarker({ status, lang }: { status: 'SUCCESS' | 'ERROR' | 'PENDING', lang: 'ar' | 'en' }) {
  if (status === 'SUCCESS') {
    return (
      <span className="flex items-center gap-1 text-[10px] font-black uppercase text-emerald-400 bg-emerald-500/10 border border-emerald-500/20 px-2.5 py-1 rounded-xl">
        <CheckCircle className="h-3.5 w-3.5" />
        <span>{lang === 'ar' ? 'مستقر' : 'SUCCESS'}</span>
      </span>
    );
  }

  if (status === 'ERROR') {
    return (
      <span className="flex items-center gap-1 text-[10px] font-black uppercase text-rose-400 bg-rose-500/10 border border-rose-500/20 px-2.5 py-1 rounded-xl">
        <XCircle className="h-3.5 w-3.5 animate-pulse" />
        <span>{lang === 'ar' ? 'فشل' : 'FAILED'}</span>
      </span>
    );
  }

  return (
    <span className="flex items-center gap-1 text-[10px] font-black uppercase text-yellow-400 bg-yellow-500/10 border border-yellow-500/20 px-2.5 py-1 rounded-xl">
      <HelpCircle className="h-3.5 w-3.5" />
      <span>{lang === 'ar' ? 'بانتظار الفحص' : 'PENDING'}</span>
    </span>
  );
}