File size: 57,746 Bytes
e3b5f50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
import React, { useState, useEffect } from 'react';
import { 
  User, 
  initAuth, 
  googleSignIn, 
  logout, 
  getAccessToken, 
  auth,
  db,
  handleFirestoreError,
  OperationType
} from '../firebase';
import { 
  listDriveFiles, 
  uploadLegalFileToDrive, 
  createPortfolioSheet, 
  sendLegalEmail, 
  createGmailDraft, 
  listCalendarEvents, 
  scheduleCourtEvent, 
  createGoogleDoc, 
  listChatSpaces, 
  postChatMessage,
  DriveFileItem,
  ChatSpaceItem,
  CourtCalendarEvent
} from '../services/googleWorkspaceService';
import { CourtSchedulingPanel } from './CourtSchedulingPanel';
import { 
  collection, 
  addDoc, 
  onSnapshot, 
  query, 
  where,
  orderBy, 
  limit, 
  serverTimestamp 
} from 'firebase/firestore';
import { 
  UNIFIED_DOCUMENT_METADATA, 
  CUSIP_PORTFOLIO_DATA, 
  ALGORITHM_SUITE, 
  EMAIL_PRESENTATION_PACKAGES,
  RULE_18_EXACTION_ITERATIONS
} from '../data/unifiedDocumentData';
import { 
  FolderGit2, 
  FileSpreadsheet, 
  Mail, 
  Calendar, 
  Video, 
  FileText, 
  MessageSquare, 
  Database, 
  ShieldCheck, 
  ExternalLink, 
  RefreshCw, 
  Send, 
  Plus, 
  AlertTriangle, 
  CheckCircle2, 
  Clock, 
  Check, 
  Sparkles, 
  Layers, 
  HardDrive,
  LogOut,
  UserCheck,
  Zap,
  Gavel
} from 'lucide-react';

interface ActivityLog {
  id: string;
  service: string;
  action: string;
  resourceTitle?: string;
  timestamp: any;
  status: string;
  uid: string;
}

