File size: 108,214 Bytes
e9a4c3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
792ee39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9a4c3e
 
 
 
 
 
992a41f
dcd43e2
071ce2b
868b75a
071ce2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5696cb
992a41f
81a8217
9a6cb9a
 
 
 
 
 
0e600b9
e9a4c3e
4b54047
 
98c8696
81a8217
e9a4c3e
aa1490a
 
 
 
 
 
 
868b75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd43e2
 
 
 
 
 
868b75a
0e600b9
b2aa446
 
dcd43e2
 
 
086cc6b
 
dcd43e2
 
cfa8541
 
 
 
 
 
 
 
5007b7e
b2aa446
 
cfa8541
 
dcd43e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f03fdf0
 
 
 
 
 
 
98c8696
 
 
 
 
f03fdf0
 
 
 
98c8696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f03fdf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98c8696
 
 
 
 
 
 
 
 
 
 
 
f03fdf0
 
 
 
98c8696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa8541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5007b7e
b2aa446
cfa8541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6cb9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa8541
 
9a6cb9a
cfa8541
9a6cb9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa8541
 
c72f0a1
cfa8541
 
 
 
 
 
98c8696
81a8217
cfa8541
 
0e600b9
98c8696
 
 
 
81a8217
cfa8541
98c8696
 
 
81a8217
 
98c8696
cfa8541
 
 
 
98c8696
 
81a8217
 
 
 
 
98c8696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa8541
 
 
868b75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd43e2
 
 
 
868b75a
 
 
dcd43e2
 
 
868b75a
 
 
 
 
dcd43e2
 
 
 
b2aa446
086cc6b
dcd43e2
 
 
0e600b9
b2aa446
 
 
dcd43e2
868b75a
dcd43e2
 
086cc6b
dcd43e2
 
 
 
 
0e600b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd43e2
868b75a
 
 
dcd43e2
 
868b75a
dcd43e2
 
 
 
 
 
 
 
0e600b9
dcd43e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72f0a1
dcd43e2
 
 
85f794d
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd43e2
 
 
 
868b75a
dcd43e2
 
 
 
 
 
 
 
0e600b9
 
 
086cc6b
 
 
 
 
 
 
 
 
 
0e600b9
 
 
 
 
 
 
 
 
 
 
 
3bc147d
 
 
0e600b9
 
 
 
 
 
 
 
 
aa1490a
 
 
 
 
 
868b75a
85f794d
 
868b75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85f794d
868b75a
 
 
aa1490a
 
81a8217
 
aa1490a
0e600b9
 
aa1490a
 
 
0e600b9
aa1490a
 
b2aa446
dcd43e2
b2aa446
aa1490a
81a8217
85f794d
b2aa446
81a8217
 
dcd43e2
b2aa446
 
 
0e600b9
 
aa1490a
dcd43e2
85f794d
 
dcd43e2
 
85f794d
dcd43e2
85f794d
 
 
dcd43e2
 
 
 
 
 
 
 
 
 
1293f2e
 
dcd43e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9a4c3e
 
dcd43e2
e9a4c3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd43e2
e9a4c3e
 
 
 
dcd43e2
 
 
 
e9a4c3e
 
 
 
 
4b54047
e9a4c3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72f0a1
dcd43e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72f0a1
071ce2b
dcd43e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
071ce2b
 
dcd43e2
 
 
 
 
 
 
 
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b54047
 
e13472e
 
 
 
 
 
 
 
 
 
 
 
4b54047
 
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b54047
 
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5696cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e13472e
 
 
 
 
 
 
4b54047
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
992a41f
 
e13472e
 
 
 
992a41f
 
e13472e
992a41f
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
992a41f
 
 
 
 
 
 
 
 
 
e13472e
 
992a41f
 
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
992a41f
 
 
 
 
 
 
e9a4c3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa8541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6cb9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa8541
 
 
 
 
 
 
 
9a6cb9a
 
 
cfa8541
 
 
 
 
 
9a6cb9a
 
 
 
cfa8541
 
 
 
 
 
 
 
 
 
 
071ce2b
cfa8541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
071ce2b
cfa8541
071ce2b
cfa8541
 
 
 
 
 
 
 
 
 
 
071ce2b
cfa8541
 
 
 
 
 
 
 
 
 
 
 
7f72224
cfa8541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f72224
 
cfa8541
 
 
 
9a6cb9a
cfa8541
 
9a6cb9a
 
cfa8541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6cb9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa8541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9a4c3e
792ee39
 
 
 
 
 
 
 
f5696cb
 
 
792ee39
992a41f
 
 
 
 
 
 
 
 
792ee39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
992a41f
 
 
 
 
 
 
 
 
 
792ee39
 
992a41f
81a8217
 
 
792ee39
 
992a41f
792ee39
992a41f
792ee39
 
 
992a41f
792ee39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
992a41f
792ee39
 
992a41f
 
 
 
 
 
 
792ee39
 
 
 
 
 
 
 
 
e13472e
792ee39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9a4c3e
 
e13472e
e9a4c3e
868b75a
 
e13472e
 
 
81a8217
e13472e
 
 
 
 
 
dcd43e2
e13472e
 
9a6cb9a
 
 
 
 
e13472e
e9a4c3e
 
81a8217
e9a4c3e
e13472e
 
dcd43e2
e13472e
9a6cb9a
e13472e
9a6cb9a
e13472e
f5696cb
868b75a
e13472e
 
 
81a8217
 
 
 
 
 
4ca2251
 
 
9a6cb9a
 
 
868b75a
 
 
 
e9a4c3e
f5696cb
e9a4c3e
dcd43e2
 
e13472e
 
 
071ce2b
e9a4c3e
4b54047
 
 
dcd43e2
 
c72f0a1
9a6cb9a
98c8696
4b54047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e13472e
 
992a41f
792ee39
 
 
e13472e
 
 
 
 
 
 
 
 
 
 
e9a4c3e
 
 
e13472e
aa1490a
 
e13472e
e9a4c3e
e13472e
a83fcff
 
 
 
 
 
868b75a
e13472e
 
a83fcff
 
868b75a
a83fcff
868b75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81a8217
 
e9a4c3e
f5696cb
 
 
 
 
 
 
 
 
 
 
 
e9a4c3e
f5696cb
 
 
e9a4c3e
 
 
 
 
 
868b75a
e9a4c3e
 
 
dcd43e2
e13472e
e9a4c3e
 
 
 
071ce2b
e9a4c3e
 
 
 
 
 
 
 
 
071ce2b
 
 
 
e9a4c3e
 
f5696cb
 
 
 
 
e9a4c3e
 
f5696cb
e9a4c3e
868b75a
e9a4c3e
 
792ee39
 
e13472e
 
792ee39
 
 
 
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd43e2
 
 
 
 
 
 
 
 
 
 
 
 
e13472e
 
 
 
 
 
071ce2b
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfa8541
e13472e
 
 
 
 
992a41f
 
e13472e
 
 
 
868b75a
e13472e
 
 
 
f5696cb
 
 
e13472e
 
cfa8541
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
071ce2b
4b54047
e13472e
 
 
 
792ee39
 
e13472e
792ee39
e13472e
792ee39
 
e13472e
 
4b54047
e13472e
 
 
 
 
071ce2b
 
dcd43e2
e13472e
 
4b54047
e13472e
 
 
 
 
 
 
 
992a41f
e13472e
 
992a41f
e13472e
992a41f
e13472e
 
071ce2b
e13472e
 
992a41f
 
 
 
 
 
 
 
 
e13472e
 
 
 
 
992a41f
e13472e
 
792ee39
dcd43e2
c72f0a1
 
 
e13472e
 
dcd43e2
e13472e
992a41f
 
 
e13472e
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd43e2
e13472e
dcd43e2
e13472e
 
4b54047
e13472e
 
 
 
 
 
992a41f
 
 
 
 
 
 
 
 
dcd43e2
e13472e
c72f0a1
dcd43e2
 
4ca2251
81a8217
 
4ca2251
 
 
 
81a8217
cfa8541
 
 
9a6cb9a
 
 
 
 
 
cfa8541
 
dcd43e2
 
 
 
9a6cb9a
dcd43e2
 
c72f0a1
 
dcd43e2
 
 
 
 
 
 
e9a4c3e
 
 
 
 
4b54047
c72f0a1
9a6cb9a
 
868b75a
 
 
 
 
 
 
 
 
81a8217
868b75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81a8217
868b75a
 
 
 
 
 
 
 
 
 
 
81a8217
 
868b75a
 
81a8217
868b75a
 
 
 
81a8217
868b75a
 
 
 
 
 
 
 
 
c72f0a1
868b75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2aa446
868b75a
086cc6b
 
868b75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72f0a1
868b75a
dcd43e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5696cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd43e2
f5696cb
e9a4c3e
 
e13472e
 
 
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
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
// ---------------------------------------------------------------------------
// connectors/DatabasesTab.tsx — W49-T14, rulings R21/R22/R23/R24/R26, contracts C3 + C4.
//
// ⭐⭐ R21, verbatim: *"Build the onboarding DISCLOSURE, generalized: show the
// source schema and the N databases a connection will create, with what is
// excluded. The table list stays authored in code; the screen DISCLOSES it, it
// does not DERIVE it."*
//
// This file is the DISCLOSURE and nothing else. Every list it paints arrives on
// `GET /connectors/{key}/detail`, composed from `odoo_relational.TABLES` (the
// authored list) joined to the Odoo field catalogue. There is no table name,
// no field name and no count written into this file — a client-side table list
// would be a second answer to "what does this connection create", and the two
// would drift the first time a ninth table was added server-side.
//
// ⛔⛔ R24 — ONLY AN INTROSPECTABLE CONNECTOR GETS THIS SCREEN, AND THE ANSWER
// IS THE SERVER'S. `detail.introspection.introspectable` is authored in
// `core/connector_directory.INTROSPECTABLE_CONNECTORS`; it is NOT "the
// databases list came back empty". Those two are the same empty list and they
// mean opposite things: Meta Ads has no schema, while an Odoo connection whose
// store read failed has one nobody could reach. Inferring the ruling from the
// payload would tell an administrator that Odoo has no schema on the one day
// the schema mattered.
//
// ⛔⛔ R23 — THE SWITCH-OFF ASKS FIRST, AND THE REFUSAL IS QUOTED, NEVER
// COMPOSED. `GET /dependents?kind=table&key=...` answers with a sentence
// ("Odoo customers is used by 25 views, 7 Routes, 2 shares and 1 chart") whose
// ZERO-COUNT KINDS ARE ABSENT BY CONSTRUCTION. Building that sentence here from
// the counts would be a second copy of C3's grammar, and the copy would have to
// decide what to say about `sheet_cell` — a kind whose provider is not
// registered until W49-T11 lands, so the truth is UNKNOWN and not zero. Quoting
// the server's string is the only way this screen cannot claim zero.
//
// ⛔ AND A CHECK THAT DID NOT ANSWER IS NOT A CHECK THAT SAID "NOTHING". If
// `/dependents` fails for any reason the table is LEFT ON and the cause is
// printed. Writing the flag anyway would make R23 a suggestion.
//
// ⭐⭐ W53-T27, R9 / C2 — THREE NAMES PER DATABASE, AND THE TAB MUST NOT BLEND THEM. `native` is
// the vendor's own identifier for the table, `defaultLabel` is the name WE ship, and `override`
// is this tenant's own. All three arrive on `/detail`: there is no native-name map and no
// default-label map in this file, for the same reason there is no table list in it.
//
// ⛔ THE OVERRIDE IS THE ONE THE NAVIGATION RAIL WRITES. R9, verbatim: *"the rename database
// under the Navigation rail also works, it should then sync to the navigation in the connector
// too."* So the rename control here PATCHes the connector door that writes the rail's own
// `nav_meta` bucket, and clearing it falls back to `defaultLabel` rather than blanking the row.
// A second store would give one workspace two names for one database.
//
// ⛔ `native` IS NEVER EDITABLE, AND THE SCREEN SAYS SO RATHER THAN SHOWING A DEAD CONTROL. A
// disabled input with no sentence beside it reads as "not yet", which is a promise this product
// cannot keep: the source names its own tables.
//
// ⚠ PURE OVER PROPS, AND IT FETCHES ONLY ON A CLICK. The modal reads `/detail`
// on mount and `renderToStaticMarkup` runs no effects, so a static proof of
// this tab has to be paintable from a payload — the same reason
// `ConnectorOverviewPanel` and `KeychainTab` are exported panels.
// ---------------------------------------------------------------------------

import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
import type { MouseEvent } from "react";
import { API_V1, CREDENTIALS, TOAST_EVENT, WORKSPACE_STALE_EVENT, signal } from "../apiContract";
import { GridPager } from "../customer-grid/GridPager";

function useRefusalToast() {
  const [messages, setMessages] = useState<{ id: number; text: string }[]>([]);
  const sequence = useRef(0);
  const delivered = useRef(0);
  useEffect(() => {
    if (!messages.length) return;
    const through = messages[messages.length - 1].id;
    for (const message of messages) {
      if (message.id <= delivered.current) continue;
      delivered.current = message.id;
      const text = message.text;
      window.dispatchEvent(new CustomEvent(TOAST_EVENT, { detail: { text, tone: "error" } }));
    }
    setMessages((current) => current.filter((message) => message.id > through));
  }, [messages]);
  return (text: string) => {
    if (!text) return;
    const message = { id: ++sequence.current, text };
    setMessages((current) => [...current, message]);
  };
}

import { ConfirmDialog } from "../ui/ConfirmDialog";
import { useEscapeClose } from "../ui/useEscapeClose";
import { DatabaseFieldsEditor } from "../settings/ManageDatabase";
import type {
  ConnectorDatabase,
  ConnectorDatabaseExclusion,
  ConnectorDatabaseField,
  ConnectorDetail,
} from "./ConnectorModal";
import { parseDatabaseExclusions, parseDatabaseFields, parseDatabasesPage } from "./ConnectorModal";
import type { ConnectorRow } from "./ConnectorsPage";
import { watchMaterialization } from "./materializationJobs";
import type { MaterializationJob } from "./materializationJobs";
import "../styles/sdlc73/connectors.css";
import "./connectors.css";