export const GoogleWorkspaceHub: React.FC = () => {
  const [user, setUser] = useState<User | null>(null);
  const [token, setToken] = useState<string | null>(null);
  const [isLoggingIn, setIsLoggingIn] = useState<boolean>(false);
  const [activeWorkspaceTab, setActiveWorkspaceTab] = useState<'drive' | 'sheets' | 'gmail' | 'scheduling' | 'calendar' | 'docs' | 'chat'>('scheduling');

  // Loading and feedback states
  const [statusMessage, setStatusMessage] = useState<{ type: 'success' | 'error' | 'info'; text: string } | null>(null);
  const [isLoading, setIsLoading] = useState<boolean>(false);

  // Drive state
  const [driveFiles, setDriveFiles] = useState<DriveFileItem[]>([]);
  
  // Calendar state
  const [calendarEvents, setCalendarEvents] = useState<any[]>([]);

  // Chat state
  const [chatSpaces, setChatSpaces] = useState<ChatSpaceItem[]>([]);
  const [selectedChatSpace, setSelectedChatSpace] = useState<string>('');
  const [chatMessageText, setChatMessageText] = useState<string>('');

  // Firestore activity state
  const [activities, setActivities] = useState<ActivityLog[]>([]);

  // Confirmation Modal State (MANDATORY User Confirmation for Destructive/Mutating Operations)
  const [confirmModal, setConfirmModal] = useState<{
    isOpen: boolean;
    title: string;
    description: string;
    onConfirm: () => Promise<void>;
  }>({
    isOpen: false,
    title: '',
    description: '',
    onConfirm: async () => {}
  });

  // Initialize Auth
  useEffect(() => {
    const unsubscribe = initAuth(
      (currentUser, accessToken) => {
        setUser(currentUser);
        setToken(accessToken);
      },
      () => {
        setUser(null);
        setToken(null);
      }
    );
    return () => unsubscribe();
  }, []);

  // Listen to Firestore activity log
  useEffect(() => {
    if (!user) {
      setActivities(prev => (prev.length > 0 ? [] : prev));
      return;
    }

    try {
      const q = query(
        collection(db, 'workspace_activity'),
        where('uid', '==', user.uid),
        limit(20)
      );

      const unsubscribe = onSnapshot(
        q,
        (snapshot) => {
          const list: ActivityLog[] = [];
          snapshot.forEach((doc) => {
            const data = doc.data();
            list.push({
              id: doc.id,
              service: data.service,
              action: data.action,
              resourceTitle: data.resourceTitle,
              timestamp: data.timestamp,
              status: data.status,
              uid: data.uid
            });
          });
          if (list.length > 0) {
            setActivities(prev => {
              const map = new Map<string, ActivityLog>();
              list.forEach(item => map.set(item.id, item));
              prev.forEach(item => {
                if (!map.has(item.id)) map.set(item.id, item);
              });
              return Array.from(map.values()).slice(0, 50);
            });
          }
        },
        (error) => {
          console.warn('Firestore onSnapshot listener handled:', error);
        }
      );

      return () => unsubscribe();
    } catch (err) {
      console.warn('Error attaching Firestore listener:', err);
    }
  }, [user]);

  // Log activity to Firestore with resilient local fallback and graceful telemetry
  const logActivityToFirestore = async (service: string, action: string, resourceTitle?: string) => {
    if (!user) return;
    const nowIso = new Date().toISOString();
    const localEntry: ActivityLog = {
      id: `act_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
      service,
      action,
      resourceTitle: resourceTitle || 'N/A',
      timestamp: nowIso,
      status: 'Completed',
      uid: user.uid
    };

    // Immediate optimistic state update
    setActivities(prev => [localEntry, ...prev.filter(a => a.id !== localEntry.id)].slice(0, 50));

    try {
      if (auth.currentUser) {
        await addDoc(collection(db, 'workspace_activity'), {
          service,
          action,
          resourceTitle: resourceTitle || 'N/A',
          timestamp: nowIso,
          status: 'Completed',
          uid: auth.currentUser.uid
        });
      }
    } catch (error) {
      console.warn('Workspace activity Firestore persistence handled:', error);
    }
  };

  // Google Login Handler
  const handleLogin = async () => {
    setIsLoggingIn(true);
    setStatusMessage(null);
    try {
      const res = await googleSignIn();
      if (res) {
        setUser(res.user);
        setToken(res.accessToken);
        setStatusMessage({
          type: 'success',
          text: `Connected to Google Workspace as ${res.user.email} with Drive, Sheets, Gmail, Calendar, Docs, Chat, and Meet enabled.`
        });
      }
    } catch (err: any) {
      setStatusMessage({
        type: 'error',
        text: err.message || 'Failed to authenticate with Google.'
      });
    } finally {
      setIsLoggingIn(false);
    }
  };

  const handleLogout = async () => {
    await logout();
    setUser(null);
    setToken(null);
    setDriveFiles([]);
    setCalendarEvents([]);
    setChatSpaces([]);
    setStatusMessage({ type: 'info', text: 'Signed out of Google Workspace.' });
  };

  // --- DRIVE ACTIONS ---
  const handleFetchDriveFiles = async () => {
    if (!token) return;
    setIsLoading(true);
    try {
      const files = await listDriveFiles(token);
      setDriveFiles(files);
      setStatusMessage({ type: 'success', text: `Loaded ${files.length} files from Google Drive.` });
    } catch (err: any) {
      setStatusMessage({ type: 'error', text: err.message });
    } finally {
      setIsLoading(false);
    }
  };

  const promptUploadPleadingToDrive = () => {
    setConfirmModal({
      isOpen: true,
      title: 'Upload Omnibus Mandamus Petition to Google Drive',
      description: 'This operation will create a new legal document file "Omnibus_Consolidated_Mandamus_Petition_Baltimore_County.txt" in your Google Drive root directory. Proceed?',
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        try {
          const content = `IN THE CIRCUIT COURT FOR BALTIMORE COUNTY, MARYLAND (Case No. C-03-CV-26-004128)
AND IN THE U.S. COURT OF APPEALS FOR THE D.C. CIRCUIT (Case No. 24-10492)
IN RE ESTATE OF CAUSTIN LEE MCLAUGHLIN & JUAN JOSE FERNANDEZ

MASTER VERIFIED PETITION FOR MANDAMUS & RULE 18 CONSOLIDATED EXACTION OF LIBERTY
Total Estate Capitalization: $500,000,000.00 Senior Notes
Converted Equities: 45,850,000 Common Shares of Centrus Energy Corp. (NYSE: LEU)
Clearing Venue: The Bank of New York Mellon (DTC Participant Desk)
Co-Custodian: M&T Bank (ABA: 022000046, SWIFT: MANTUS33)

Executed: November 1, 2026`;
          const file = await uploadLegalFileToDrive(token, 'Omnibus_Consolidated_Mandamus_Petition_Baltimore_County.txt', content);
          await logActivityToFirestore('drive', 'Upload File', file.name);
          setStatusMessage({ type: 'success', text: `Successfully uploaded "${file.name}" to Google Drive (ID: ${file.id}).` });
          await handleFetchDriveFiles();
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  // --- SHEETS ACTIONS ---
  const promptExportPortfolioToSheets = () => {
    setConfirmModal({
      isOpen: true,
      title: 'Create Google Spreadsheet with $500M CUSIP Portfolio',
      description: 'This will create a new Google Spreadsheet in your Google Drive containing the complete 1999 Indenture CUSIP Reconciliation (Series A $350M, Series B $150M, 45,850,000 LEU shares) and SEC Form 13F Institutional Audit Table. Proceed?',
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        try {
          const headers = ['Issuer', 'Title of Class', 'CUSIP', 'Value ($1,000s)', 'Shares / Principal', 'Discretion', 'Clearing Status'];
          const rows: (string | number)[][] = [headers];
          
          CUSIP_PORTFOLIO_DATA.forEach(item => {
            rows.push([
              item.issuer,
              item.titleOfClass,
              item.cusip,
              item.valueK,
              item.sharesOrPrincipal,
              item.discretion,
              item.relevance
            ]);
          });

          const sheet = await createPortfolioSheet(token, 'USEC_Centrus_500M_CUSIP_Portfolio_Settlement', rows);
          await logActivityToFirestore('sheets', 'Create Spreadsheet', sheet.title);
          setStatusMessage({
            type: 'success',
            text: `Created Google Sheet "${sheet.title}". URL: ${sheet.spreadsheetUrl}`
          });
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  const promptExportAlgorithmsToSheets = () => {
    setConfirmModal({
      isOpen: true,
      title: 'Export 31 Financial Algorithms to Google Sheets',
      description: 'This will create a new Google Sheet containing all 31 algorithms, modules, formulations, and computational complexities. Proceed?',
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        try {
          const headers = ['#', 'ID', 'Module', 'Algorithm Name', 'Purpose', 'Formulation', 'Complexity', 'Status'];
          const rows: (string | number)[][] = [headers];
          
          ALGORITHM_SUITE.forEach(a => {
            rows.push([a.num, a.id, a.module, a.name, a.purpose, a.formulation, a.complexity, a.register]);
          });

          const sheet = await createPortfolioSheet(token, 'Americana_Trust_31_Algorithm_Master_Suite', rows);
          await logActivityToFirestore('sheets', 'Export Algorithms', sheet.title);
          setStatusMessage({
            type: 'success',
            text: `Created Google Sheet "${sheet.title}".`
          });
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  // --- GMAIL ACTIONS ---
  const promptSendEmail = (pkg: typeof EMAIL_PRESENTATION_PACKAGES[0], isDraft: boolean) => {
    setConfirmModal({
      isOpen: true,
      title: isDraft ? `Create Gmail Draft for ${pkg.recipientOrg}` : `Send Email to ${pkg.recipientOrg}`,
      description: isDraft 
        ? `This will create a draft email to "${pkg.toEmail}" in your Gmail account containing the complete legal filing presentation. Proceed?`
        : `Are you sure you want to SEND an email from your Gmail account to "${pkg.toEmail}" with subject "${pkg.subject}"? This cannot be undone.`,
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        try {
          if (isDraft) {
            await createGmailDraft(token, {
              to: pkg.toEmail,
              cc: pkg.ccEmail,
              subject: pkg.subject,
              body: pkg.emailBody
            });
            await logActivityToFirestore('gmail', 'Create Draft', pkg.subject);
            setStatusMessage({ type: 'success', text: `Draft created in your Gmail for "${pkg.toEmail}".` });
          } else {
            await sendLegalEmail(token, {
              to: pkg.toEmail,
              cc: pkg.ccEmail,
              subject: pkg.subject,
              body: pkg.emailBody
            });
            await logActivityToFirestore('gmail', 'Send Email', pkg.subject);
            setStatusMessage({ type: 'success', text: `Email successfully sent via Gmail to "${pkg.toEmail}".` });
          }
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  const promptBatchSendAllEmails = () => {
    setConfirmModal({
      isOpen: true,
      title: '⚡ 1-Click Send All 5 Packages via Gmail API',
      description: `This will dispatch all 5 formal legal packages (Baltimore County Circuit Court, D.C. Circuit Court of Appeals, The Bank of New York Mellon, M&T Bank, and Wyoming Secretary of State) from your connected Gmail account (${user?.email}) and register cryptographic delivery records in Firestore. Proceed?`,
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        let successCount = 0;
        try {
          for (const pkg of EMAIL_PRESENTATION_PACKAGES) {
            await sendLegalEmail(token, {
              to: pkg.toEmail,
              cc: pkg.ccEmail,
              subject: pkg.subject,
              body: pkg.emailBody
            });
            await logActivityToFirestore('gmail', '1-Click Batch Send', `${pkg.recipientOrg}: ${pkg.subject}`);
            successCount++;
          }
          setStatusMessage({
            type: 'success',
            text: `Successfully dispatched all ${successCount} correspondence packages and exhibits in 1 click via Gmail API!`
          });
        } catch (err: any) {
          setStatusMessage({
            type: successCount > 0 ? 'info' : 'error',
            text: `Dispatched ${successCount}/5 packages. Error: ${err.message}`
          });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  // --- CALENDAR & MEET ACTIONS ---
  const handleFetchCalendar = async () => {
    if (!token) return;
    setIsLoading(true);
    try {
      const items = await listCalendarEvents(token);
      setCalendarEvents(items);
      setStatusMessage({ type: 'success', text: `Retrieved ${items.length} upcoming calendar events.` });
    } catch (err: any) {
      setStatusMessage({ type: 'error', text: err.message });
    } finally {
      setIsLoading(false);
    }
  };

  const promptScheduleHearing = (withMeet: boolean) => {
    setConfirmModal({
      isOpen: true,
      title: withMeet ? 'Schedule Baltimore County Hearing with Google Meet' : 'Schedule Court Docket Hearing',
      description: `This will create a calendar event "Baltimore County Circuit Court Status Conference (Case C-03-CV-26-004128)" on your primary Google Calendar${withMeet ? ' with an active Google Meet video conference link' : ''}. Proceed?`,
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        try {
          const startTime = new Date(Date.now() + 86400000 * 2); // 2 days from now
          startTime.setHours(10, 0, 0, 0);
          const endTime = new Date(startTime.getTime() + 3600000); // 1 hour duration

          const ev = await scheduleCourtEvent(token, {
            summary: 'Circuit Court for Baltimore County Hearing (Case No. C-03-CV-26-004128)',
            description: `Omnibus Mandamus, Rule 18 Consolidated Exaction of Liberty, and Indenture Specific Performance Conference.
Petitioner: Caustin Lee McLaughlin & Juan Jose Fernandez
Respondents: BNY Mellon, Computershare, Centrus Energy Corp.
Location: Towson, MD 21204`,
            location: 'Circuit Court for Baltimore County, County Courts Building, Towson, MD 21204',
            startTime: startTime.toISOString(),
            endTime: endTime.toISOString(),
            createMeetRoom: withMeet
          });

          await logActivityToFirestore(withMeet ? 'meet' : 'calendar', 'Schedule Event', ev.summary);
          const meetUrl = ev.conferenceData?.entryPoints?.find((ep: any) => ep.entryPointType === 'video')?.uri;
          setStatusMessage({
            type: 'success',
            text: `Scheduled calendar event! ${meetUrl ? `Google Meet Link: ${meetUrl}` : ''}`
          });
          await handleFetchCalendar();
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  // --- COURT SCHEDULING & MILESTONE INVITATION HANDLERS ---
  const promptScheduleMilestone = async (eventPayload: CourtCalendarEvent, milestoneTitle: string) => {
    const attendeeCount = eventPayload.attendees?.length || 0;
    const formattedStartTime = new Date(eventPayload.startTime).toLocaleString('en-US', {
      dateStyle: 'full',
      timeStyle: 'short'
    });

    setConfirmModal({
      isOpen: true,
      title: `Schedule Court Appearance & Send Invitations`,
      description: `This will create the event "${eventPayload.summary}" on your primary Google Calendar scheduled for ${formattedStartTime} at "${eventPayload.location}".

${attendeeCount > 0 ? `Official Google Calendar email invitations will be dispatched to ${attendeeCount} parties: ${eventPayload.attendees?.map(a => a.email).join(', ')}.` : 'No external attendees selected.'}
${eventPayload.createMeetRoom ? 'An active Google Meet video conference link will be automatically generated and embedded in the invitations.' : ''}

Proceed with scheduling and dispatching invitations?`,
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        try {
          const ev = await scheduleCourtEvent(token, eventPayload);
          await logActivityToFirestore('calendar', 'Court Invitation Sent', `${milestoneTitle} (${attendeeCount} invitees)`);
          const meetUrl = ev.conferenceData?.entryPoints?.find((ep: any) => ep.entryPointType === 'video')?.uri;
          setStatusMessage({
            type: 'success',
            text: `Successfully scheduled "${milestoneTitle}" and sent Google Calendar invitations to ${attendeeCount} parties! ${meetUrl ? `Google Meet Link: ${meetUrl}` : ''}`
          });
          await handleFetchCalendar();
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  const promptBatchScheduleAllMilestones = async (events: { event: CourtCalendarEvent; milestoneTitle: string }[]) => {
    setConfirmModal({
      isOpen: true,
      title: `Batch Sync All Master Pleading Milestones (${events.length} Events)`,
      description: `This will automatically schedule ${events.length} court appearance dates from the Master Mandamus and Rule 18 Pleading on your Google Calendar, generate Google Meet video bridges, and send official calendar invitations to named co-trustees, depository bank representatives, and court clerks.

Proceed with batch calendar generation and invitation dispatch?`,
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        let successCount = 0;
        const failed: string[] = [];
        try {
          for (const item of events) {
            try {
              await scheduleCourtEvent(token, item.event);
              await logActivityToFirestore('calendar', 'Batch Milestone Invite', item.milestoneTitle);
              successCount++;
            } catch (e: any) {
              console.warn(`Error scheduling ${item.milestoneTitle}:`, e);
              failed.push(item.milestoneTitle);
            }
          }
          await handleFetchCalendar();
          setStatusMessage({
            type: successCount > 0 ? 'success' : 'error',
            text: `Batch scheduled and dispatched invitations for ${successCount}/${events.length} court appearances to Google Calendar.${failed.length > 0 ? ` (Failed: ${failed.join(', ')})` : ''}`
          });
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  // --- DOCS ACTIONS ---
  const promptExportPleadingToDoc = () => {
    setConfirmModal({
      isOpen: true,
      title: 'Export Omnibus Mandamus Petition to Google Docs',
      description: 'This will create an editable Google Doc in your Google Drive titled "Baltimore County Circuit Court & DC Circuit Omnibus Mandamus Petition" containing the full verified legal brief. Proceed?',
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        try {
          const body = `IN THE CIRCUIT COURT FOR BALTIMORE COUNTY, MARYLAND
CASE NO. C-03-CV-26-004128

IN RE ESTATE OF CAUSTIN LEE MCLAUGHLIN, Debtor-in-Possession
AND JUAN JOSE FERNANDEZ, Co-Trustee

v.

THE BANK OF NEW YORK MELLON, et al.

VERIFIED PETITION FOR PEREMPTORY WRIT OF MANDAMUS,
DECLARATORY JUDGMENT & RULE 18 CONSOLIDATED EXACTION OF LIBERTY

1. Plenary Jurisdiction under Md. Rule 15-701 & 28 U.S.C. § 1651.
2. The $500,000,000.00 Senior Notes Indenture converts into 45,850,000 common shares of Centrus Energy Corp. (NYSE: LEU).
3. Rule 18 Exaction of Liberty Iterations I through IV joined pursuant to Fed. R. Civ. P. 18(a) and Md. Rule 2-303(b).
4. Depository clearing commanded to BNY Mellon DTC Participant desk.

Executed: November 1, 2026`;

          const docResult = await createGoogleDoc(token, 'Baltimore_County_Circuit_Court_Omnibus_Mandamus_Petition', body);
          await logActivityToFirestore('docs', 'Create Document', docResult.title);
          setStatusMessage({
            type: 'success',
            text: `Created Google Doc "${docResult.title}" (ID: ${docResult.documentId}).`
          });
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  // --- CHAT ACTIONS ---
  const handleFetchChatSpaces = async () => {
    if (!token) return;
    setIsLoading(true);
    try {
      const spaces = await listChatSpaces(token);
      setChatSpaces(spaces);
      if (spaces.length > 0 && !selectedChatSpace) {
        setSelectedChatSpace(spaces[0].name);
      }
      setStatusMessage({ type: 'success', text: `Found ${spaces.length} Google Chat spaces.` });
    } catch (err: any) {
      setStatusMessage({ type: 'error', text: err.message });
    } finally {
      setIsLoading(false);
    }
  };

  const promptPostChatMessage = () => {
    if (!selectedChatSpace || !chatMessageText.trim()) return;
    setConfirmModal({
      isOpen: true,
      title: 'Post Message to Google Chat Space',
      description: `Are you sure you want to post this status update to Chat space "${selectedChatSpace}"?
Message: "${chatMessageText}"`,
      onConfirm: async () => {
        if (!token) return;
        setIsLoading(true);
        try {
          await postChatMessage(token, selectedChatSpace, chatMessageText);
          await logActivityToFirestore('chat', 'Post Message', selectedChatSpace);
          setChatMessageText('');
          setStatusMessage({ type: 'success', text: `Message posted successfully to Google Chat space.` });
        } catch (err: any) {
          setStatusMessage({ type: 'error', text: err.message });
        } finally {
          setIsLoading(false);
          setConfirmModal(prev => ({ ...prev, isOpen: false }));
        }
      }
    });
  };

  return (
    <div className="max-w-7xl mx-auto space-y-6 pb-12">
      {/* Top Banner */}
      <div className="bg-gradient-to-r from-blue-950/60 via-slate-900 to-indigo-950/70 border border-blue-500/40 rounded-xl p-6">
        <div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
          <div>
            <div className="flex items-center gap-2">
              <span className="px-2.5 py-0.5 rounded bg-blue-500/20 text-blue-300 font-mono text-xs font-bold border border-blue-500/30">
                GOOGLE WORKSPACE &amp; CLOUD FIRESTORE
              </span>
              <h2 className="text-xl sm:text-2xl font-bold text-white">
                Google Workspace &amp; Firebase Legal Integration Hub
              </h2>
            </div>
            <p className="text-xs sm:text-sm text-slate-300 mt-1 max-w-3xl">
              Real-time Google Workspace connectivity across <strong>Google Drive</strong>, <strong>Google Sheets</strong>, <strong>Gmail</strong>, <strong>Google Calendar</strong>, <strong>Google Docs</strong>, <strong>Google Chat</strong>, and <strong>Google Meet</strong>, with cloud state persistence backed by Google Cloud Firestore.
            </p>
          </div>

          {/* Auth Action */}
          <div className="shrink-0 flex items-center gap-3">
            {!user ? (
              <button 
                onClick={handleLogin}
                disabled={isLoggingIn}
                className="flex items-center gap-2.5 px-4 py-2 bg-white text-slate-900 hover:bg-slate-100 rounded-lg font-semibold text-xs transition-colors shadow-md disabled:opacity-50 cursor-pointer"
              >
                <svg className="w-4 h-4" viewBox="0 0 48 48">
                  <path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"/>
                  <path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"/>
                  <path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"/>
                  <path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"/>
                </svg>
                <span>{isLoggingIn ? 'Connecting...' : 'Sign in with Google'}</span>
              </button>
            ) : (
              <div className="flex items-center gap-3">
                <div className="flex items-center gap-2 px-3 py-1.5 bg-slate-800/80 rounded-lg border border-slate-700 text-xs">
                  <div className="w-2 h-2 rounded-full bg-emerald-400 animate-pulse"></div>
                  <span className="font-mono text-emerald-300 font-semibold">{user.email}</span>
                </div>
                <button
                  onClick={handleLogout}
                  className="px-3 py-1.5 bg-slate-800 hover:bg-slate-700 text-slate-300 hover:text-white rounded-lg text-xs flex items-center gap-1.5 transition-colors"
                >
                  <LogOut className="w-3.5 h-3.5" />
                  <span>Disconnect</span>
                </button>
              </div>
            )}
          </div>
        </div>

        {/* Status Alert */}
        {statusMessage && (
          <div className={`mt-4 p-3 rounded-lg text-xs flex items-start justify-between gap-2 ${
            statusMessage.type === 'success' 
              ? 'bg-emerald-950/60 border border-emerald-500/40 text-emerald-200' 
              : statusMessage.type === 'error'
              ? 'bg-red-950/60 border border-red-500/40 text-red-200'
              : 'bg-indigo-950/60 border border-indigo-500/40 text-indigo-200'
          }`}>
            <div className="flex items-center gap-2">
              {statusMessage.type === 'success' ? (
                <CheckCircle2 className="w-4 h-4 text-emerald-400 shrink-0" />
              ) : (
                <AlertTriangle className="w-4 h-4 text-amber-400 shrink-0" />
              )}
              <span>{statusMessage.text}</span>
            </div>
            <button 
              onClick={() => setStatusMessage(null)}
              className="text-slate-400 hover:text-white"
            >
              &times;
            </button>
          </div>
        )}
      </div>

      {/* Main Workspace Tabs */}
      <div className="flex flex-wrap items-center gap-2 border-b border-slate-800 pb-3">
        {[
          { id: 'scheduling', label: 'Court Scheduling', icon: Gavel, badge: 'Pleading Sync' },
          { id: 'calendar', label: 'Google Calendar & Meet', icon: Calendar },
          { id: 'drive', label: 'Google Drive', icon: HardDrive },
          { id: 'sheets', label: 'Google Sheets', icon: FileSpreadsheet },
          { id: 'gmail', label: 'Gmail Dispatch', icon: Mail },
          { id: 'docs', label: 'Google Docs', icon: FileText },
          { id: 'chat', label: 'Google Chat', icon: MessageSquare }
        ].map((tab) => {
          const Icon = tab.icon;
          const isActive = activeWorkspaceTab === tab.id;
          return (
            <button
              key={tab.id}
              onClick={() => setActiveWorkspaceTab(tab.id as any)}
              className={`flex items-center gap-2 px-3.5 py-2 rounded-lg text-xs font-semibold transition-all cursor-pointer ${
                isActive
                  ? tab.id === 'scheduling'
                    ? 'bg-amber-600 text-white shadow-lg shadow-amber-900/50'
                    : 'bg-blue-600 text-white shadow-lg'
                  : 'bg-slate-900 text-slate-400 hover:text-slate-200 hover:bg-slate-800'
              }`}
            >
              <Icon className="w-3.5 h-3.5" />
              <span>{tab.label}</span>
              {tab.badge && (
                <span className={`text-[9px] font-bold px-1.5 py-0.2 rounded uppercase ${
                  isActive ? 'bg-black/30 text-amber-200' : 'bg-amber-500/20 text-amber-300'
                }`}>
                  {tab.badge}
                </span>
              )}
            </button>
          );
        })}
      </div>

      {/* Unauthenticated notice */}
      {!user && (
        <div className="p-8 bg-slate-900/60 border border-slate-800 rounded-xl text-center space-y-3">
          <HardDrive className="w-8 h-8 text-blue-400 mx-auto" />
          <h3 className="font-bold text-white text-base">Google Workspace Authentication Required</h3>
          <p className="text-xs text-slate-400 max-w-md mx-auto">
            Click <strong>&ldquo;Sign in with Google&rdquo;</strong> above to grant client-side access to Google Drive, Google Sheets, Gmail, Google Calendar, Google Docs, Google Chat, and Google Meet.
          </p>
        </div>
      )}

      {/* Authenticated Panels */}
      {user && (
        <div className="space-y-6">
          {/* TAB 0: COURT SCHEDULING (DIRECT MASTER PLEADING SYNCHRONIZATION) */}
          {activeWorkspaceTab === 'scheduling' && (
            <CourtSchedulingPanel
              token={token}
              userEmail={user.email}
              isLoading={isLoading}
              onScheduleMilestone={promptScheduleMilestone}
              onBatchScheduleAll={promptBatchScheduleAllMilestones}
              onRefreshCalendar={handleFetchCalendar}
              existingEvents={calendarEvents}
            />
          )}

          {/* TAB 1: GOOGLE DRIVE */}
          {activeWorkspaceTab === 'drive' && (
            <div className="bg-slate-900/90 border border-slate-800 rounded-xl p-6 space-y-6">
              <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 border-b border-slate-800 pb-4">
                <div>
                  <h3 className="text-lg font-bold text-white flex items-center gap-2">
                    <HardDrive className="w-5 h-5 text-blue-400" />
                    <span>Google Drive Document Storage &amp; Pleading Vault</span>
                  </h3>
                  <p className="text-xs text-slate-400 mt-1">
                    Store, organize, and inspect formal court pleadings and liquidation documents directly in Google Drive.
                  </p>
                </div>
                <div className="flex items-center gap-2">
                  <button
                    onClick={handleFetchDriveFiles}
                    disabled={isLoading}
                    className="flex items-center gap-1.5 px-3 py-1.5 bg-slate-800 hover:bg-slate-700 text-slate-200 rounded-lg text-xs transition-colors cursor-pointer"
                  >
                    <RefreshCw className={`w-3.5 h-3.5 ${isLoading ? 'animate-spin' : ''}`} />
                    <span>Refresh Files</span>
                  </button>
                  <button
                    onClick={promptUploadPleadingToDrive}
                    disabled={isLoading}
                    className="flex items-center gap-1.5 px-3 py-1.5 bg-blue-600 hover:bg-blue-500 text-white rounded-lg text-xs font-semibold transition-colors cursor-pointer shadow"
                  >
                    <Plus className="w-3.5 h-3.5" />
                    <span>Upload Omnibus Pleading</span>
                  </button>
                </div>
              </div>

              {/* File list */}
              <div>
                <div className="text-xs font-semibold text-slate-300 uppercase tracking-wider mb-2">
                  Recent Drive Files
                </div>
                {driveFiles.length === 0 ? (
                  <div className="p-6 bg-slate-950 rounded-lg border border-slate-800 text-center text-xs text-slate-500">
                    No files loaded yet. Click &ldquo;Refresh Files&rdquo; to browse your Google Drive or &ldquo;Upload Omnibus Pleading&rdquo; to store court records.
                  </div>
                ) : (
                  <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3">
                    {driveFiles.map((file) => (
                      <div key={file.id} className="p-3 bg-slate-950 rounded-lg border border-slate-800 flex flex-col justify-between hover:border-slate-700 transition-colors">
                        <div>
                          <div className="font-semibold text-xs text-white truncate" title={file.name}>
                            {file.name}
                          </div>
                          <div className="text-[10px] text-slate-400 font-mono mt-0.5">
                            {file.mimeType}
                          </div>
                        </div>
                        <div className="mt-3 pt-2 border-t border-slate-900 flex items-center justify-between text-[10px]">
                          <span className="text-slate-500">{file.size ? `${Math.round(Number(file.size)/1024)} KB` : 'Folder/Doc'}</span>
                          {file.webViewLink && (
                            <a 
                              href={file.webViewLink} 
                              target="_blank" 
                              rel="noreferrer" 
                              className="text-blue-400 hover:underline flex items-center gap-1"
                            >
                              <span>Open in Drive</span>
                              <ExternalLink className="w-2.5 h-2.5" />
                            </a>
                          )}
                        </div>
                      </div>
                    ))}
                  </div>
                )}
              </div>
            </div>
          )}

          {/* TAB 2: GOOGLE SHEETS */}
          {activeWorkspaceTab === 'sheets' && (
            <div className="bg-slate-900/90 border border-slate-800 rounded-xl p-6 space-y-6">
              <div className="border-b border-slate-800 pb-4">
                <h3 className="text-lg font-bold text-white flex items-center gap-2">
                  <FileSpreadsheet className="w-5 h-5 text-emerald-400" />
                  <span>Google Sheets Portfolio &amp; Algorithmic Export</span>
                </h3>
                <p className="text-xs text-slate-400 mt-1">
                  Programmatically generate live Google Spreadsheets populated with the $500,000,000.00 CUSIP portfolio, SEC 13F holdings, and the complete 31-algorithm execution suite.
                </p>
              </div>

              <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div className="p-5 bg-slate-950 rounded-lg border border-slate-800 space-y-3">
                  <div className="flex items-center justify-between">
                    <span className="font-bold text-white text-sm">1. $500M CUSIP Reconciliation Sheet</span>
                    <span className="text-[10px] text-emerald-400 font-mono bg-emerald-950/40 px-2 py-0.5 rounded border border-emerald-500/30">
                      SEC 13F Form Data
                    </span>
                  </div>
                  <p className="text-xs text-slate-400">
                    Exports USEC Series A (917300AA1, $350M), Series B (917300AB9, $150M), and the 45,850,000 LEU common shares conversion matrix into Google Sheets.
                  </p>
                  <button
                    onClick={promptExportPortfolioToSheets}
                    disabled={isLoading}
                    className="w-full py-2 bg-emerald-600 hover:bg-emerald-500 text-white rounded-lg text-xs font-semibold flex items-center justify-center gap-2 transition-colors cursor-pointer shadow"
                  >
                    <FileSpreadsheet className="w-3.5 h-3.5" />
                    <span>Create CUSIP Portfolio Sheet</span>
                  </button>
                </div>

                <div className="p-5 bg-slate-950 rounded-lg border border-slate-800 space-y-3">
                  <div className="flex items-center justify-between">
                    <span className="font-bold text-white text-sm">2. 31-Algorithm Suite Master Sheet</span>
                    <span className="text-[10px] text-indigo-400 font-mono bg-indigo-950/40 px-2 py-0.5 rounded border border-indigo-500/30">
                      11 Modules Catalog
                    </span>
                  </div>
                  <p className="text-xs text-slate-400">
                    Exports all 31 algorithms, mathematical formulations, Big-O complexities, and implementation registers into a structured spreadsheet.
                  </p>
                  <button
                    onClick={promptExportAlgorithmsToSheets}
                    disabled={isLoading}
                    className="w-full py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-xs font-semibold flex items-center justify-center gap-2 transition-colors cursor-pointer shadow"
                  >
                    <FileSpreadsheet className="w-3.5 h-3.5" />
                    <span>Create Algorithms Sheet</span>
                  </button>
                </div>
              </div>
            </div>
          )}

          {/* TAB 3: GMAIL DISPATCH */}
          {activeWorkspaceTab === 'gmail' && (
            <div className="bg-slate-900/90 border border-slate-800 rounded-xl p-6 space-y-6">
              <div className="border-b border-slate-800 pb-4">
                <h3 className="text-lg font-bold text-white flex items-center gap-2">
                  <Mail className="w-5 h-5 text-red-400" />
                  <span>Gmail Legal Presentation Dispatch Hub</span>
                </h3>
                <p className="text-xs text-slate-400 mt-1">
                  Send or draft authenticated presentment packages directly through your connected Gmail account (<code>{user.email}</code>) to court clerks, BNY Mellon, and counterparties.
                </p>
              </div>

              {/* 1-Click Batch Dispatch Entire Docket Banner */}
              <div className="p-4 bg-gradient-to-r from-indigo-950/80 via-slate-950 to-emerald-950/80 border border-indigo-500/30 rounded-xl flex flex-col sm:flex-row items-center justify-between gap-3 shadow-lg">
                <div className="flex items-center gap-3">
                  <div className="p-2.5 rounded-lg bg-amber-500/20 text-amber-300 border border-amber-500/30 shrink-0">
                    <Zap className="w-5 h-5 text-amber-400" />
                  </div>
                  <div>
                    <h4 className="font-bold text-white text-xs sm:text-sm">
                      1-Click Batch Transmit All 5 Packages via Gmail API
                    </h4>
                    <p className="text-[11px] text-slate-300">
                      Dispatches all 5 court filings, BNY Mellon DTC instructions, and M&amp;T Bank packages sequentially in one click with live delivery logging.
                    </p>
                  </div>
                </div>

                <button
                  onClick={promptBatchSendAllEmails}
                  disabled={isLoading}
                  className="flex items-center gap-2 px-4 py-2 bg-gradient-to-r from-amber-500 via-indigo-600 to-emerald-600 hover:brightness-110 text-white rounded-lg text-xs font-bold transition-all shadow-md cursor-pointer whitespace-nowrap"
                >
                  <Zap className="w-3.5 h-3.5 text-amber-200" />
                  <span>⚡ 1-Click Send All</span>
                </button>
              </div>

              <div className="space-y-4">
                {EMAIL_PRESENTATION_PACKAGES.map((pkg) => (
                  <div key={pkg.id} className="p-4 bg-slate-950 rounded-lg border border-slate-800 space-y-3">
                    <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 border-b border-slate-800/80 pb-2">
                      <div>
                        <div className="font-bold text-white text-sm">{pkg.recipientOrg}</div>
                        <div className="text-xs text-slate-400">
                          To: <span className="font-mono text-indigo-300">{pkg.toEmail}</span>
                        </div>
                      </div>
                      <div className="flex items-center gap-2">
                        <button
                          onClick={() => promptSendEmail(pkg, true)}
                          disabled={isLoading}
                          className="px-3 py-1 bg-slate-800 hover:bg-slate-700 text-slate-200 rounded text-xs transition-colors cursor-pointer"
                        >
                          Save as Draft
                        </button>
                        <button
                          onClick={() => promptSendEmail(pkg, false)}
                          disabled={isLoading}
                          className="px-3 py-1 bg-red-600 hover:bg-red-500 text-white rounded text-xs font-semibold transition-colors cursor-pointer flex items-center gap-1.5 shadow"
                        >
                          <Send className="w-3 h-3" />
                          <span>Send via Gmail</span>
                        </button>
                      </div>
                    </div>
                    <div className="text-xs text-slate-300">
                      <strong>Subject:</strong> {pkg.subject}
                    </div>
                    <div className="text-[11px] text-slate-400">
                      <strong>Attachments ({pkg.attachedDocuments.length}):</strong>{' '}
                      {pkg.attachedDocuments.map(d => d.title).join(', ')}
                    </div>
                  </div>
                ))}
              </div>
            </div>
          )}

          {/* TAB 4: GOOGLE CALENDAR & MEET */}
          {activeWorkspaceTab === 'calendar' && (
            <div className="bg-slate-900/90 border border-slate-800 rounded-xl p-6 space-y-6">
              <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 border-b border-slate-800 pb-4">
                <div>
                  <h3 className="text-lg font-bold text-white flex items-center gap-2">
                    <Calendar className="w-5 h-5 text-amber-400" />
                    <span>Google Calendar &amp; Google Meet Court Conferences</span>
                  </h3>
                  <p className="text-xs text-slate-400 mt-1">
                    Schedule case hearings and generate Google Meet video conference rooms for Baltimore County Circuit Court (Case No. C-03-CV-26-004128) and D.C. Circuit appeals.
                  </p>
                </div>
                <div className="flex items-center gap-2">
                  <button
                    onClick={handleFetchCalendar}
                    disabled={isLoading}
                    className="flex items-center gap-1.5 px-3 py-1.5 bg-slate-800 hover:bg-slate-700 text-slate-200 rounded-lg text-xs transition-colors cursor-pointer"
                  >
                    <RefreshCw className={`w-3.5 h-3.5 ${isLoading ? 'animate-spin' : ''}`} />
                    <span>Fetch Calendar</span>
                  </button>
                  <button
                    onClick={() => promptScheduleHearing(true)}
                    disabled={isLoading}
                    className="flex items-center gap-1.5 px-3 py-1.5 bg-amber-600 hover:bg-amber-500 text-white rounded-lg text-xs font-semibold transition-colors cursor-pointer shadow"
                  >
                    <Video className="w-3.5 h-3.5" />
                    <span>Schedule Hearing + Google Meet</span>
                  </button>
                </div>
              </div>

              {/* Upcoming Events */}
              <div>
                <div className="text-xs font-semibold text-slate-300 uppercase tracking-wider mb-2">
                  Upcoming Court Events
                </div>
                {calendarEvents.length === 0 ? (
                  <div className="p-6 bg-slate-950 rounded-lg border border-slate-800 text-center text-xs text-slate-500">
                    No upcoming events found or not yet fetched. Click &ldquo;Fetch Calendar&rdquo; or schedule a hearing.
                  </div>
                ) : (
                  <div className="space-y-2">
                    {calendarEvents.map((ev) => (
                      <div key={ev.id} className="p-3 bg-slate-950 rounded-lg border border-slate-800 flex items-center justify-between">
                        <div>
                          <div className="font-semibold text-xs text-white">{ev.summary}</div>
                          <div className="text-[10px] text-slate-400 mt-0.5">
                            {ev.start?.dateTime || ev.start?.date} — {ev.location || 'Online'}
                          </div>
                        </div>
                        {ev.hangoutLink && (
                          <a
                            href={ev.hangoutLink}
                            target="_blank"
                            rel="noreferrer"
                            className="px-2.5 py-1 bg-blue-600/30 text-blue-300 border border-blue-500/40 rounded text-xs flex items-center gap-1 hover:bg-blue-600/50"
                          >
                            <Video className="w-3 h-3" />
                            <span>Join Meet</span>
                          </a>
                        )}
                      </div>
                    ))}
                  </div>
                )}
              </div>
            </div>
          )}

          {/* TAB 5: GOOGLE DOCS */}
          {activeWorkspaceTab === 'docs' && (
            <div className="bg-slate-900/90 border border-slate-800 rounded-xl p-6 space-y-6">
              <div className="border-b border-slate-800 pb-4">
                <h3 className="text-lg font-bold text-white flex items-center gap-2">
                  <FileText className="w-5 h-5 text-indigo-400" />
                  <span>Google Docs Legal Brief Synchronization</span>
                </h3>
                <p className="text-xs text-slate-400 mt-1">
                  Export master pleadings and Rule 18 briefs into Google Docs for team drafting, commentary, and legal formatting.
                </p>
              </div>

              <div className="p-5 bg-slate-950 rounded-lg border border-slate-800 flex flex-col sm:flex-row items-center justify-between gap-4">
                <div>
                  <h4 className="font-bold text-white text-sm">Export Omnibus Verified Pleading to Google Docs</h4>
                  <p className="text-xs text-slate-400 mt-1 max-w-xl">
                    Creates a collaborative Google Doc containing the caption, $500M capitalization specifications, Rule 18 exaction iterations, and BNY Mellon directives.
                  </p>
                </div>
                <button
                  onClick={promptExportPleadingToDoc}
                  disabled={isLoading}
                  className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-xs font-semibold shrink-0 transition-colors shadow cursor-pointer flex items-center gap-2"
                >
                  <FileText className="w-4 h-4" />
                  <span>Export to Google Docs</span>
                </button>
              </div>
            </div>
          )}

          {/* TAB 6: GOOGLE CHAT */}
          {activeWorkspaceTab === 'chat' && (
            <div className="bg-slate-900/90 border border-slate-800 rounded-xl p-6 space-y-6">
              <div className="border-b border-slate-800 pb-4">
                <h3 className="text-lg font-bold text-white flex items-center gap-2">
                  <MessageSquare className="w-5 h-5 text-teal-400" />
                  <span>Google Chat Case Status Broadcaster</span>
                </h3>
                <p className="text-xs text-slate-400 mt-1">
                  Post real-time status updates and execution notices to your legal team&apos;s Google Chat spaces.
                </p>
              </div>

              <div className="space-y-4 max-w-xl">
                <div className="flex items-center gap-3">
                  <button
                    onClick={handleFetchChatSpaces}
                    disabled={isLoading}
                    className="px-3 py-1.5 bg-slate-800 hover:bg-slate-700 text-slate-200 rounded-lg text-xs transition-colors cursor-pointer"
                  >
                    Fetch Chat Spaces
                  </button>
                  {chatSpaces.length > 0 && (
                    <select
                      value={selectedChatSpace}
                      onChange={(e) => setSelectedChatSpace(e.target.value)}
                      className="flex-1 p-1.5 bg-slate-950 border border-slate-700 rounded text-xs text-slate-200 font-mono"
                    >
                      {chatSpaces.map(s => (
                        <option key={s.name} value={s.name}>
                          {s.displayName || s.name}
                        </option>
                      ))}
                    </select>
                  )}
                </div>

                <div className="space-y-2">
                  <label className="text-xs text-slate-400 block">Message Text:</label>
                  <textarea
                    rows={3}
                    value={chatMessageText}
                    onChange={(e) => setChatMessageText(e.target.value)}
                    placeholder="Enter status notice (e.g. Baltimore County filing submitted via MDEC...)"
                    className="w-full p-2.5 bg-slate-950 border border-slate-700 rounded-lg text-xs text-slate-200 font-sans focus:outline-none focus:border-teal-500"
                  />
                  <button
                    onClick={promptPostChatMessage}
                    disabled={!chatMessageText.trim() || !selectedChatSpace || isLoading}
                    className="px-4 py-2 bg-teal-600 hover:bg-teal-500 text-white rounded-lg text-xs font-semibold transition-colors disabled:opacity-50 cursor-pointer flex items-center gap-2 shadow"
                  >
                    <Send className="w-3.5 h-3.5" />
                    <span>Post to Google Chat</span>
                  </button>
                </div>
              </div>
            </div>
          )}

          {/* FIRESTORE AUDIT LEDGER */}
          <div className="bg-slate-900/90 border border-slate-800 rounded-xl p-6 space-y-4">
            <div className="flex items-center justify-between border-b border-slate-800 pb-3">
              <div className="flex items-center gap-2">
                <Database className="w-4 h-4 text-emerald-400" />
                <h4 className="font-bold text-white text-sm">
                  Google Cloud Firestore — Real-Time Activity Audit Trail
                </h4>
              </div>
              <span className="text-[10px] font-mono text-emerald-400 bg-emerald-950/40 px-2 py-0.5 rounded border border-emerald-500/30">
                Collection: workspace_activity
              </span>
            </div>

            {activities.length === 0 ? (
              <div className="p-4 bg-slate-950 rounded-lg border border-slate-800 text-center text-xs text-slate-500">
                No Workspace activities logged in Firestore yet. Execute a Drive, Sheets, or Gmail action above to see real-time database synchronization.
              </div>
            ) : (
              <div className="overflow-x-auto">
                <table className="w-full text-xs text-left border border-slate-800">
                  <thead>
                    <tr className="bg-slate-950 text-slate-300">
                      <th className="p-2">Service</th>
                      <th className="p-2">Action</th>
                      <th className="p-2">Target Resource</th>
                      <th className="p-2">Timestamp</th>
                      <th className="p-2">Status</th>
                    </tr>
                  </thead>
                  <tbody className="divide-y divide-slate-800 font-mono text-[11px]">
                    {activities.map((act) => (
                      <tr key={act.id} className="hover:bg-slate-800/40">
                        <td className="p-2 font-bold uppercase text-blue-400">{act.service}</td>
                        <td className="p-2 text-slate-200">{act.action}</td>
                        <td className="p-2 text-slate-400 truncate max-w-xs">{act.resourceTitle}</td>
                        <td className="p-2 text-slate-500">{typeof act.timestamp === 'string' ? act.timestamp.replace('T', ' ').slice(0, 19) : 'Recent'}</td>
                        <td className="p-2 text-emerald-400 font-semibold">{act.status}</td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            )}
          </div>
        </div>
      )}

      {/* MANDATORY USER CONFIRMATION MODAL */}
      {confirmModal.isOpen && (
        <div className="fixed inset-0 z-50 bg-slate-950/80 backdrop-blur-sm flex items-center justify-center p-4">
          <div className="bg-slate-900 border border-slate-700 rounded-xl max-w-md w-full p-6 shadow-2xl space-y-4">
            <div className="flex items-center gap-3">
              <div className="w-10 h-10 rounded-full bg-amber-500/20 text-amber-400 flex items-center justify-center shrink-0">
                <AlertTriangle className="w-5 h-5" />
              </div>
              <div>
                <h4 className="font-bold text-white text-base">{confirmModal.title}</h4>
                <p className="text-xs text-slate-400 mt-0.5">Google Workspace User Confirmation</p>
              </div>
            </div>

            <p className="text-xs text-slate-300 leading-relaxed bg-slate-950 p-3 rounded-lg border border-slate-800">
              {confirmModal.description}
            </p>

            <div className="flex items-center justify-end gap-3 pt-2">
              <button
                onClick={() => setConfirmModal(prev => ({ ...prev, isOpen: false }))}
                className="px-4 py-2 bg-slate-800 hover:bg-slate-700 text-slate-300 rounded-lg text-xs font-semibold transition-colors cursor-pointer"
              >
                Cancel
              </button>
              <button
                onClick={confirmModal.onConfirm}
                className="px-4 py-2 bg-blue-600 hover:bg-blue-500 text-white rounded-lg text-xs font-semibold transition-colors shadow cursor-pointer flex items-center gap-2"
              >
                <Check className="w-4 h-4" />
                <span>Confirm &amp; Proceed</span>
              </button>
            </div>
          </div>
        </div>
      )}
    </div>
  );
};