interface AmazonRefusedReport {
  table: string;
  label: string;
  reason: string;
  amazonStatus: string;
}

interface AmazonFirstLoad {
  remaining: number | null;
  etaSeconds: number;
  cause: string;
  needsAttention: boolean;
  refused?: AmazonRefusedReport[];
  line: string;
  etaDays: number | null;
  details: {
    table: string;
    label: string;
    state: "loaded" | "loading" | "not_offered" | "retrying" | "failed";
    reason: string;
  }[];
}

interface AmazonTableSync {
  rowsLoaded: number | null;
  lastSuccess: string | null;
  inProgress: boolean;
  deferred: { reason: string } | null;
  nextDue: string | null;
  firstLoad: AmazonFirstLoad | null;
  refused?: boolean;
  /** C-pub: a refused or filling table kept at its last good rows while the other tables publish. */
  held?: { reason: string; rows: number | null } | null;
  stuck: boolean | null;
  stuckCause: string;
  unknownCause: string;
  /** A source-internal mismatch: the table published, and both counts are named. */
  warning?: string;
}

export interface DatabaseSyncHealth {
  state: string;
  lastSuccess: string | null;
  sourceTimestamp: string | null;
  materializedTimestamp: string | null;
  rowCount: number | null;
  lagSeconds: number | null;
  error: { code: string; message: string } | null;
  warning?: string | null;
  /** D-12160: reports the source does not offer this account; a standing fact, not a failed sync. */
  notOffered?: number | null;
}

function syncObject(value: unknown): Record<string, unknown> {
  return value !== null && typeof value === "object" && !Array.isArray(value)
    ? value as Record<string, unknown> : {};
}

function syncText(value: unknown): string {
  return typeof value === "string" && value.length <= 512 ? value : "";
}

function syncCount(value: unknown): number | null {
  return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : null;
}

function syncStamp(value: unknown): string | null {
  const text = syncText(value);
  return text && Number.isFinite(Date.parse(text)) ? text : null;
}

interface ConnectorSourceCatchUp {
  active: boolean;
  done: number;
  total: number;
  behind: string;
}

export interface ConnectorCatchUp {
  active: boolean;
  done: number;
  total: number;
  newest: Record<string, string>;
  /** Per-source progress; null when the server reports only the tenant total. */
  sources: Record<string, ConnectorSourceCatchUp> | null;
  /** Set when this status is scoped to one source that settled behind. */
  behind?: string;
}

/** Missing catchUp is compatible; a malformed status is not a settled sync. */
export function parseConnectorCatchUp(value: unknown): ConnectorCatchUp | null {
  const payload = syncObject(value);
  if (payload.ok !== true) throw new Error("Sync status unavailable");
  if (!Object.prototype.hasOwnProperty.call(payload, "catchUp")) return null;
  const raw = syncObject(payload.catchUp);
  const done = syncCount(raw.done);
  const total = syncCount(raw.total);
  if (typeof raw.active !== "boolean" || done === null || total === null || done > total ||
      !raw.newest || typeof raw.newest !== "object" || Array.isArray(raw.newest)) {
    throw new Error("Sync status unavailable");
  }
  const newest: Record<string, string> = {};
  for (const [key, value] of Object.entries(raw.newest)) {
    const stamp = syncStamp(value);
    if (!stamp) throw new Error("Sync status unavailable");
    newest[key] = stamp;
  }
  let sources: Record<string, ConnectorSourceCatchUp> | null = null;
  if (Object.prototype.hasOwnProperty.call(raw, "sources")) {
    const rawSources = syncObject(raw.sources);
    sources = {};
    for (const [name, value] of Object.entries(rawSources)) {
      const entry = syncObject(value);
      const sourceDone = syncCount(entry.done);
      const sourceTotal = syncCount(entry.total);
      if (typeof entry.active !== "boolean" || sourceDone === null || sourceTotal === null
        || sourceDone > sourceTotal || typeof entry.behind !== "string") {
        throw new Error("Sync status unavailable");
      }
      sources[name] = { active: entry.active, done: sourceDone, total: sourceTotal,
        behind: syncText(entry.behind) };
    }
  }
  return { active: raw.active, done, total, newest, sources };
}

/** The catch-up source a connector card or database waits on. */
export function catchUpSourceKey(key: string): string {
  if (key === "amazon_sp" || key === "amazon" || key.startsWith("ut_amzn_")) return "amazon";
  if (key === "meta_ads" || key === "meta" || key.startsWith("ut_meta_")) return "meta";
  return "odoo";
}

/** Scope the tenant status to one source; an older server keeps the tenant total on Odoo only. */
export function sourceCatchUp(catchUp: ConnectorCatchUp | null, source: string): ConnectorCatchUp | null {
  if (!catchUp) return null;
  if (catchUp.sources === null) return source === "odoo" ? catchUp : null;
  const own = catchUp.sources[source];
  if (!own) return null;
  return { ...catchUp, active: catchUp.active && own.active, done: own.done, total: own.total,
    behind: own.behind || undefined };
}

export async function readConnectorCatchUp(signal: AbortSignal): Promise<ConnectorCatchUp | null> {
  const response = await fetch(`${API_V1}/odoo-tables/status`, { credentials: CREDENTIALS, signal });
  if (!response.ok) throw new Error("Sync status unavailable");
  return parseConnectorCatchUp(await response.json());
}

export function catchUpLabel(catchUp: ConnectorCatchUp): string {
  return `Updating ${catchUp.done.toLocaleString()} of ${catchUp.total.toLocaleString()}`;
}

export function behindLabel(reason: string): string {
  return `Behind. ${reason}`;
}

export function useConnectorCatchUp(enabled: boolean) {
  const [state, setState] = useState<{
    catchUp: ConnectorCatchUp | null; error: string; loading: boolean;
  }>({ catchUp: null, error: "", loading: enabled });
  useEffect(() => {
    setState({ catchUp: null, error: "", loading: enabled });
    if (!enabled) return;
    const controller = new AbortController();
    let timer: number | undefined;
    const read = async () => {
      let retry = true;
      try {
        const catchUp = await readConnectorCatchUp(controller.signal);
        if (controller.signal.aborted) return;
        setState({ catchUp, error: "", loading: false });
        retry = catchUp?.active === true;
      } catch {
        if (controller.signal.aborted) return;
        setState((previous) => ({ ...previous, error: "Sync status unavailable", loading: false }));
      }
      if (retry && !controller.signal.aborted) timer = window.setTimeout(() => void read(), 5000);
    };
    void read();
    return () => { controller.abort(); window.clearTimeout(timer); };
  }, [enabled]);
  return enabled ? state : { catchUp: null, error: "", loading: false };
}

export function parseDatabaseSyncHealth(value: unknown): DatabaseSyncHealth | null {
  const raw = syncObject(value);
  const state = syncText(raw.state);
  const allowed = new Set([
    "never_run", "queued", "running", "succeeded", "partial", "failed", "disabled",
  ]);
  if (!allowed.has(state)) return null;
  const rawError = syncObject(raw.error);
  const errorCode = syncText(rawError.code);
  const errorMessage = syncText(rawError.message);
  const lagSeconds = syncCount(raw.lagSeconds);
  return {
    state,
    lastSuccess: syncStamp(raw.lastSuccess),
    sourceTimestamp: syncStamp(raw.sourceTimestamp),
    materializedTimestamp: syncStamp(raw.materializedTimestamp),
    rowCount: syncCount(raw.rowCount),
    lagSeconds,
    error: errorCode && errorMessage ? { code: errorCode, message: errorMessage } : null,
    warning: syncText(raw.warning).trim() || null,
    notOffered: syncCount(raw.notOffered),
  };
}

function parseDatabaseSyncStates(value: unknown): Record<string, DatabaseSyncHealth> | null {
  const payload = syncObject(value);
  if (!Array.isArray(payload.databases)) return null;
  const states: Record<string, DatabaseSyncHealth> = {};
  for (const item of payload.databases) {
    const row = syncObject(item);
    const key = syncText(row.key);
    const health = parseDatabaseSyncHealth(row.syncHealth);
    if (key && health) states[key] = health;
  }
  return states;
}

/**
 * S66-T39 / D-723 — SYNC HEALTH RIDES THE DETAIL ROWS, SUMMARY PAGES INCLUDED. This used to be a
 * second, unpaged `/detail` read made every time the tab mounted, which carried every database
 * with every field again and undid the paging the modal does. Summary rows keep `syncHealth`,
 * so the states are parsed from the rows already read, by the same row parser as before.
 */
function useDatabaseSyncStates(detail: ConnectorDetail) {
  return useMemo(() => {
    const states = parseDatabaseSyncStates({
      databases: detail.databases.map((database) => ({
        key: database.key,
        syncHealth: database.syncHealth,
      })),
    }) ?? {};
    return { states, error: "" };
  }, [detail.databases]);
}

type FieldRead =
  | { phase: "loading"; row: ConnectorDatabase }
  | {
      phase: "ready";
      row: ConnectorDatabase;
      fields: ConnectorDatabaseField[];
      excluded: ConnectorDatabaseExclusion[];
    }
  | { phase: "error"; row: ConnectorDatabase; message: string };

async function readDatabaseFields(
  connectorKey: string,
  databaseKey: string,
  signal: AbortSignal
): Promise<
  | { ok: true; fields: ConnectorDatabaseField[]; excluded: ConnectorDatabaseExclusion[] }
  | { ok: false; message: string }
> {
  try {
    const res = await fetch(
      `${API_V1}/connectors/${encodeURIComponent(connectorKey)}/databases/` +
        `${encodeURIComponent(databaseKey)}/fields`,
      { credentials: CREDENTIALS, signal }
    );
    const body: unknown = await res.json().catch(() => null);
    if (!res.ok) return { ok: false, message: apiError(body, res.status) };
    const payload = syncObject(body);
    if (payload.key !== databaseKey) {
      return {
        ok: false,
        message: "The field list was refused because the answer described a different database.",
      };
    }
    return {
      ok: true,
      fields: parseDatabaseFields(payload.fields),
      excluded: parseDatabaseExclusions(payload.excluded),
    };
  } catch {
    return { ok: false, message: "The field list could not be read. Check the connection and try again." };
  }
}

/**
 * ⭐ S66-T39 / D-723 — A DEFERRED ROW'S FIELDS, READ WHEN SOMEBODY ASKS FOR THEM.
 *
 * A `fields=summary` page sends each database with `fieldsDeferred: true` and empty lists. Those
 * empties mean "not read yet", so a wanted row is read from its own fields route and merged back
 * in; until then the row says it is reading, never "no fields".
 *
 * ⛔ A READ BELONGS TO THE ROW OBJECT IT WAS MADE FOR. A committed Save re-reads `/detail`, which
 * makes new row objects, so an old field list (with old switch states) is never reused. A later
 * database page keeps the existing row objects, so paging never re-reads a field list.
 */
function useDeferredFields(connectorKey: string, served: ConnectorDatabase[], wanted: string[]) {
  const [reads, setReads] = useState<Record<string, FieldRead>>({});
  const wantedKey = wanted.join("\n");
  const controllerRef = useRef<AbortController | null>(null);
  useEffect(() => {
    const controller = new AbortController();
    controllerRef.current = controller;
    return () => controller.abort();
  }, []);
  useEffect(() => {
    const want = new Set(wantedKey ? wantedKey.split("\n") : []);
    const due = served.filter((database) =>
      database.fieldsDeferred === true && want.has(database.key) && reads[database.key]?.row !== database
    );
    if (!due.length) return;
    setReads((current) => {
      const next = { ...current };
      for (const database of due) next[database.key] = { phase: "loading", row: database };
      return next;
    });
    const signal = controllerRef.current?.signal ?? new AbortController().signal;
    for (const database of due) {
      void readDatabaseFields(connectorKey, database.key, signal).then((answer) => {
        if (signal.aborted) return;
        setReads((current) => {
          if (current[database.key]?.row !== database) return current;
          return {
            ...current,
            [database.key]: answer.ok
              ? { phase: "ready", row: database, fields: answer.fields, excluded: answer.excluded }
              : { phase: "error", row: database, message: answer.message },
          };
        });
      });
    }
  }, [connectorKey, served, wantedKey, reads]);
  const databases = useMemo(() => served.map((database) => {
    const read = reads[database.key];
    if (!database.fieldsDeferred || read?.phase !== "ready" || read.row !== database) return database;
    return { ...database, fields: read.fields, excluded: read.excluded, fieldsDeferred: false };
  }), [reads, served]);
  const readOf = useCallback((key: string): FieldRead | undefined => {
    const read = reads[key];
    const database = served.find((candidate) => candidate.key === key);
    return read && read.row === database ? read : undefined;
  }, [reads, served]);
  const retry = useCallback((databaseKey: string) => {
    setReads((current) => {
      const next = { ...current };
      delete next[databaseKey];
      return next;
    });
  }, []);
  return { databases, readOf, retry };
}

export function readableLag(seconds: number): string {
  if (seconds < 60) return `${seconds.toLocaleString()}s`;
  if (seconds < 3600) return `${Math.floor(seconds / 60).toLocaleString()}m`;
  if (seconds < 86400) return `${Math.floor(seconds / 3600).toLocaleString()}h`;
  return `${Math.floor(seconds / 86400).toLocaleString()}d`;
}

export function DatabaseSyncSummary({ health, error = "", errorCovered = false,
  databaseKey, catchUp, catchUpError = "", catchUpLoading = false, compact = false }: {
  health?: DatabaseSyncHealth;
  error?: string;
  errorCovered?: boolean;
  databaseKey?: string;
  catchUp?: ConnectorCatchUp | null;
  catchUpError?: string;
  catchUpLoading?: boolean;
  compact?: boolean;
}) {
  const newest = databaseKey ? catchUp?.newest[databaseKey] : undefined;
  const updating = catchUp?.active === true;
  if (!health && !updating && !newest && !catchUpError && !catchUpLoading) {
    return compact ? <span className="conn-db-state" title={error || undefined}>Unknown</span>
      : error ? <p className="conn-job-error">{error}</p> : null;
  }
  const stateLabel: Record<string, string> = {
    never_run: "Never run", queued: "Queued", running: "Running", succeeded: "Succeeded",
    partial: "Partial", failed: "Failed", disabled: "Disabled",
  };
  const state = updating ? "updating" : catchUpError ? "unknown"
    : catchUpLoading ? "loading" : health?.state;
  if (compact) return <span className="conn-job" data-sync-state={state}
    title={catchUpError || error || health?.error?.message || health?.warning || undefined}>
    {updating ? catchUpLabel(catchUp) : catchUpError ? "Unknown" : catchUpLoading ? "Checking sync"
      : health ? stateLabel[health.state] || health.state : "Unknown"}
  </span>;
  return <div className="conn-job" data-sync-state={state} aria-live="polite">
    <span>{updating ? catchUpLabel(catchUp) : catchUpError || (catchUpLoading ? "Checking sync"
      : health ? stateLabel[health.state] || health.state : "Sync status unavailable")}</span>
    {updating && catchUpError ? <span>{catchUpError}</span> : null}
    {health ? <>
      <span>{health.rowCount === null ? "Row count unavailable"
        : `${health.rowCount.toLocaleString()} rows`}</span>
      <span>{health.lastSuccess ? `Last success ${readableSyncStamp(health.lastSuccess)}`
        : "No successful run"}</span>
    </> : null}
    {newest ? <span className="conn-sync-newest">Newest data <time dateTime={newest}>
      {newest.length === 10 ? new Date(`${newest}T00:00:00`).toLocaleDateString(undefined, { dateStyle: "medium" })
        : readableSyncStamp(newest)}
    </time></span> : null}
    {health ? <>
      <span>{health.sourceTimestamp ? `Source ${readableSyncStamp(health.sourceTimestamp)}`
        : "Source timestamp unavailable"}</span>
      <span>{health.materializedTimestamp
        ? `Materialized ${readableSyncStamp(health.materializedTimestamp)}`
        : "Materialized timestamp unavailable"}</span>
      <span>{health.lagSeconds === null ? "Lag unavailable" : `Lag ${readableLag(health.lagSeconds)}`}</span>
      {health.error && !errorCovered ? <span className="conn-job-error">
        {health.error.code}: {health.error.message}
      </span> : null}
    </> : null}
  </div>;
}

/** Preserve C1 and the preceding release's refusal facts without capping reports. */
export function parseAmazonFirstLoad(value: unknown): AmazonFirstLoad | null {
  const load = syncObject(value);
  const plain = (text: unknown) => typeof text === "string"
    ? text.replace(/\u2014/g, ". ").replace(/\u2013/g, " to ") : "";
  const line = plain(load.line).trim();
  const remaining = syncCount(load.remaining);
  const etaSeconds = syncCount(load.etaSeconds);
  const cause = syncText(load.cause);
  const refused = Array.isArray(load.refused) ? load.refused.flatMap((item): AmazonRefusedReport[] => {
    const report = syncObject(item);
    const table = syncText(report.table);
    const label = plain(report.label);
    const reason = plain(report.reason);
    const amazonStatus = syncText(report.amazonStatus);
    return table && label && reason && ["CANCELLED", "FATAL", "NOT_OFFERED", "REFUSED"].includes(amazonStatus)
      ? [{ table, label, reason, amazonStatus }] : [];
  }) : [];
  const details = Array.isArray(load.details) ? load.details.flatMap((item): AmazonFirstLoad["details"] => {
    const report = syncObject(item);
    const table = typeof report.table === "string" ? report.table : "";
    const label = plain(report.label);
    const state = report.state;
    return table && label && (state === "loaded" || state === "loading" || state === "not_offered" ||
      state === "retrying" || state === "failed") && typeof report.reason === "string"
      ? [{ table, label, state, reason: plain(report.reason) }] : [];
  }) : [];
  if (!line && !refused.length && !(remaining !== null && remaining > 0)) return null;
  return { remaining, etaSeconds: etaSeconds ?? 0, cause,
    needsAttention: load.needsAttention === true || refused.length > 0, refused,
    line, etaDays: syncCount(load.etaDays), details };
}

/** Read C1 from the connection envelope or legacy database sync objects. */
export function parseAmazonSyncStates(value: unknown): Record<string, AmazonTableSync | null> | null {
  const payload = syncObject(value);
  if (syncObject(payload.overview).key !== "amazon_sp" || !Array.isArray(payload.databases)) return null;
  const states: Record<string, AmazonTableSync | null> = {};
  const connectionLoad = Object.prototype.hasOwnProperty.call(payload, "firstLoad")
    ? parseAmazonFirstLoad(payload.firstLoad) : undefined;
  const summaryLoad = parseAmazonFirstLoad(payload.amazonFirstLoad);
  for (const raw of payload.databases) {
    const database = syncObject(raw);
    if (typeof database.key !== "string" || !database.key.startsWith("ut_amzn_")) continue;
    const state = syncObject(database.sync);
    // An explicit connection-level null clears stale copies on database rows.
    const load = connectionLoad !== undefined ? connectionLoad
      : Object.prototype.hasOwnProperty.call(state, "firstLoad") ? parseAmazonFirstLoad(state.firstLoad) : summaryLoad;
    if ((!database.sync || typeof database.sync !== "object" || Array.isArray(database.sync)) && !load) {
      states[database.key] = null;
      continue;
    }
    const reason = syncText(syncObject(state.deferred).reason);
    const held = syncObject(state.held);
    const warning = syncText(state.warning).replace(/\u2014/g, ". ").replace(/\u2013/g, " to ").trim();
    states[database.key] = {
      rowsLoaded: syncCount(state.rowsLoaded), lastSuccess: syncStamp(state.lastSuccess),
      inProgress: state.inProgress === true,
      refused: state.refused === true,
      held: state.held && typeof state.held === "object" && !Array.isArray(state.held)
        ? { reason: syncText(held.reason).replace(/\u2014/g, ". ").replace(/\u2013/g, " to ").trim(),
          rows: syncCount(held.rows) } : null,
      deferred: reason ? { reason } : null, nextDue: syncStamp(state.nextDue),
      firstLoad: load,
      stuck: state.stuck === true ? true : state.stuck === false ? false : null,
      stuckCause: syncText(state.stuckCause), unknownCause: syncText(state.unknownCause),
      ...(warning ? { warning } : {}),
    };
  }
  return states;
}

const AMAZON_SYNC_PAGE_SIZE = 50;

/** Read the provider additions from summary pages; publish only a complete, unique walk. */
export async function readAmazonSyncStates(
  signal: AbortSignal,
): Promise<Record<string, AmazonTableSync | null>> {
  const states: Record<string, AmazonTableSync | null> = {};
  let offset: number | null = 0;
  let total: number | null = null;
  while (offset !== null) {
    if (signal.aborted) throw new Error("Sync status read cancelled");
    const response = await fetch(
      `${API_V1}/connectors/amazon_sp/detail` +
        `?databases_offset=${offset}&databases_limit=${AMAZON_SYNC_PAGE_SIZE}&fields=summary`,
      { credentials: CREDENTIALS, signal },
    );
    const raw: unknown = await response.json();
    if (signal.aborted) throw new Error("Sync status read cancelled");
    const next = response.ok ? parseAmazonSyncStates(raw) : null;
    const page = parseDatabasesPage(syncObject(raw).databasesPage);
    const databases = syncObject(raw).databases;
    if (!next || !page || page.offset !== offset || page.limit !== AMAZON_SYNC_PAGE_SIZE ||
        !Array.isArray(databases) || databases.length > AMAZON_SYNC_PAGE_SIZE ||
        (total !== null && page.total !== total) ||
        (page.next !== null && page.next >= page.total)) {
      throw new Error("Unreadable sync status");
    }
    total = page.total;
    for (const [key, state] of Object.entries(next)) {
      if (!Object.prototype.hasOwnProperty.call(states, key)) states[key] = state;
    }
    offset = page.next;
  }
  if (Object.keys(states).length !== total) throw new Error("Incomplete sync status");
  return states;
}

function useAmazonSyncStates(enabled: boolean, detail: ConnectorDetail, syncPhase?: string) {
  const [states, setStates] = useState<Record<string, AmazonTableSync | null>>(
    () => enabled ? parseAmazonSyncStates(detail) ?? {} : {}
  );
  const [error, setError] = useState("");
  useEffect(() => {
    setStates(enabled ? parseAmazonSyncStates(detail) ?? {} : {});
    setError("");
    if (!enabled) return;
    const controller = new AbortController();
    let reading = false;
    const poll = async () => {
      if (reading || controller.signal.aborted) return;
      reading = true;
      try {
        const next = await readAmazonSyncStates(controller.signal);
        if (controller.signal.aborted) return;
        setStates(next);
        setError("");
      } catch {
        if (controller.signal.aborted) return;
        setError("Sync status could not be read. Reload to check again.");
      } finally {
        reading = false;
      }
    };
    void poll();
    const timer = window.setInterval(() => {
      if (document.visibilityState !== "hidden") void poll();
    }, 5000);
    return () => { controller.abort(); window.clearInterval(timer); };
  }, [enabled, detail, syncPhase]);
  return { states, error };
}

export function readableSyncStamp(value: string): string {
  return new Date(value).toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" });
}

/** Keep reporting dates on their calendar day and format retry instants like other sync times. */
export function readableAmazonSyncText(value: string): string {
  return value.replace(/\u2014/g, ". ").replace(/\u2013/g, " to ").replace(
    /\b\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?)?\b/gi,
    (stamp) => {
      const day = stamp.slice(0, 10);
      const date = new Date(`${day}T00:00:00Z`);
      if (!Number.isFinite(date.getTime()) || date.toISOString().slice(0, 10) !== day) return "Date unavailable";
      if (stamp.length === 10) return date.toLocaleDateString(undefined, { dateStyle: "medium", timeZone: "UTC" });
      return Number.isFinite(Date.parse(stamp)) ? readableSyncStamp(stamp) : "Date unavailable";
    },
  );
}

function plural(count: number, unit: string): string {
  return `${count.toLocaleString()} ${unit}${count === 1 ? "" : "s"}`;
}

export function firstLoadEstimate(seconds: number): string {
  if (seconds < 60) return plural(seconds, "second");
  const minutes = Math.ceil(seconds / 60);
  if (minutes < 60) return plural(minutes, "minute");
  const hours = Math.floor(minutes / 60);
  const rest = minutes % 60;
  return rest ? `${plural(hours, "hour")} ${plural(rest, "minute")}` : plural(hours, "hour");
}

function amazonRefusalReason(reason: string, status = ""): string {
  const text = reason.trim();
  const lower = text.toLowerCase();
  // Mirrors plain_refusal_reason: a reconciliation hold is our own check, never a load failure.
  const reconciliation = /\breconciliation refused:\s*([\s\S]*)$/i.exec(text);
  if (reconciliation) {
    const detail = reconciliation[1].trim().replace(/[.\s]+$/, "").replace(/\u2014/g, ". ").replace(/\u2013/g, " to ");
    if (/^the check could not run/i.test(detail)) {
      return "The reconciliation check could not run. The last good rows are kept until it runs.";
    }
    return detail ? `Reconciliation did not tie: ${detail}. The last good rows are kept.`
      : "Reconciliation did not tie. The last good rows are kept.";
  }
  if (/within (?:the past )?two years/.test(lower)) {
    return "Amazon refused this date range. Choose a start date within the past two years and retry sync.";
  }
  if (/reportperiod|reporting period/.test(lower)) {
    return "Amazon needs a reporting period for this report. Check the date range and retry sync.";
  }
  if (status === "CANCELLED" || lower.includes("cancelled")) {
    return "Amazon cancelled this report. Retry sync to request it again.";
  }
  if (status === "FATAL" || lower.includes("fatal")) {
    return "Amazon could not prepare this report. Check its date range and seller eligibility, then retry sync.";
  }
  if (/\bAmazonThrottled\b|\bTHROTTLED:|\bthrottled\s*\(429\)|\bHTTP\s*429\b/i.test(text)) {
    return "Amazon is limiting requests. Wait, then retry sync.";
  }
  // Old containers and persisted snapshots can still deliver the producer's diagnostic sentence.
  if (!/REFUSED:|FATAL|\b\w*(?:Error|Exception)\b|GET_[A-Z_]+|[{}]|SQLSTATE|SELECT\s|report[ _-]?id\b|\bHTTP\s*(?:403|429)\b/i.test(text)) return text;
  if (/required role|missing.*role|not granted|needs the/.test(lower) && !/already holds/.test(lower)) {
    return "Amazon has not granted access to this report. Ask an administrator to check the required Amazon roles and reconnect Amazon.";
  }
  if (/\bHTTP\s*403\b|not allowed|already holds/.test(lower)) {
    return "Amazon does not offer this report to this seller account. Check seller eligibility in Seller Central.";
  }
  return "Amazon could not load this report. Retry sync; if it fails again, ask an administrator to review the Amazon connection.";
}

export function AmazonFirstLoadNotice({ states, databaseNames }: {
  states: Record<string, AmazonTableSync | null>; databaseNames: Record<string, string>;
}) {
  const load = Object.values(states).find((state) => state?.firstLoad)?.firstLoad;
  if (!load || load.remaining === 0 || (!load.line && !(load.remaining && load.remaining > 0))) return null;
  const line = readableAmazonSyncText(load.line || (load.etaDays != null
    ? `Loading history, about ${plural(load.etaDays, "day")} left` : "Loading history"));
  const reports: AmazonFirstLoad["details"] = load.details?.length ? load.details : (load.refused ?? []).map((report) => ({
    ...report, state: report.amazonStatus === "NOT_OFFERED" ? "not_offered" : "retrying",
  }));
  return <details className="conn-first-load">
    <summary aria-label="Details">
      <span className="conn-first-load-line" role="status">{line}</span>
      <span className="conn-db-link">Details</span>
    </summary>
    <ul className="conn-first-load-reports" aria-label="History reports">
      {reports.map((report, index) => <li key={`${report.table}:${index}`} data-report-state={report.state}>
        <div className="conn-first-load-report">
          <span>{databaseNames[`ut_amzn_${report.table}`] ?? report.label}</span>
          <span className="conn-first-load-state">{({ loaded: "Loaded", loading: "Loading",
            not_offered: "Not offered", retrying: "Retrying", failed: "Failed" })[report.state]}</span>
        </div>
        {report.reason ? <p>{readableAmazonSyncText(report.reason)}</p> : null}
      </li>)}
    </ul>
  </details>;
}

export function AmazonSyncChip({ state, error, databaseKey, compact = false }: {
  state?: AmazonTableSync | null; error?: string; databaseKey?: string; compact?: boolean;
}) {
  const report = state?.firstLoad?.refused?.find((item) => `ut_amzn_${item.table}` === databaseKey);
  const refusalReason = report ? amazonRefusalReason(report.reason, report.amazonStatus) : "";
  const unknownCause = state?.unknownCause ?? "";
  const cause = amazonRefusalReason(unknownCause.trim()).trim();
  const reason = refusalReason.trim();
  const refused = Boolean(report || state?.refused || /REFUSED:|\b(?:FATAL|CANCELLED|NOT_OFFERED)\b|Amazon (?:refused|cancelled|could not (?:prepare|load)|has not granted access|does not offer|is limiting requests|needs a reporting period)|This connector cannot request/i.test(unknownCause));
  const repeatsRefusal = Boolean(report && cause && reason &&
    (cause.startsWith(reason) || reason.startsWith(cause)));
  const held = state?.held ?? null;
  const phase = state?.inProgress ? "syncing" : state?.stuck ? "failed"
    : held ? "held" : refused ? "refused" : state?.deferred ? "deferred"
    : state?.lastSuccess || (state?.rowsLoaded ?? 0) > 0 ? "ready" : "unknown";
  if (compact) return <span className="conn-job" data-sync-state={phase}
    title={readableAmazonSyncText(held?.reason || refusalReason || state?.deferred?.reason || state?.stuckCause || cause || error || "") || undefined}>
    {({ syncing: "Syncing", failed: "Failed", held: "Held", refused: "Refused", deferred: "Deferred",
      ready: "Ready", unknown: "Unknown" })[phase]}
  </span>;
  return <div className="conn-job" data-sync-state={phase} aria-live="polite">
    <span className="conn-job-attempt">{held
      ? held.rows === null ? "Last good rows kept" : `${held.rows.toLocaleString()} rows kept from the last good sync`
      : state?.rowsLoaded === 0 && state.lastSuccess
      ? refused ? "No rows retained from the last successful sync."
        : "Loaded. Amazon returned no rows for this report."
      : state?.rowsLoaded == null || (state.rowsLoaded === 0 && !state.lastSuccess) ? "Not loaded yet"
      : `${state.rowsLoaded.toLocaleString()} rows loaded`}</span>
    {held ? <span className="conn-job-error">Held: {readableAmazonSyncText(held.reason || "this table waits for its next good sync.")}</span> : null}
    {report ? <span className="conn-job-error">Amazon refused this report: {readableAmazonSyncText(refusalReason)}</span> : null}
    {state?.lastSuccess ? <span>Last success {readableSyncStamp(state.lastSuccess)}</span> : null}
    {state?.inProgress ? <span>Syncing</span> : null}
    {state?.deferred ? <span className="conn-job-error">Deferred: {readableAmazonSyncText(state.deferred.reason)}</span> : null}
    {state?.nextDue ? <span>Next due {readableSyncStamp(state.nextDue)}</span> : null}
    {state?.stuckCause ? <span className="conn-job-error">{readableAmazonSyncText(state.stuckCause)}. Retry sync to refresh progress.</span> : null}
    {unknownCause && !repeatsRefusal ? <span className="conn-job-error">{readableAmazonSyncText(cause)}</span> : null}
    {!state ? <span className="conn-job-error">{readableAmazonSyncText(error || "Sync status is unknown in this container. Reload to check again.")}</span> : null}
  </div>;
}

interface DependentItem { id: string; label: string; href: string | null; reason: string }
interface DependentKind {
  kind: string; label: string; count: number; ids: string[];
  items?: DependentItem[]; href?: string | null; reason?: string;
}

function dependentHref(value: unknown): string | null {
  return typeof value === "string"
    && (value.startsWith("#/table?") || value.startsWith("#/data?") || value.startsWith("#/sheet?"))
    ? value : null;
}

function parseDependents(value: unknown): DependentKind[] {
  if (!Array.isArray(value)) return [];
  return value.flatMap((entry: unknown): DependentKind[] => {
    if (!entry || typeof entry !== "object") return [];
    const row = entry as Record<string, unknown>;
    if (typeof row.kind !== "string" || typeof row.label !== "string" ||
        typeof row.count !== "number" || !Number.isFinite(row.count) || row.count <= 0 ||
        !Array.isArray(row.ids) || !row.ids.every((id) => typeof id === "string")) return [];
    const dependent: DependentKind = {
      kind: row.kind, label: row.label, count: row.count, ids: row.ids,
    };
    if ("href" in row) dependent.href = dependentHref(row.href);
    if (typeof row.reason === "string") dependent.reason = row.reason;
    if (Array.isArray(row.items)) {
      dependent.items = row.items.flatMap((raw: unknown): DependentItem[] => {
        if (!raw || typeof raw !== "object") return [];
        const item = raw as Record<string, unknown>;
        if (typeof item.id !== "string" || typeof item.label !== "string" ||
            typeof item.reason !== "string" ||
            !(item.href === null || typeof item.href === "string")) return [];
        return [{ id: item.id, label: item.label, href: dependentHref(item.href), reason: item.reason }];
      });
    }
    return [dependent];
  });
}

function dependentRows(dependent: DependentKind): DependentItem[] {
  if (dependent.items?.length) return dependent.items;
  // Lane A may supply its workbook address and name on the dependent itself instead
  // of an items list. Render that shape too without guessing any Sheet address.
  if ("href" in dependent) return [{
    id: dependent.ids.join(":"), label: dependent.label, href: dependent.href ?? null,
    reason: dependent.reason || "This dependent has no direct address available.",
  }];
  return dependent.ids.map((id) => ({
    id, label: id, href: null,
    reason: dependent.reason || "This dependent has no direct address available.",
  }));
}

/** What `GET /dependents` said, or why it could not say anything. */
type DependentsAnswer =
  | { ok: true; refusal: string; dependents: DependentKind[] }
  | { ok: false; message: string };

/**
 * C3's door, read verbatim.
 *
 * ⛔ `refusal` IS TAKEN AS A STRING AND SHOWN AS ONE. The payload also carries a `dependents`
 * array with per-kind counts, and re-deriving the sentence from it is the tempting shape: it is
 * also how a client starts printing "and 0 Sheet cells" about a provider that is not registered.
 */
async function readDependents(tableKey: string): Promise<DependentsAnswer> {
  try {
    const res = await fetch(
      `${API_V1}/dependents?kind=table&key=${encodeURIComponent(tableKey)}`,
      { credentials: CREDENTIALS }
    );
    const body = (await res.json().catch(() => null)) as
      | { refusal?: unknown; dependents?: unknown; error?: { message?: string } }
      | null;
    if (!res.ok) {
      return { ok: false, message: body?.error?.message || `The server answered ${res.status}.` };
    }
    return {
      ok: true, refusal: typeof body?.refusal === "string" ? body.refusal : "",
      dependents: parseDependents(body?.dependents),
    };
  } catch {
    return { ok: false, message: "Cannot reach the server." };
  }
}

async function write(path: string, method: "PUT" | "PATCH" | "POST", body: unknown): Promise<string> {
  try {
    const res = await fetch(`${API_V1}${path}`, {
      method,
      credentials: CREDENTIALS,
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(body ?? {}),
    });
    const payload = (await res.json().catch(() => null)) as
      | { error?: { message?: string } }
      | null;
    if (!res.ok) return payload?.error?.message || `The server answered ${res.status}.`;
    return "";
  } catch {
    return "Cannot reach the server.";
  }
}

export type SyncNowReceipt = {
  provider: string;
  state: "idle" | "queued" | "running" | "done" | "failed";
  detail: string;
};

async function readSyncNow(path: string, provider: string): Promise<
  { receipt: SyncNowReceipt; message: "" } | { receipt: null; message: string }
> {
  try {
    const res = await fetch(`${API_V1}${path}`, { credentials: CREDENTIALS });
    const payload = await res.json().catch(() => null) as
      | { provider?: unknown; state?: unknown; detail?: unknown; error?: { message?: string } }
      | null;
    if (!res.ok) return { receipt: null, message: payload?.error?.message || `The server answered ${res.status}.` };
    if (payload?.provider !== provider || typeof payload.state !== "string" ||
        !["idle", "queued", "running", "done", "failed"].includes(payload.state) ||
        typeof payload.detail !== "string") {
      return { receipt: null, message: "The server returned an unreadable sync status." };
    }
    return { receipt: { provider, state: payload.state as SyncNowReceipt["state"], detail: payload.detail }, message: "" };
  } catch {
    return { receipt: null, message: "Cannot reach the server to check sync progress." };
  }
}

export function useSyncNow(capability: ConnectorDetail["syncNow"]) {
  const databaseRefusal = useRefusalToast();
  const path = capability?.path;
  const provider = capability?.provider;
  const [receipt, setReceipt] = useState<SyncNowReceipt | null>(null);
  const [message, setMessage] = useState("");
  const [requestError, setRequestError] = useState("");
  const [posting, setPosting] = useState(false);
  const [reads, setReads] = useState(0);
  const postInFlight = useRef(false);
  const readGeneration = useRef(0);
  useEffect(() => {
    if (!path || !provider) return;
    const generation = ++readGeneration.current;
    let stopped = false;
    let reading = false;
    const poll = async () => {
      if (reading || stopped || generation !== readGeneration.current) return;
      reading = true;
      const answer = await readSyncNow(path, provider);
      reading = false;
      if (stopped || generation !== readGeneration.current) return;
      setMessage(answer.message);
      setPosting(false);
      if (!answer.receipt) return; // A failed read does not prove that the run finished.
      setReceipt(answer.receipt);
      if (!["queued", "running"].includes(answer.receipt.state)) window.clearInterval(timer);
    };
    const timer = window.setInterval(() => void poll(), 1500);
    void poll();
    return () => { stopped = true; readGeneration.current += 1; window.clearInterval(timer); };
  }, [path, provider, reads]);
  const active = receipt !== null && receipt.provider === provider &&
    (receipt.state === "queued" || receipt.state === "running");
  const start = async () => {
    if (!path || active || postInFlight.current) return;
    const generation = ++readGeneration.current;
    postInFlight.current = true;
    setPosting(true);
    setReceipt(null);
    setMessage("");
    setRequestError("");
    const error = await write(path, "POST", {});
    postInFlight.current = false;
    if (generation !== readGeneration.current) return;
    setRequestError("");
    if (error) databaseRefusal(error);
    // Read after every response, including an uncertain network failure. The server may have
    // accepted the intent already. Only a readback may claim that it is queued or finished.
    setReads((value) => value + 1);
  };
  return { receipt: receipt?.provider === provider ? receipt : null, message: requestError || message,
    busy: Boolean(active || posting), start };
}

/**
 * Enabling sets the flag and THEN materialises, so a refusal can still have changed the server.
 *
 * ⛔ THE ANSWER IS A MESSAGE, NOT A FLAG READ OFF AN ERROR BODY. `deps.err` is the one non-2xx
 * shape and it carries `error` alone. `reread` is therefore unconditional below: every outcome of
 * this door can change what `/detail` answers, so the screen re-reads rather than trusting a
 * sibling key that the error contract does not have.
 */
type EnableDatabaseAnswer =
  | {
      ok: true;
      workspaceChanged: boolean;
      populatedFieldsEnabled: number;
      fieldCensusComplete: boolean;
      autoEnableAlsoAffects: Array<{ key: string; label: string }>;
      schemaFields: number;
      state: string;
    }
  | { ok: false; message: string };

async function enableDatabase(tableKey: string): Promise<EnableDatabaseAnswer> {
  try {
    const res = await fetch(
      `${API_V1}/connectors/odoo/databases/${encodeURIComponent(tableKey)}/enable`,
      { method: "POST", credentials: CREDENTIALS }
    );
    const payload = (await res.json().catch(() => null)) as
      | {
          workspaceChanged?: boolean;
          schemaFields?: number;
          state?: string;
          populatedFieldsEnabled?: number;
          fieldCensusComplete?: boolean;
          autoEnableAlsoAffects?: unknown;
          error?: { message?: string };
        }
      | null;
    if (res.ok) {
      const shared = Array.isArray(payload?.autoEnableAlsoAffects)
        ? payload.autoEnableAlsoAffects.flatMap((value) => {
            if (!value || typeof value !== "object") return [];
            const row = value as Record<string, unknown>;
            const key = typeof row.key === "string" ? row.key : "";
            const label = typeof row.label === "string" ? row.label : "";
            return key && label ? [{ key, label }] : [];
          })
        : [];
      const count = payload?.populatedFieldsEnabled;
      return {
        ok: true,
        workspaceChanged: payload?.workspaceChanged === true,
        schemaFields: typeof payload?.schemaFields === "number" ? payload.schemaFields : 0,
        state: typeof payload?.state === "string" ? payload.state : "",
        populatedFieldsEnabled:
          typeof count === "number" && Number.isFinite(count) ? Math.max(0, Math.trunc(count)) : 0,
        fieldCensusComplete: payload?.fieldCensusComplete === true,
        autoEnableAlsoAffects: shared,
      };
    }
    return {
      ok: false,
      message: payload?.error?.message || `The server answered ${res.status}.`,
    };
  } catch {
    return { ok: false, message: "Cannot reach the server." };
  }
}

async function disableDatabase(
  tableKey: string
): Promise<{ ok: true } | { ok: false; message: string }> {
  try {
    const res = await fetch(
      `${API_V1}/connectors/odoo/databases/${encodeURIComponent(tableKey)}/disable`,
      { method: "POST", credentials: CREDENTIALS }
    );
    const payload = (await res.json().catch(() => null)) as
      | { error?: { message?: string } }
      | null;
    if (res.ok) return { ok: true };
    return {
      ok: false,
      message: payload?.error?.message || `The server answered ${res.status}.`,
    };
  } catch {
    return { ok: false, message: "Cannot reach the server." };
  }
}

function sentenceList(values: string[]): string {
  if (values.length < 2) return values[0] || "";
  if (values.length === 2) return `${values[0]} and ${values[1]}`;
  return `${values.slice(0, -1).join(", ")}, and ${values[values.length - 1]}`;
}

function autoEnableResult(answer: Extract<EnableDatabaseAnswer, { ok: true }>): string {
  if (answer.state) return `${answer.schemaFields.toLocaleString()} fields available. Row sync ${answer.state}.`;
  const siblings = sentenceList(answer.autoEnableAlsoAffects.map((row) => row.label));
  const unknown = answer.fieldCensusComplete
    ? ""
    : ", while fields that could not be counted were left unchanged";
  if (!answer.populatedFieldsEnabled) {
    return siblings
      ? `No populated fields were found, so no field settings were changed here or in ${siblings}${unknown}.`
      : `No populated fields were found, so no field settings were changed${unknown}.`;
  }
  const fields = answer.populatedFieldsEnabled === 1
    ? "One populated field was"
    : `${answer.populatedFieldsEnabled.toLocaleString()} populated fields were`;
  return siblings
    ? `${fields} switched on here and in ${siblings} because the databases read the same Odoo source table${unknown}.`
    : `${fields} switched on${unknown}.`;
}

export interface DatabaseReveal {
  key: string;
  status: string;
}

export interface DatabasesTabHandle {
  savePending: () => Promise<void>;
  discardPending: () => void;
  stageTable: (database: ConnectorDatabase, next: boolean) => Promise<void>;
  tableEnabled: (database: ConnectorDatabase) => boolean;
}

interface PendingDatabaseChanges {
  tables: Record<string, boolean>;
  fields: Record<string, boolean>;
  names: Record<string, string>;
}

const EMPTY_PENDING: PendingDatabaseChanges = { tables: {}, fields: {}, names: {} };

function pendingChangeCount(pending: PendingDatabaseChanges): number {
  return Object.keys(pending.tables).length +
    Object.keys(pending.fields).length +
    Object.keys(pending.names).length;
}

type PopulationCountAnswer =
  | {
      ok: true;
      complete: boolean;
      capturedAt: string;
      durationMs: number;
      modelsCounted: number;
      modelsWithRows: number;
      mode: string;
      exactCounts: Record<string, number>;
      errors: string[];
    }
  | { ok: false; message: string };

/** Both passes start on a click. Connector detail never starts this work. */
async function countSourceRows(exact = false): Promise<PopulationCountAnswer> {
  try {
    const res = await fetch(`${API_V1}/connectors/odoo/census${exact ? "?exact=true" : ""}`, {
      method: "POST",
      credentials: CREDENTIALS,
    });
    const payload = (await res.json().catch(() => null)) as Record<string, unknown> | null;
    if (!res.ok) {
      const error = payload?.error as { message?: unknown } | undefined;
      return {
        ok: false,
        message: typeof error?.message === "string"
          ? error.message
          : `The server answered ${res.status}.`,
      };
    }
    const number = (value: unknown) =>
      typeof value === "number" && Number.isFinite(value)
        ? Math.max(0, Math.trunc(value))
        : 0;
    const rawErrors = payload?.errors;
    const mode = typeof payload?.mode === "string" ? payload.mode : "unknown";
    const exactCounts: Record<string, number> = {};
    if ((mode === "exact" || mode === "exact_fallback") && payload?.counts &&
        typeof payload.counts === "object" && !Array.isArray(payload.counts)) {
      for (const [model, value] of Object.entries(payload.counts)) {
        if (typeof value === "number" && Number.isInteger(value) && value >= 0) {
          exactCounts[model] = value;
        }
      }
    }
    return {
      ok: true,
      mode,
      exactCounts,
      complete: payload?.complete === true,
      capturedAt: typeof payload?.capturedAt === "string" ? payload.capturedAt : "",
      durationMs: number(payload?.durationMs),
      modelsCounted: number(payload?.modelsCounted),
      modelsWithRows: number(payload?.modelsWithRows),
      errors: Array.isArray(rawErrors)
        ? rawErrors.filter((value): value is string => typeof value === "string")
        : [],
    };
  } catch {
    return { ok: false, message: "Cannot reach the server." };
  }
}

function populationSummary(modelsCounted: number, modelsWithRows: number, durationMs: number) {
  const seconds = Math.max(1, Math.round(durationMs / 1000));
  return `Counted ${modelsCounted.toLocaleString()} source tables in ${seconds.toLocaleString()} ` +
    `seconds. ${modelsWithRows.toLocaleString()} hold data.`;
}

/** One class per tri-state, so the CSS owns the emphasis and no style prop competes with it. */
function populationClass(populated: boolean | null): string {
  if (populated === true) return "conn-db-row is-populated";
  if (populated === false) return "conn-db-row is-empty";
  return "conn-db-row is-uncounted";
}

function populationTime(capturedAt: string) {
  const millis = Date.parse(capturedAt);
  return Number.isFinite(millis) ? new Date(millis).toLocaleString() : "";
}

function populationResult(
  modelsCounted: number,
  modelsWithRows: number,
  durationMs: number,
  capturedAt: string,
  complete: boolean
) {
  const summary = populationSummary(modelsCounted, modelsWithRows, durationMs);
  const countedAt = populationTime(capturedAt);
  return summary + (countedAt ? ` Counted on ${countedAt}.` : "") +
    (complete ? "" : " The count was incomplete because some source tables could not be read.");
}

function sourceRowsTitle(exactCount: number | undefined): string {
  const explanation = "The display threshold is 100 for speed and does not limit data pulled from the source";
  return typeof exactCount === "number"
    ? `Exact total: ${exactCount.toLocaleString("en-US")} rows. ${explanation}.`
    : `${explanation}; use Exact totals beside Count rows for the full total.`;
}

/** The on/off control, one shape for both rails (DESIGN.md section 4: one control shape). */
function Switch({
  on,
  label,
  busy,
  onFlip,
}: {
  on: boolean;
  /** The accessible name. No dash, ever, including here (standing rule 2). */
  label: string;
  busy: boolean;
  onFlip: (next: boolean) => void;
}) {
  return (
    <button
      type="button"
      role="switch"
      aria-checked={on}
      aria-label={label}
      className={"conn-db-switch" + (on ? " is-on" : "")}
      disabled={busy}
      onClick={() => onFlip(!on)}
    >
      <span className="conn-db-knob" aria-hidden="true" />
    </button>
  );
}

/**
 * ⭐ R24's OTHER SIDE, and it is a real branch rather than an empty list.
 *
 * A scraper, Meta Ads or a catalogue listing keeps what its CARD says: the connector's own
 * description, plus the server's sentence for why there is no schema behind it. No toggles are
 * rendered at all — not disabled ones — because this directory's rule is that a reader who may
 * not do a thing sees no door for it rather than one that refuses.
 */
export function DatabasesUnavailable({ row, reason }: { row: ConnectorRow; reason: string }) {
  return (
    <div className="conn-db">
      {row.desc ? <p className="conn-db-desc">{row.desc}</p> : null}
      <p className="conn-db-note">
        {reason || "This connector does not publish a field catalogue."}
      </p>
    </div>
  );
}

interface ScraperPreview {
  recipe: NonNullable<ConnectorDatabase["scraperRecipe"]>;
  provider: string;
  credentialReady: boolean;
  estimatedUsd: number;
  costCapUsd: number;
  recordsCap: number;
  canRun: boolean;
  note: string;
}

function apiError(payload: unknown, status: number): string {
  const root = syncObject(payload);
  const direct = syncText(syncObject(root.error).message);
  const nested = syncText(syncObject(syncObject(root.detail).error).message);
  return direct || nested || `The server answered ${status}.`;
}

async function scraperPost(path: string, body: unknown): Promise<
  { ok: true; payload: Record<string, unknown> } | { ok: false; message: string }
> {
  try {
    const response = await fetch(`${API_V1}${path}`, {
      method: "POST", credentials: CREDENTIALS,
      headers: { "Content-Type": "application/json" }, body: JSON.stringify(body ?? {}),
    });
    const payload: unknown = await response.json().catch(() => null);
    if (!response.ok) return { ok: false, message: apiError(payload, response.status) };
    return { ok: true, payload: syncObject(payload) };
  } catch {
    return { ok: false, message: "Cannot reach the server." };
  }
}

function parseScraperPreview(payload: Record<string, unknown>): ScraperPreview | null {
  const recipeRaw = syncObject(payload.recipe);
  const seedRaw = recipeRaw.seed;
  const limitsRaw = syncObject(recipeRaw.limits);
  const scheduleRaw = syncObject(recipeRaw.schedule);
  const kind = syncText(recipeRaw.kind);
  const source = syncText(recipeRaw.source);
  const seed = typeof seedRaw === "string" ? seedRaw : {
    search: syncText(syncObject(seedRaw).search), geography: syncText(syncObject(seedRaw).geography),
  };
  if (!kind || !source || !syncText(scheduleRaw.mode) ||
      (typeof seed !== "string" && (!seed.search || !seed.geography))) return null;
  const recipe: NonNullable<ConnectorDatabase["scraperRecipe"]> = {
    kind, source, seed,
    limits: {
      ...(typeof limitsRaw.posts === "number" ? { posts: limitsRaw.posts } : {}),
      ...(syncText(limitsRaw.postType) ? { postType: syncText(limitsRaw.postType) } : {}),
      ...(typeof limitsRaw.records === "number" ? { records: limitsRaw.records } : {}),
    },
    schedule: { mode: syncText(scheduleRaw.mode) },
  };
  if (typeof payload.estimatedUsd !== "number" || typeof payload.costCapUsd !== "number" ||
      typeof payload.recordsCap !== "number") return null;
  return {
    recipe, provider: syncText(payload.provider), credentialReady: payload.credentialReady === true,
    estimatedUsd: payload.estimatedUsd, costCapUsd: payload.costCapUsd,
    recordsCap: payload.recordsCap, canRun: payload.canRun === true, note: syncText(payload.note),
  };
}

/**
 * S66-T39 / D-723 — WHILE THE LIST IS STILL BEING PAGED IN, IT SAYS SO. A partial list with no
 * sentence under it reads as the whole list, which is the short-count lie paging could introduce.
 * A failed page gets a way to read the list again, so no database becomes unreachable.
 */
function DatabasesPagingNote({ paging, shown, total, onRetry }: {
  paging?: "loading" | "failed";
  shown?: number;
  total?: number;
  onRetry: () => void;
}) {
  if (!paging) return null;
  const counted = typeof shown === "number" && typeof total === "number" && total > shown
    ? ` Showing ${shown.toLocaleString("en-US")} of ${total.toLocaleString("en-US")}.` : "";
  if (paging === "loading") {
    return (
      <p className="conn-db-note" role="status" aria-live="polite">
        More databases are still loading.{counted}
      </p>
    );
  }
  return (
    <div className="conn-modal-error">
      <p className="conn-db-error" role="alert">
        Some databases could not be read, so this list is incomplete.{counted}
      </p>
      <button className="conn-modal-retry" type="button" onClick={onRetry}>
        Try again
      </button>
    </div>
  );
}

type ScraperDatabaseTab = "schema" | "settings" | "history";

function ScraperDatabases({
  databases,
  syncStates,
  syncError,
  onChanged,
  onBusyChange,
  fieldReadError = () => "",
  onRetryFields,
  paging,
}: {
  databases: ConnectorDatabase[];
  syncStates: Record<string, DatabaseSyncHealth>;
  syncError: string;
  onChanged: (reveal?: DatabaseReveal) => void;
  onBusyChange: (busy: boolean) => void;
  /** S66-T39: a deferred schema read's failure for one database, or "". */
  fieldReadError?: (databaseKey: string) => string;
  onRetryFields?: (databaseKey: string) => void;
  paging?: "loading" | "failed";
}) {
  const [creating, setCreating] = useState(false);
  const [kind, setKind] = useState("instagram");
  const [label, setLabel] = useState("");
  const [seed, setSeed] = useState("");
  const [search, setSearch] = useState("");
  const [geography, setGeography] = useState("");
  const [limit, setLimit] = useState("12");
  const [postType, setPostType] = useState("video");
  const [preview, setPreview] = useState<ScraperPreview | null>(null);
  const [busy, setBusy] = useState("");
  const databaseRefusal = useRefusalToast();
  const [error, setError] = useState("");
  const [status, setStatus] = useState("");
  const [tabs, setTabs] = useState<Record<string, ScraperDatabaseTab>>({});

  useEffect(() => {
    onBusyChange(Boolean(busy));
    return () => onBusyChange(false);
  }, [busy, onBusyChange]);

  const resetPreview = () => { setPreview(null); setError(""); setStatus(""); };
  const recipePayload = () => kind === "google_maps"
    ? { kind, source: "places", seed: { search, geography }, limits: { records: limit }, schedule: { mode: "manual" } }
    : { kind, source: "profile", seed, limits: { posts: limit, postType }, schedule: { mode: "manual" } };

  const requestPreview = async () => {
    setBusy("preview"); setError(""); setStatus("");
    const answer = await scraperPost("/connectors/scraper/databases/preview", { recipe: recipePayload() });
    setBusy("");
    if (answer.ok === false) { setPreview(null); setError(answer.message); return; }
    const parsed = parseScraperPreview(answer.payload);
    if (!parsed) { setPreview(null); setError("The server returned an unreadable setup preview."); return; }
    setPreview(parsed);
  };

  const createDatabase = async () => {
    if (!preview) return;
    setBusy("create"); setError(""); setStatus("");
    const answer = await scraperPost("/connectors/scraper/databases", { label, recipe: preview.recipe });
    setBusy("");
    if (answer.ok === false) { databaseRefusal(answer.message); return; }
    const key = syncText(answer.payload.key);
    if (!key) { databaseRefusal("The server created no readable database."); return; }
    setCreating(false); setPreview(null); setLabel(""); setSeed(""); setSearch(""); setGeography("");
    setStatus("Database created. No provider was called and no cost was incurred.");
    signal(WORKSPACE_STALE_EVENT);
    onChanged({ key, status: "Recipe saved. Choose Run now when you are ready to use the provider." });
  };

  const runDatabase = async (database: ConnectorDatabase) => {
    if (!database.runPath || !database.canRun) return;
    setBusy(database.key); setError(""); setStatus("");
    const answer = await scraperPost(database.runPath, {});
    setBusy("");
    if (answer.ok === false) { databaseRefusal(answer.message); return; }
    const state = syncText(answer.payload.state) || "finished";
    setStatus(`${database.label} run ${state}. Run history has been refreshed.`);
    signal(WORKSPACE_STALE_EVENT);
    onChanged({ key: database.key, status: `${database.label} run ${state}.` });
  };

  return (
    <div className="conn-db">
      <div className="conn-db-head">
        <div>
          <p className="conn-db-lede">Saved scraper databases</p>
          <p className="conn-db-note">
            Creating a database saves its recipe for $0. Credentials stay in Setup and are
            never stored with the recipe. A provider is contacted only after you choose Run now.
          </p>
        </div>
        <button type="button" className="conn-db-count-action" disabled={Boolean(busy)}
          onClick={() => { setCreating((value) => !value); resetPreview(); }}>
          {creating ? "Cancel" : "Create database"}
        </button>
      </div>
      {error ? <p className="conn-db-error" role="alert">{error}</p> : null}
      {status ? <p className="conn-db-note" role="status">{status}</p> : null}
      {creating ? (
        <div className="conn-kc-form">
          <div className="conn-kc-field">
            <label htmlFor="scraper-database-name">Database name</label>
            <input id="scraper-database-name" value={label} disabled={Boolean(busy)} maxLength={80}
              onChange={(event) => { setLabel(event.target.value); resetPreview(); }} />
          </div>
          <div className="conn-kc-field">
            <label htmlFor="scraper-recipe-kind">Recipe</label>
            <select id="scraper-recipe-kind" value={kind} disabled={Boolean(busy)}
              onChange={(event) => { setKind(event.target.value); setPostType("video"); resetPreview(); }}>
              <option value="instagram">Instagram profile posts</option>
              <option value="tiktok">TikTok profile videos</option>
              <option value="google_maps">Google Maps places</option>
            </select>
          </div>
          {kind === "google_maps" ? <>
            <div className="conn-kc-field">
              <label htmlFor="scraper-maps-search">Category or search</label>
              <input id="scraper-maps-search" value={search} disabled={Boolean(busy)}
                onChange={(event) => { setSearch(event.target.value); resetPreview(); }} />
              <button type="button" className="conn-db-link" disabled={Boolean(busy)}
                onClick={() => { setSearch("florist"); resetPreview(); }}>Use florist preset</button>
            </div>
            <div className="conn-kc-field">
              <label htmlFor="scraper-maps-geography">Geography</label>
              <input id="scraper-maps-geography" value={geography} disabled={Boolean(busy)}
                placeholder="City, region, or country"
                onChange={(event) => { setGeography(event.target.value); resetPreview(); }} />
            </div>
          </> : (
            <div className="conn-kc-field">
              <label htmlFor="scraper-profile-seed">Profile handle or URL</label>
              <input id="scraper-profile-seed" value={seed} disabled={Boolean(busy)}
                onChange={(event) => { setSeed(event.target.value); resetPreview(); }} />
            </div>
          )}
          <div className="conn-kc-field">
            <label htmlFor="scraper-result-limit">
              {kind === "google_maps" ? "Result cap" : "Last-N post limit"}
            </label>
            <input id="scraper-result-limit" inputMode="numeric" value={limit} disabled={Boolean(busy)}
              onChange={(event) => { setLimit(event.target.value); resetPreview(); }} />
          </div>
          {kind === "instagram" ? (
            <div className="conn-kc-field">
              <label htmlFor="scraper-post-type">Post type</label>
              <select id="scraper-post-type" value={postType} disabled={Boolean(busy)}
                onChange={(event) => { setPostType(event.target.value); resetPreview(); }}>
                <option value="video">Video</option><option value="image">Image</option>
                <option value="carousel">Carousel</option>
              </select>
            </div>
          ) : null}
          <div className="conn-kc-actions">
            <button type="button" className="conn-kc-btn" disabled={Boolean(busy)}
              onClick={() => void requestPreview()}>{busy === "preview" ? "Checking..." : "Preview setup"}</button>
            <button type="button" className="conn-kc-btn is-primary"
              disabled={Boolean(busy) || !preview} onClick={() => void createDatabase()}>
              {busy === "create" ? "Creating..." : "Create database"}
            </button>
          </div>
          {preview ? (
            <dl className="conn-modal-rows" aria-label="Scraper setup preview">
              <dt>Inputs</dt><dd>{typeof preview.recipe.seed === "string" ? preview.recipe.seed
                : `${preview.recipe.seed.search} in ${preview.recipe.seed.geography}`}</dd>
              <dt>Provider</dt><dd>{preview.provider || "No provider declared"}</dd>
              <dt>Estimated cost</dt><dd>${preview.estimatedUsd.toFixed(4)}</dd>
              <dt>Hard cap</dt><dd>{preview.recordsCap.toLocaleString()} records, up to ${preview.costCapUsd.toFixed(4)}</dd>
              <dt>Credential</dt><dd>{preview.credentialReady
                ? "Ready in Setup"
                : "Missing. Add it in Setup before Run now."}</dd>
            </dl>
          ) : null}
        </div>
      ) : null}
      {!databases.length && !creating && !paging ? (
        <p className="conn-db-note">No scraper databases have been created in this workspace.</p>
      ) : null}
      <DatabasesPagingNote paging={paging} onRetry={() => onChanged()} />

      {databases.map((database) => {
        const active = tabs[database.key] || "schema";
        const recipe = database.scraperRecipe;
        if (!recipe) return null;
        return (
          <section key={database.key} className="conn-db-excluded" aria-labelledby={`scraper-${database.key}`}>
            <div className="conn-db-head">
              <div><h3 id={`scraper-${database.key}`}>{database.label}</h3>
                <p className="conn-db-note">{recipe.kind === "google_maps" ? "Google Maps" : recipe.kind} recipe</p></div>
              {database.canRun ? (
                <button type="button" className="conn-modal-go" disabled={Boolean(busy)}
                  onClick={() => void runDatabase(database)}>
                  {busy === database.key ? "Running..." : "Run now"}
                </button>
              ) : <span className="conn-db-state">Run now is unavailable for this saved recipe.</span>}
            </div>
            <DatabaseSyncSummary health={syncStates[database.key]} error={syncError} />
            <div className="conn-modal-tabs" role="tablist" aria-label={`${database.label} database sections`}>
              {(["schema", "settings", "history"] as ScraperDatabaseTab[]).map((tab) => (
                <button type="button" role="tab" className="conn-modal-tab" key={tab}
                  aria-selected={active === tab}
                  onClick={() => setTabs((current) => ({ ...current, [database.key]: tab }))}>
                  {tab === "schema" ? "Schema" : tab === "settings" ? "Scraper settings" : "Run history"}
                </button>
              ))}
            </div>
            {active === "schema" && database.fieldsDeferred ? (
              fieldReadError(database.key) ? (
                <div className="conn-modal-error">
                  <p className="conn-db-error" role="alert">{fieldReadError(database.key)}</p>
                  {onRetryFields ? (
                    <button className="conn-modal-retry" type="button"
                      onClick={() => onRetryFields(database.key)}>Try again</button>
                  ) : null}
                </div>
              ) : (
                <p className="conn-db-note" role="status" aria-live="polite">
                  Reading the columns for this database.
                </p>
              )
            ) : active === "schema" ? (
              <table className="conn-db-fields"><thead><tr><th>Column</th><th>Type</th></tr></thead>
                <tbody>{database.fields.map((field) => <tr key={field.key}><td>{field.label}</td><td>{field.type}</td></tr>)}</tbody>
              </table>
            ) : active === "settings" ? (
              <dl className="conn-modal-rows">
                <dt>Source</dt><dd>{recipe.source}</dd>
                <dt>Seed</dt><dd>{typeof recipe.seed === "string" ? recipe.seed
                  : `${recipe.seed.search} in ${recipe.seed.geography}`}</dd>
                <dt>Limit</dt><dd>{recipe.limits.posts ?? recipe.limits.records ?? 0}</dd>
                <dt>Post type</dt><dd>{recipe.limits.postType || "Not applicable"}</dd>
                <dt>Schedule</dt><dd>{recipe.schedule.mode === "manual" ? "Manual, Run now only" : recipe.schedule.mode}</dd>
              </dl>
            ) : database.scraperRuns?.length ? (
              <table className="conn-db-fields"><thead><tr><th>Finished</th><th>State</th><th>Provider</th><th>Rows</th><th>Estimate</th></tr></thead>
                <tbody>{database.scraperRuns.map((run) => <tr key={run.id}>
                  <td>{Number.isFinite(Date.parse(run.finishedAt)) ? new Date(run.finishedAt).toLocaleString() : run.finishedAt}</td>
                  <td>{run.state}</td><td>{run.provider}</td><td>{run.rows.toLocaleString()}</td>
                  <td>${run.estimatedUsd.toFixed(4)}</td></tr>)}</tbody>
              </table>
            ) : <p className="conn-db-note">No runs yet. Creating the database did not contact a provider.</p>}
          </section>
        );
      })}
    </div>
  );
}

/** One database's disclosure: its fields, and what the catalogue left out. */
/**
 * ⭐ W53-T27 — THE TENANT'S OWN NAME FOR ONE DATABASE, and the way back to ours.
 *
 * ⛔ PURE OVER PROPS, DRAFT INCLUDED. The text being typed lives in the tab's own state rather
 * than in here, so a cleared override cannot leave a stale draft behind in a cell nobody
 * remounted, and so this whole tab still paints under `renderToStaticMarkup`.
 *
 * ⛔ NO CONTROL AT ALL FOR A READER WHO MAY NOT RENAME (`canRename` is the server's own `may_open`
 * answer). Enabling one Odoo database materialises it in the same request (W56-T31,
 * `routes_connectors.py::_enable_odoo_database`), so there is no waiting state left to describe.
 * A reader who may not rename gets no control at all, rather than a sentence about a wait.
 */
function PenIcon() {
  return (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" aria-hidden focusable="false">
      <path d="m3.2 11.9.7-2.8 6.5-6.5 2.5 2.5-6.5 6.5-2.7.8Z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round" />
      <path d="m9.1 4 2.5 2.5" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" />
    </svg>
  );
}

function NameCell({
  database,
  editing,
  draft,
  busy,
  onDraft,
  onEdit,
  onCancel,
  onSave,
}: {
  database: ConnectorDatabase;
  editing: boolean;
  draft: string;
  busy: boolean;
  onDraft: (next: string) => void;
  onEdit: () => void;
  onCancel: () => void;
  /** An empty string CLEARS, which is what falling back to our default means. */
  onSave: (next: string) => void;
}) {
  const penRef = useRef<HTMLButtonElement>(null);
  const wasEditing = useRef(editing);
  useEffect(() => {
    if (wasEditing.current && !editing) penRef.current?.focus();
    wasEditing.current = editing;
  }, [editing]);
  // Escape is claimed through useEscapeClose: the arbiter listens at document capture,
  // so a local handler cannot see the key. Enter stays local because nothing arbitrates it.
  useEscapeClose(editing, onCancel);

  if (!editing) {
    return (
      <div className="conn-db-name-line">
        <span className="conn-db-name" title={database.override || database.defaultLabel}>
          {database.override || database.defaultLabel}
        </span>
        {database.canRename ? (
          <button
            ref={penRef}
            type="button"
            className="conn-db-pen"
            aria-label={`Rename the database ${database.label}`}
            onClick={onEdit}
          >
            <PenIcon />
          </button>
        ) : null}
      </div>
    );
  }
  return (
    <>
      {/* ⚠ THE FIELD AND LINK SHAPES ARE THE MODAL'S OWN, borrowed rather than invented: this
          ticket ships no stylesheet, and a bare input inside a styled tab is the native chrome
          DESIGN.md section 4 rules out. */}
      <div className="conn-kc-field">
        <label htmlFor={`conn-db-rename-${database.key}`}>Your name for this database</label>
        <input
          id={`conn-db-rename-${database.key}`}
          value={draft}
          disabled={busy}
          autoFocus
          autoComplete="off"
          onChange={(event) => onDraft(event.target.value)}
          onKeyDown={(event) => {
            if (event.key === "Enter") {
              event.preventDefault();
              event.stopPropagation();
              if (!(busy || !draft.trim())) onSave(draft);
            }
          }}
        />
      </div>
      <div className="conn-db-head">
        <button
          type="button"
          className="conn-db-link"
          disabled={busy || !draft.trim()}
          onClick={() => onSave(draft)}
        >
          Done
        </button>
        {database.override ? (
          <button
            type="button"
            className="conn-db-link"
            disabled={busy}
            onClick={() => onSave("")}
          >
            Use the default name
          </button>
        ) : null}
        <button type="button" className="conn-db-link" disabled={busy} onClick={onCancel}>
          Cancel
        </button>
      </div>
    </>
  );
}

/**
 * ⭐ THE TAB, PURE AND EXPORTED. Everything it lists is `detail`; the only network it does is a
 * a read the reader asked for, plus R23's pre-flight before an off change is accepted.
 */
const DATABASE_PAGE_SIZE = 10;

const DatabasesTab = forwardRef<DatabasesTabHandle, {
  row: ConnectorRow;
  detail: ConnectorDetail;
  isAdmin?: boolean;
  /** Re-read `/detail` once after a committed batch or a population count. */
  onChanged: (reveal?: DatabaseReveal) => void;
  /** Reports the diff count to the modal chrome, where Save and Discard live. */
  onPendingChange: (count: number) => void;
  /** Locks the modal actions while the dependents check or the batch is in flight. */
  onBusyChange: (busy: boolean) => void;
  onConfirmSurface?: (element: HTMLElement | null) => void;
  /** The enabled row and its result survive the modal's loading remount. */
  reveal?: DatabaseReveal | null;
  /**
   * S66-T39: later database pages are still being read ("loading"), or one failed ("failed").
   * Absent means `detail.databases` is the whole list.
   */
  databasesPaging?: "loading" | "failed";
}>(function DatabasesTab({
  row,
  detail,
  isAdmin = false,
  onChanged,
  onPendingChange,
  onBusyChange,
  onConfirmSurface,
  reveal,
  databasesPaging,
}, ref) {
  const { introspection } = detail;
  const showsPopulation = row.key === "odoo";
  const usesProviderEnablement = Boolean(detail.enablement?.canManage && detail.enablement.updatePath);
  // Database detail stays closed until requested.
  const [open, setOpen] = useState<Record<string, boolean>>(
    () => reveal?.key ? { [reveal.key]: true } : {}
  );
  const [editingFields, setEditingFields] = useState<string | null>(null);
  const fieldsOpener = useRef("");
  const closeFields = useCallback(() => {
    setEditingFields(null);
    window.requestAnimationFrame(() => document.getElementById(fieldsOpener.current)?.focus());
  }, []);
  // The Fields editor's source disclosure reads its database's deferred field list on open.
  const wantedFields = row.key === "scraper" ? detail.databases.map((database) => database.key)
    : editingFields ? [editingFields] : [];
  const fieldReads = useDeferredFields(row.key, detail.databases, wantedFields);
  const databases = fieldReads.databases;
  const totalDatabases = detail.databasesPage?.total ?? databases.length;
  const [pageOffset, setPageOffset] = useState(0);
  const databaseOpener = useRef("");
  const detailRef = useRef<HTMLElement | null>(null);
  const selectedKey = Object.keys(open).find((key) => open[key]) || "";
  const [busy, setBusy] = useState("");
  const [confirmOff, setConfirmOff] = useState<ConnectorDatabase | null>(null);
  const [refusals, setRefusals] = useState<Record<string, string>>({});
  const [refusalDependents, setRefusalDependents] = useState<Record<string, DependentKind[]>>({});
  const [pendingDestination, setPendingDestination] = useState<string | null>(null);
  const [autoEnableResults, setAutoEnableResults] = useState<Record<string, string>>(
    () => reveal?.key && reveal.status ? { [reveal.key]: reveal.status } : {}
  );
  const databaseRefusal = useRefusalToast();
  const [error, setError] = useState("");
  const [jobs, setJobs] = useState<MaterializationJob[]>([]);
  const [jobError, setJobError] = useState("");
  const [retrying, setRetrying] = useState("");
  const capability = row.key === "amazon_sp" ? null : detail.materialization;
  const showsAmazonSync = row.key === "amazon_sp" && introspection.fieldsVisible;
  const amazonSync = useAmazonSyncStates(showsAmazonSync, detail);
  const databaseSync = useDatabaseSyncStates(detail);
  const catchUpStatus = useConnectorCatchUp(row.key === "odoo" && row.state === "connected");
  useEffect(() => {
    if (!capability) return;
    return watchMaterialization(capability, (next, message) => {
      if (!message) setJobs(next);
      setJobError(message);
    });
  }, [capability?.provider, capability?.statusPath, capability?.retryPath]);
  const retryJob = async (job: MaterializationJob) => {
    if (!capability || retrying) return;
    setRetrying(job.id);
    const message = await write(
      `${capability.retryPath}?database_key=${encodeURIComponent(job.databaseKey)}` +
        `&connection_id=${encodeURIComponent(job.connectionId)}`, "POST", {});
    setJobError(message);
    setRetrying("");
    if (!message) watchMaterialization(capability);
  };
  const [censusBusy, setCensusBusy] = useState(false);
  const [censusResult, setCensusResult] = useState("");
  const [exactCounts, setExactCounts] = useState<Record<string, number>>({});
  /** The one row being renamed, and the text in its box. Empty means nobody is renaming. */
  const [renaming, setRenaming] = useState("");
  const [draft, setDraft] = useState("");
  /** Only values that differ from the last server answer live here. */
  const [pending, setPending] = useState<PendingDatabaseChanges>(EMPTY_PENDING);
  const pendingCount = pendingChangeCount(pending);

  useEffect(() => {
    onPendingChange(pendingCount);
  }, [onPendingChange, pendingCount]);

  useEffect(() => {
    onBusyChange(Boolean(busy));
  }, [busy, onBusyChange]);

  const labels = useMemo(() => {
    const out: Record<string, string> = {};
    for (const database of databases) {
      const override = pending.names[database.key] ?? database.override;
      out[database.key] = override || database.defaultLabel;
    }
    return out;
  }, [databases, pending.names]);
  const orderedDatabases = useMemo(
    () =>
      databases
        .map((database, index) => ({ database, index }))
        .sort(
          (a, b) =>
            Number(b.database.enabled) - Number(a.database.enabled) ||
            Number(b.database.populated === true) - Number(a.database.populated === true) ||
            a.index - b.index
        )
        .map(({ database }) => database),
    [databases]
  );
  const offset = Math.min(pageOffset,
    Math.max(0, Math.ceil(orderedDatabases.length / DATABASE_PAGE_SIZE) - 1) * DATABASE_PAGE_SIZE);
  const pagedDatabases = orderedDatabases.slice(offset, offset + DATABASE_PAGE_SIZE);
  const closeDatabaseDetail = useCallback(() => {
    if (busy) return;
    setOpen({});
    setRenaming("");
    setDraft("");
    window.requestAnimationFrame(() => document.getElementById(databaseOpener.current)?.focus());
  }, [busy]);
  useEscapeClose(Boolean(selectedKey) && !editingFields, closeDatabaseDetail);
  useEffect(() => {
    if (selectedKey) detailRef.current?.focus();
  }, [selectedKey]);
  const openDatabaseDetail = (databaseKey: string) => {
    if (busy || renaming) return;
    databaseOpener.current = `conn-database-row-${databaseKey}`;
    setOpen((current) => current[databaseKey] ? {} : { [databaseKey]: true });
  };
  const editedDatabase = isAdmin
    ? databases.find((database) => database.key === editingFields) : undefined;

  const stageTable = useCallback(
    (database: ConnectorDatabase, next: boolean) => {
      setPending((current) => {
        const tables = { ...current.tables };
        if (next === database.enabled) delete tables[database.key];
        else tables[database.key] = next;
        return { ...current, tables };
      });
    },
    []
  );

  /**
   * R23: the dependents check precedes staging because the disable door only writes config.
   * W57-T31 / R10 / C9: Odoo also warns about the lock before accepting an off change.
   * Provider-managed enablement keeps its existing staging path.
   *
   * ⛔ AND A FAILED CHECK IS NOT A PASSED ONE. Any non-answer leaves the table on and prints the
   * cause; proceeding would turn "REFUSED" into "usually refused".
   */
  const flipTable = useCallback(
    async (database: ConnectorDatabase, next: boolean) => {
      if (busy || !(usesProviderEnablement || introspection.canToggleTables)) return;
      setBusy(database.key);
      setError("");
      setRefusals((current) => ({ ...current, [database.key]: "" }));
      setRefusalDependents((current) => ({ ...current, [database.key]: [] }));
      setAutoEnableResults((current) => ({ ...current, [database.key]: "" }));
      if (!next) {
        const answer = await readDependents(database.key);
        if (!answer.ok) {
          setBusy("");
          databaseRefusal(
            `${database.label} was left switched on: the check for what depends on it did not ` +
              `answer. ${answer.message}`
          );
          return;
        }
        if (answer.refusal) {
          setBusy("");
          // The server's sentence, verbatim. C3 omits every zero-count kind, so this cannot
          // claim a count it does not have.
          if (answer.dependents.length > 0) {
            setRefusals((current) => ({ ...current, [database.key]: answer.refusal }));
            setRefusalDependents((current) => ({ ...current, [database.key]: answer.dependents }));
          } else databaseRefusal(answer.refusal);
          return;
        }
        if (!usesProviderEnablement) {
          setBusy("");
          setConfirmOff(database);
          return;
        }
      }
      setBusy("");
      stageTable(database, next);
    },
    [busy, introspection.canToggleTables, stageTable, usesProviderEnablement]
  );

  /**
   * ⭐ W53-T27 / R9 — THE OVERRIDE, WRITTEN THROUGH THE DOOR THAT WRITES THE NAVIGATION RAIL'S
   * OWN BUCKET at commit time. An empty `name` CLEARS it, which is the half `POST /nav/meta`
   * refuses and the reason this door exists at all.
   */
  const renameDatabase = useCallback(
    async (database: ConnectorDatabase, next: string) => {
      setError("");
      const normalized = next.trim();
      setPending((current) => {
        const names = { ...current.names };
        if (normalized === database.override) delete names[database.key];
        else names[database.key] = normalized;
        return { ...current, names };
      });
      setRenaming("");
      setDraft("");
    },
    []
  );

  const discardPending = useCallback(() => {
    setPending(EMPTY_PENDING);
    setError("");
    setRenaming("");
    setDraft("");
    onPendingChange(0);
  }, [onPendingChange]);

  const openDependent = (event: MouseEvent<HTMLAnchorElement>, href: string) => {
    if (busy) {
      event.preventDefault();
      return;
    }
    // Hash navigation never fires beforeunload. Reuse the modal's discard wording
    // and ConfirmDialog surface registration without extending its prop contract.
    if (pendingCount || renaming) {
      event.preventDefault();
      setPendingDestination(href);
    }
  };

  const savePending = useCallback(async () => {
    const snapshot = pending;
    if (!pendingChangeCount(snapshot)) return;

    const failed: PendingDatabaseChanges = { tables: {}, fields: {}, names: {} };
    const failures: string[] = [];
    const fieldFailures: string[] = [];
    setBusy("save");
    setError("");

    for (const [databaseKey, next] of Object.entries(snapshot.tables)) {
      const database = databases.find((candidate) => candidate.key === databaseKey);
      const label = database?.label || databaseKey;
      const providerEnablement = detail.enablement;
      if (providerEnablement?.canManage && providerEnablement.updatePath) {
        const path = providerEnablement.updatePath.replace(
          "{databaseKey}",
          encodeURIComponent(databaseKey)
        );
        const message = path === providerEnablement.updatePath
          ? "This provider did not return a usable enablement path."
          : await write(path, "PATCH", { enabled: next });
        if (message) {
          failed.tables[databaseKey] = next;
          failures.push(`${label}: ${message}`);
        } else signal(WORKSPACE_STALE_EVENT);
        continue;
      }
      if (next) {
        const answer = await enableDatabase(databaseKey);
        if (!answer.ok) {
          // Unlike disables and renames, a refused enable needs something else to change.
          // Drop its pending state so the toggle shows the server state instead of a fake retry.
          failures.push(`${label}: ${answer.message}`);
          continue;
        }
        const status = autoEnableResult(answer);
        setOpen({ [databaseKey]: true });
        setAutoEnableResults((current) => ({ ...current, [databaseKey]: status }));
        if (answer.workspaceChanged) signal(WORKSPACE_STALE_EVENT);
        continue;
      }
      const answer = await disableDatabase(databaseKey);
      if (!answer.ok) {
        const message = answer.message;
        failed.tables[databaseKey] = next;
        failures.push(`${label}: ${message}`);
      } else signal(WORKSPACE_STALE_EVENT);
    }

    for (const [fieldKey, next] of Object.entries(snapshot.fields)) {
      const fieldDatabase = databases.find((database) =>
        database.fields.some((candidate) => candidate.key === fieldKey)
      );
      const field = fieldDatabase?.fields.find((candidate) => candidate.key === fieldKey);
      const message = await write(
        `/connectors/odoo/schema/${encodeURIComponent(fieldKey)}`,
        "PATCH",
        { enabled: next }
      );
      if (message) {
        failed.fields[fieldKey] = next;
        const subject = fieldDatabase
          ? `${fieldDatabase.label}, ${field?.label || fieldKey}`
          : fieldKey;
        fieldFailures.push(`${subject}: ${message}`);
      } else signal(WORKSPACE_STALE_EVENT);
    }

    for (const [databaseKey, name] of Object.entries(snapshot.names)) {
      const database = databases.find((candidate) => candidate.key === databaseKey);
      const message = await write(
        `/connectors/${encodeURIComponent(row.key)}/databases/` +
          `${encodeURIComponent(databaseKey)}/name`,
        "PATCH",
        { name }
      );
      if (message) {
        failed.names[databaseKey] = name;
        failures.push(`${database?.label || databaseKey}: ${message}`);
      } else signal(WORKSPACE_STALE_EVENT);
    }

    setPending(failed);
    onPendingChange(pendingChangeCount(failed));
    setBusy("");
    setError(fieldFailures.join(" "));
    databaseRefusal(failures.join(" "));
    if (detail.materialization && row.key !== "amazon_sp") watchMaterialization(detail.materialization);
    // One batch gets one fresh detail payload. That payload also carries the newly derived graph.
    onChanged();
  }, [databases, detail.enablement, detail.materialization, onChanged, onPendingChange, pending, row.key]);

  useImperativeHandle(ref, () => ({
    savePending,
    discardPending,
    stageTable: flipTable,
    tableEnabled: (database) => pending.tables[database.key] ?? database.enabled,
  }), [discardPending, flipTable, pending.tables, savePending]);

  const runPopulationCount = useCallback(async (exact = false) => {
    setCensusBusy(true);
    setCensusResult("");
    setExactCounts({});
    setError("");
    const answer = await countSourceRows(exact);
    setCensusBusy(false);
    if (!answer.ok) {
      databaseRefusal(answer.message);
      return;
    }
    setExactCounts(answer.exactCounts);
    const pass = answer.mode === "bounded" ? "Counted up to 101 rows per source table. "
      : answer.mode === "exact" ? "Counted exact totals. "
      : answer.mode === "exact_fallback"
        ? "Counted exact totals because this connection does not support bounded counts. "
        : "";
    const exactHint = Object.keys(answer.exactCounts).length
      ? " Exact totals are available in each Source rows tooltip." : "";
    setCensusResult(pass + populationResult(
      answer.modelsCounted,
      answer.modelsWithRows,
      answer.durationMs,
      answer.capturedAt,
      answer.complete
    ) + exactHint);
    onChanged();
  }, [onChanged]);

  const showCensus = showsPopulation && introspection.fieldsVisible;
  const header = showCensus ? (
      <details className="conn-settings-source-counts">
        <summary>Source counts</summary>
        <div className="conn-db-head">
          <div>
            {showCensus ? <>
            <p className="conn-db-note" id="odoo-census-warning">
              Count rows stops at 101 rows per source table and displays 100+ from 100 onward.
              Each table still needs a request, so this can take about a minute or longer.
              Counting exact totals takes considerably longer than the default pass.
            </p>
            {censusResult ? (
              <p className="conn-db-note" role="status">{censusResult}</p>
            ) : introspection.censusAt ? (
              <p className="conn-db-note">
                {populationResult(
                  introspection.censusModelsCounted,
                  introspection.censusModelsWithRows,
                  introspection.censusDurationMs,
                  introspection.censusAt,
                  introspection.censusComplete
                )}
              </p>
            ) : null}
            </> : null}
          </div>
          {showCensus ? <>
          <button
            type="button"
            className="conn-db-count-action"
            aria-describedby="odoo-census-warning"
            disabled={censusBusy}
            onClick={() => void runPopulationCount()}
          >
            {censusBusy ? "Counting rows..." : "Count rows"}
          </button>
          <button
            type="button"
            className="conn-db-link"
            aria-describedby="odoo-census-warning"
            disabled={censusBusy}
            onClick={() => void runPopulationCount(true)}
          >
            Exact totals
          </button>
          </> : null}
        </div>
      </details>
  ) : null;

  const editedRead = editedDatabase ? fieldReads.readOf(editedDatabase.key) : undefined;
  if (editedDatabase) return <DatabaseFieldsEditor key={editedDatabase.key}
    databaseKey={editedDatabase.key} databaseLabel={labels[editedDatabase.key]}
    onBack={closeFields} onBusyChange={onBusyChange}
    disclosure={introspection.fieldsVisible ? { database: editedDatabase, introspection,
      readError: editedRead?.phase === "error" ? editedRead.message : "",
      onRetry: () => fieldReads.retry(editedDatabase.key) } : undefined} />;

  if (row.key === "scraper") {
    return <ScraperDatabases databases={databases} onChanged={onChanged}
      syncStates={databaseSync.states} syncError={databaseSync.error}
      onBusyChange={onBusyChange} paging={databasesPaging}
      fieldReadError={(key) => {
        const read = fieldReads.readOf(key);
        return read?.phase === "error" ? read.message : "";
      }}
      onRetryFields={fieldReads.retry} />;
  }

  if (!introspection.introspectable) {
    return <div className="conn-db">{header}<DatabasesUnavailable row={row} reason={introspection.reason} /></div>;
  }

  if (!databases.length && !databasesPaging) {
    return (
      <div className="conn-db">
        <p className="conn-settings-empty">
          No databases from this connection.
        </p>
      </div>
    );
  }

  return (
    <div className="conn-db">
      {error ? (
        <p className="conn-db-error" role="alert">
          {error}
        </p>
      ) : null}
      {jobError ? <p className="conn-db-error" role="alert">{jobError}</p> : null}
      <h3 className="conn-settings-table-title">Databases</h3>
      <DatabasesPagingNote paging={databasesPaging} shown={databases.length}
        total={totalDatabases} onRetry={() => onChanged()} />
      <div className="conn-settings-table-scroll conn-settings-paged-table" tabIndex={0}
        role="region" aria-label="Database list">
        <div className="conn-settings-databases conn-settings-grid" role="table"
          aria-label="Connector databases" aria-colcount={2} aria-rowcount={totalDatabases + 1}>
          <div role="rowgroup">
            <div className="conn-settings-grid-header" role="row" aria-rowindex={1}>
              <div role="columnheader">Database</div>
              <div role="columnheader">Last success</div>
            </div>
          </div>
          <div role="rowgroup">
            {pagedDatabases.map((database, index) => {
              const enabled = pending.tables[database.key] ?? database.enabled;
              const label = labels[database.key];
              const lastSuccess = databaseSync.states[database.key]?.lastSuccess
                ?? amazonSync.states[database.key]?.lastSuccess;
              const isOpen = selectedKey === database.key;
              return <div key={database.key} role="row" tabIndex={0}
                id={`conn-database-row-${database.key}`} data-database-key={database.key}
                className={"conn-settings-grid-row " + populationClass(database.populated) + (isOpen ? " is-selected" : "")}
                aria-rowindex={offset + index + 2} aria-expanded={isOpen}
                aria-controls={isOpen ? `conn-database-detail-${database.key}` : undefined}
                onClick={(event) => {
                  if ((event.target as HTMLElement).closest("button, input, a, select")) return;
                  openDatabaseDetail(database.key);
                }}
                onKeyDown={(event) => {
                  if (event.target !== event.currentTarget || !["Enter", " "].includes(event.key)) return;
                  event.preventDefault();
                  openDatabaseDetail(database.key);
                }}>
                <div role="cell" className="conn-settings-grid-name">
                  <span className="conn-db-name" title={label}>{label}</span>
                  <div className="conn-settings-row-controls" onClick={(event) => event.stopPropagation()}>
                    {isAdmin ? <button type="button" className="conn-db-link conn-db-fields-action"
                      id={`conn-database-controls-${database.key}`} aria-label={`Fields for ${label}`}
                      disabled={Boolean(busy)} onClick={() => {
                        fieldsOpener.current = `conn-database-controls-${database.key}`;
                        setEditingFields(database.key);
                      }}>Fields</button> : null}
                    <Switch on={enabled} busy={Boolean(busy) || !(usesProviderEnablement || introspection.canToggleTables)}
                      label={enabled ? `Switch off the database ${label}` : `Switch on the database ${label}`}
                      onFlip={(next) => void flipTable(database, next)} />
                  </div>
                </div>
                <div role="cell" className="conn-settings-database-success">
                  {lastSuccess ? <time dateTime={lastSuccess}>{readableSyncStamp(lastSuccess)}</time> : "Not recorded"}
                </div>
              </div>;
            })}
          </div>
        </div>
      </div>
      <div className="conn-settings-database-pager">
        <GridPager offset={offset} size={DATABASE_PAGE_SIZE} sizes={[DATABASE_PAGE_SIZE]}
          total={totalDatabases} pending={Boolean(busy) || databasesPaging === "loading"}
          hasNextPage={offset + DATABASE_PAGE_SIZE < orderedDatabases.length}
          onPage={(next) => {
            if (busy) return;
            setPageOffset(next);
            if (!renaming) closeDatabaseDetail();
          }} onSize={() => undefined} />
      </div>
      {databases.filter((database) => refusals[database.key]).map((database) => {
          return <div key={database.key} className="conn-settings-database-refusal">
            <p className="conn-db-refusal" role="alert">{refusals[database.key]}</p>
            <ul className="conn-settings-dependents">
              {(refusalDependents[database.key] || []).flatMap((dependent, kindIndex) =>
                dependentRows(dependent).map((item, itemIndex) => <li key={`${kindIndex}:${item.id}:${itemIndex}`}>
                  {item.href ? <a className="conn-db-link" href={item.href}
                    onClick={(event) => openDependent(event, item.href!)}>{item.label}</a>
                    : <>{item.label}: {item.reason || "This dependent has no direct address available."}</>}
                </li>))}
            </ul>
          </div>;
      })}
      {databases.filter((database) => database.key === selectedKey).map((database) => {
        const enabled = pending.tables[database.key] ?? database.enabled;
        const override = pending.names[database.key] ?? database.override;
        const renderedDatabase = { ...database, enabled, override, label: labels[database.key] };
        const health = databaseSync.states[database.key];
        const amazonState = amazonSync.states[database.key];
        const lastSuccess = health?.lastSuccess ?? amazonState?.lastSuccess;
        const rowCount = health?.rowCount ?? amazonState?.rowsLoaded;
        const newest = sourceCatchUp(catchUpStatus.catchUp, catchUpSourceKey(row.key))?.newest[database.key];
        const job = jobs.find((candidate) => candidate.databaseKey === database.key &&
          (!database.identity || (candidate.connectionId === database.identity.connectionId &&
            candidate.provider === database.identity.provider)));
        const siblingLabels = database.sharesFieldsWith.map((key) => labels[key]).filter(Boolean);
        return <section key={database.key} className="conn-settings-database-detail"
          id={`conn-database-detail-${database.key}`} ref={detailRef} tabIndex={-1}
          aria-label={`Details for ${renderedDatabase.label}`}>
          <div className="conn-db-head">
            <NameCell database={renderedDatabase} editing={renaming === database.key}
              draft={draft} busy={Boolean(busy)} onDraft={setDraft}
              onEdit={() => {
                setRenaming(database.key);
                setDraft(override || database.defaultLabel);
                setError("");
              }} onCancel={() => { setRenaming(""); setDraft(""); }}
              onSave={(next) => void renameDatabase(database, next)} />
            <button type="button" className="conn-db-link" disabled={Boolean(busy)}
              onClick={closeDatabaseDetail}>Close details</button>
          </div>
          <dl className="conn-settings-database-facts">
            <div><dt>Rows</dt><dd>{rowCount == null ? "Unknown" : rowCount.toLocaleString()}</dd></div>
            <div><dt>Last success</dt><dd>{lastSuccess ? <time dateTime={lastSuccess}>
              {readableSyncStamp(lastSuccess)}</time> : "Not recorded"}</dd></div>
            <div><dt>Newest data</dt><dd>{newest ? <time dateTime={newest}>
              {newest.length === 10
                ? new Date(`${newest}T00:00:00`).toLocaleDateString(undefined, { dateStyle: "medium" })
                : readableSyncStamp(newest)}</time> : "Not recorded"}</dd></div>
          </dl>
          <div className="conn-settings-database-status">
            {showsAmazonSync ? <AmazonSyncChip databaseKey={database.key}
              state={amazonState} error={amazonSync.error} />
              : <DatabaseSyncSummary compact health={health} error={databaseSync.error}
                databaseKey={database.key} catchUp={sourceCatchUp(catchUpStatus.catchUp, "odoo")}
                catchUpError={catchUpStatus.error} catchUpLoading={catchUpStatus.loading} />}
            {health?.error && !amazonState?.refused && !amazonState?.held ? <p className="conn-db-error" role="alert">
              {health.error.message}</p> : null}
            {health?.warning || amazonState?.warning ? <p className="conn-db-note">
              {health?.warning || amazonState?.warning}</p> : null}
            {job?.state === "failed" && job.retryable ? <button type="button"
              className="conn-job-retry" disabled={Boolean(retrying)}
              onClick={() => void retryJob(job)}>Retry sync</button> : null}
            {job?.stall ? <p className="conn-db-note">{job.stall.cause}</p> : null}
            {job?.error ? <p className="conn-db-error">{job.error}</p> : null}
            {autoEnableResults[database.key] && !job ? <p className="conn-db-note" role="status">
              {autoEnableResults[database.key]}</p> : null}
          </div>
          <dl className="conn-settings-database-facts">
            <div><dt>Source name</dt><dd>{database.native || "Not published"}</dd></div>
            <div><dt>Default name</dt><dd>{database.defaultLabel}</dd></div>
            {showsPopulation ? <div><dt>Source rows</dt>
              <dd title={sourceRowsTitle(exactCounts[database.native])}>
                {database.sourceRows === null ? "Not counted yet"
                  : database.sourceRows >= 100 ? "100+" : database.sourceRows.toLocaleString()}</dd></div> : null}
          </dl>
          {!enabled && introspection.canToggleFields && siblingLabels.length ? <p className="conn-db-shared">
            {renderedDatabase.label} shares its Odoo source table with {sentenceList(siblingLabels)},
            so populated fields will be switched on there too.
          </p> : null}

        </section>;
      })}
      {header}
      {showsAmazonSync ? <AmazonFirstLoadNotice states={amazonSync.states} databaseNames={labels} /> : null}
      <ConfirmDialog
        open={pendingDestination !== null}
        title="Discard unsaved changes"
        body={<p>{pendingCount === 1
          ? "You have 1 unsaved change to this connection."
          : pendingCount ? `You have ${pendingCount} unsaved changes to this connection.`
          : "You have an unfinished database name change."}</p>}
        confirmLabel="Discard changes"
        tone="destructive"
        onConfirm={() => {
          const destination = pendingDestination;
          setPendingDestination(null);
          discardPending();
          if (destination) window.location.hash = destination;
        }}
        onCancel={() => setPendingDestination(null)}
        surfaceRef={onConfirmSurface}
      />
      <ConfirmDialog
        open={confirmOff !== null}
        title="Switch off this database"
        tone="destructive"
        confirmLabel="Switch off"
        body={
          <>
            <p>{confirmOff?.label} stops being materialised from Odoo when you save this change.</p>
            <p>
              Switching a database off starts a 7 day lock. It cannot be switched back on until
              the week is up, and nobody can shorten it.
            </p>
          </>
        }
        onConfirm={() => {
          if (confirmOff) stageTable(confirmOff, false);
          setConfirmOff(null);
        }}
        onCancel={() => setConfirmOff(null)}
        surfaceRef={onConfirmSurface}
      />
    </div>
  );
});

export default DatabasesTab;