ngit 2.4.1

nostr plugin for git
Documentation
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
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
// have you considered

// TO USE ASYNC

// in traits (required for mocking unit tests)
// https://rust-lang.github.io/async-book/07_workarounds/05_async_in_traits.html
// https://github.com/dtolnay/async-trait
// see https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html
// I think we can use the async-trait crate and switch to the native feature
// which is currently in nightly. alternatively we can use nightly as it looks
// certain that the implementation is going to make it to stable but we don't
// want to inadvertlty use other features of nightly that might be removed.
use std::{
    collections::{HashMap, HashSet},
    fmt::{Display, Write},
    fs::create_dir_all,
    path::Path,
    sync::{
        Arc, Mutex, RwLock,
        atomic::{AtomicBool, AtomicU64, Ordering},
    },
    time::Duration,
};

use anyhow::{Context, Result, anyhow, bail};
use async_trait::async_trait;
use console::Style;
use futures::{
    future::join_all,
    stream::{self, StreamExt},
};
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressState, ProgressStyle};
#[cfg(test)]
use mockall::*;
use nostr::{
    Event,
    event::UnsignedEvent,
    filter::Alphabet,
    nips::{
        nip01::Coordinate,
        nip05::{Nip05Address, Nip05Profile},
        nip19::Nip19Coordinate,
    },
    signer::SignerBackend,
};
use nostr_database::{NostrDatabase, SaveEventStatus};
use nostr_lmdb::NostrLMDB;
use nostr_relay_pool::relay::ReqExitPolicy;
use nostr_sdk::{
    ClientOptions, EventBuilder, EventId, Kind, NostrSigner, PublicKey, RelayUrl, SingleLetterTag,
    Timestamp, Url, prelude::RelayLimits,
};
use serde_json::Value;

use crate::{
    get_dirs,
    git::{Repo, RepoActions, get_git_config_item},
    git_events::{
        KIND_COMMENT, KIND_COVER_NOTE, KIND_LABEL, KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE,
        KIND_USER_GRASP_LIST, event_is_cover_letter, event_is_patch_set_root,
        event_is_revision_root, event_is_valid_pr_or_pr_update, status_kinds,
    },
    login::{get_likely_logged_in_user, user::get_user_ref_from_cache},
    repo_ref::{RepoRef, normalize_grasp_server_url},
    repo_state::RepoState,
    // TEMPORARY: Remove when async-wsocket includes Happy Eyeballs support.
    // See src/lib/transport.rs header for full removal instructions.
    transport::HappyEyeballsTransport,
};

pub fn is_verbose() -> bool {
    std::env::var("NGIT_VERBOSE").is_ok()
}

const SPINNER_EXPAND_DELAY_MS: u64 = 5000;

/// Holds the final state of a progress bar that finished before the detail
/// view was revealed. The style and prefix are already set on the bar; only
/// the `finish_with_message` call is deferred.
struct DeferredFinish {
    bar: ProgressBar,
    message: String,
}

/// Coordinates the transition from spinner to detail progress bars.
/// While `revealed` is false, `finish_bar` stores finish operations in
/// `deferred`. The background timer sets `revealed` to true, switches the
/// draw target, and flushes all deferred finishes so every bar appears.
struct BarRevealState {
    revealed: AtomicBool,
    deferred: Mutex<Vec<DeferredFinish>>,
}

/// Finish a progress bar, deferring the operation if the detail view has not
/// yet been revealed. When `reveal_state` is `None` (verbose or test mode),
/// the bar is finished immediately.
fn finish_bar(bar: &ProgressBar, message: String, reveal_state: &Option<Arc<BarRevealState>>) {
    match reveal_state {
        None => bar.finish_with_message(message),
        Some(state) => {
            // Lock the deferred list and check `revealed` while holding the
            // lock. The timer also holds this lock when it sets `revealed`
            // and drains the list, so there is no window where a bar could
            // be pushed after the drain.
            let mut deferred = state.deferred.lock().unwrap();
            if state.revealed.load(Ordering::Acquire) {
                drop(deferred);
                bar.finish_with_message(message);
            } else {
                // Style and prefix are already set on the bar. Store the
                // pending finish so the timer can apply it after reveal.
                deferred.push(DeferredFinish {
                    bar: bar.clone(),
                    message,
                });
            }
        }
    }
}

#[allow(clippy::struct_field_names)]
pub struct Client {
    client: nostr_sdk::Client,
    relay_default_set: Vec<String>,
    blaster_relays: Vec<String>,
    fallback_signer_relays: Vec<String>,
    grasp_default_set: Vec<String>,
    relays_not_to_retry: Arc<RwLock<HashMap<RelayUrl, String>>>,
}

impl Client {
    /// Marks a relay as skipped for the current session with a given reason.
    /// This method encapsulates the write lock for the relays_not_to_retry map.
    fn skip_relay_for_session(&self, relay_url: RelayUrl, reason: String) {
        self.relays_not_to_retry
            .write()
            .unwrap()
            .insert(relay_url, reason);
    }

    /// Checks if a relay should be skipped for the current session and returns
    /// the reason if it is. This method encapsulates the read lock for the
    /// relays_not_to_retry map.
    fn is_relay_skipped_for_session(&self, relay_url: &RelayUrl) -> Option<String> {
        self.relays_not_to_retry
            .read()
            .unwrap()
            .get(relay_url)
            .cloned()
    }
}

#[cfg_attr(test, automock)]
#[async_trait]
pub trait Connect {
    fn default() -> Self;
    fn new(opts: Params) -> Self;
    async fn set_signer(&mut self, signer: Arc<dyn NostrSigner>);
    async fn connect(&self, relay_url: &RelayUrl) -> Result<()>;
    async fn disconnect(&self) -> Result<()>;
    fn get_relay_default_set(&self) -> &Vec<String>;
    fn get_blaster_relays(&self) -> &Vec<String>;
    fn get_fallback_signer_relays(&self) -> &Vec<String>;
    fn get_grasp_default_set(&self) -> &Vec<String>;
    async fn send_event_to<'a>(
        &self,
        git_repo_path: Option<&'a Path>,
        url: &str,
        event: nostr::event::Event,
    ) -> Result<nostr::EventId>;
    async fn get_events(
        &self,
        relays: Vec<String>,
        filters: Vec<nostr::Filter>,
    ) -> Result<Vec<nostr::Event>>;
    async fn get_events_per_relay(
        &self,
        relays: Vec<RelayUrl>,
        filters: Vec<nostr::Filter>,
        progress_reporter: MultiProgress,
    ) -> Result<(Vec<Result<Vec<nostr::Event>>>, MultiProgress)>;
    async fn fetch_all<'a>(
        &self,
        git_repo_path: Option<&'a Path>,
        repo_coordinates: Option<&'a Nip19Coordinate>,
        user_profiles: &HashSet<PublicKey>,
    ) -> Result<(Vec<Result<FetchReport>>, MultiProgress)>;
    async fn fetch_all_from_relay<'a>(
        &self,
        git_repo_path: Option<&'a Path>,
        request: FetchRequest,
        pb: &Option<ProgressBar>,
    ) -> Result<FetchReport>;
}

#[async_trait]
impl Connect for Client {
    fn default() -> Self {
        Self::new(Params::default())
    }

    fn new(opts: Params) -> Self {
        Client {
            client: if let Some(keys) = opts.keys {
                nostr_sdk::ClientBuilder::new()
                    .opts(
                        ClientOptions::new()
                            .relay_limits(RelayLimits::disable())
                            .verify_subscriptions(true),
                    )
                    .signer(keys)
                    .websocket_transport(HappyEyeballsTransport) // TEMPORARY: see transport.rs
                    .build()
            } else {
                nostr_sdk::ClientBuilder::new()
                    .opts(
                        ClientOptions::new()
                            .relay_limits(RelayLimits::disable())
                            .verify_subscriptions(true),
                    )
                    .websocket_transport(HappyEyeballsTransport) // TEMPORARY: see transport.rs
                    .build()
            },
            relay_default_set: opts.relay_default_set,
            blaster_relays: opts.blaster_relays,
            fallback_signer_relays: opts.fallback_signer_relays,
            grasp_default_set: opts.grasp_default_set,
            relays_not_to_retry: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    async fn set_signer(&mut self, signer: Arc<dyn NostrSigner>) {
        self.client.set_signer(signer).await;
    }

    async fn connect(&self, relay_url: &RelayUrl) -> Result<()> {
        if let Some(reason) = self.is_relay_skipped_for_session(relay_url) {
            bail!("{reason}");
        }
        self.client
            .add_relay(relay_url)
            .await
            .context("failed to add relay")?;

        let relay = self.client.relay(relay_url).await?;

        if !relay.is_connected() {
            #[allow(clippy::large_futures)]
            relay
                .try_connect(std::time::Duration::from_secs(long_timeout()))
                .await?;
        }

        Ok(())
    }

    async fn disconnect(&self) -> Result<()> {
        self.client.disconnect().await;
        Ok(())
    }

    fn get_relay_default_set(&self) -> &Vec<String> {
        &self.relay_default_set
    }

    fn get_blaster_relays(&self) -> &Vec<String> {
        &self.blaster_relays
    }

    fn get_fallback_signer_relays(&self) -> &Vec<String> {
        &self.fallback_signer_relays
    }

    fn get_grasp_default_set(&self) -> &Vec<String> {
        &self.grasp_default_set
    }

    async fn send_event_to<'a>(
        &self,
        git_repo_path: Option<&'a Path>,
        url: &str,
        event: Event,
    ) -> Result<nostr::EventId> {
        self.client.add_relay(url).await?;
        #[allow(clippy::large_futures)]
        self.client.connect_relay(url).await?;
        self.client.relay(url).await?.send_event(&event).await?;
        if let Some(git_repo_path) = git_repo_path {
            save_event_in_local_cache(git_repo_path, &event).await?;
        }
        if [Kind::GitRepoAnnouncement, KIND_USER_GRASP_LIST].contains(&event.kind) {
            save_event_in_global_cache(git_repo_path, &event).await?;
        }
        Ok(event.id)
    }

    async fn get_events(
        &self,
        relays: Vec<String>,
        filters: Vec<nostr::Filter>,
    ) -> Result<Vec<nostr::Event>> {
        let (relay_results, _) = self
            .get_events_per_relay(
                relays.iter().map(|r| RelayUrl::parse(r).unwrap()).collect(),
                filters,
                MultiProgress::new(),
            )
            .await?;
        Ok(get_dedup_events(relay_results))
    }

    async fn get_events_per_relay(
        &self,
        relays: Vec<RelayUrl>,
        filters: Vec<nostr::Filter>,
        progress_reporter: MultiProgress,
    ) -> Result<(Vec<Result<Vec<nostr::Event>>>, MultiProgress)> {
        // add relays
        for relay in &relays {
            self.client
                .add_relay(relay.as_str())
                .await
                .context("failed to add relay")?;
        }

        let relays_map = self.client.relays().await;

        // Static timeout for get_events_per_relay (no adaptive timeout here)
        let static_timeout = Arc::new(AtomicU64::new(long_timeout()));

        let futures: Vec<_> = relays
            .clone()
            .iter()
            // don't look for events on blaster
            .filter(|r| !r.as_str().contains("nostr.mutinywallet.com"))
            .map(|r| (relays_map.get(r).unwrap(), filters.clone()))
            .map(|(relay, filters)| {
                let static_timeout_clone = static_timeout.clone();
                let progress_reporter_clone = progress_reporter.clone();
                async move {
                    let pb = if std::env::var("NGITTEST").is_err() {
                        let pb = progress_reporter_clone.add(
                            ProgressBar::new(1)
                                .with_prefix(format!("{: <11}{}", "connecting", relay.url()))
                                .with_style(pb_style(static_timeout_clone)?),
                        );
                        pb.enable_steady_tick(Duration::from_millis(300));
                        Some(pb)
                    } else {
                        None
                    };
                    fn update_progress_bar_with_error(
                        relay_url: &RelayUrl,
                        pb: Option<ProgressBar>,
                        error: &anyhow::Error,
                    ) {
                        if let Some(pb) = pb {
                            pb.set_style(pb_after_style(false));
                            pb.set_prefix(format!("{: <11}{}", "error", relay_url));
                            pb.finish_with_message(
                                console::style(
                                    error.to_string().replace("relay pool error:", "error:"),
                                )
                                .for_stderr()
                                .red()
                                .to_string(),
                            );
                        }
                    }
                    if let Some(reason) = self.is_relay_skipped_for_session(relay.url()) {
                        update_progress_bar_with_error(relay.url(), pb, &anyhow!("{reason}"));
                        bail!("{reason}");
                    }
                    #[allow(clippy::large_futures)]
                    match get_events_of(relay, filters, &pb).await {
                        Err(error) => {
                            // Check error for timeout/connection issues and add to skip list
                            if error.to_string().contains("connection timeout") {
                                self.skip_relay_for_session(relay.url().clone(), error.to_string());
                            }
                            update_progress_bar_with_error(relay.url(), pb, &error);
                            Err(error)
                        }
                        Ok(res) => {
                            if let Some(pb) = pb {
                                pb.set_style(pb_after_style(true));
                                pb.set_prefix(format!(
                                    "{: <11}{}",
                                    format!("{} events", res.len()),
                                    relay.url()
                                ));
                                pb.finish_with_message("");
                            }
                            Ok(res)
                        }
                    }
                }
            })
            .collect();

        let relay_results: Vec<Result<Vec<nostr::Event>>> =
            stream::iter(futures).buffer_unordered(15).collect().await;

        Ok((relay_results, progress_reporter))
    }

    #[allow(clippy::too_many_lines)]
    async fn fetch_all<'a>(
        &self,
        git_repo_path: Option<&'a Path>,
        trusted_maintainer_coordinate: Option<&'a Nip19Coordinate>,
        user_profiles: &HashSet<PublicKey>,
    ) -> Result<(Vec<Result<FetchReport>>, MultiProgress)> {
        let relay_default_set = &self
            .relay_default_set
            .iter()
            .filter_map(|r| RelayUrl::parse(r).ok())
            .collect::<HashSet<RelayUrl>>();

        let mut request = create_relays_request(
            git_repo_path,
            trusted_maintainer_coordinate,
            user_profiles,
            relay_default_set.clone(),
        )
        .await?;

        let verbose = is_verbose();
        let is_test = std::env::var("NGITTEST").is_ok();

        // Set up the two-MultiProgress pattern:
        // 1. A spinner MultiProgress shown immediately (concise mode only)
        // 2. A detail MultiProgress that starts hidden and becomes visible after a
        //    delay
        let spinner_multi = if !verbose && !is_test {
            let m = MultiProgress::new();
            let spinner = m.add(
                ProgressBar::new_spinner()
                    .with_style(
                        ProgressStyle::with_template("{spinner} {msg}")
                            .unwrap()
                            .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈"),
                    )
                    .with_message("Checking nostr relays..."),
            );
            spinner.enable_steady_tick(Duration::from_millis(100));
            Some((m, spinner))
        } else {
            None
        };

        let progress_reporter = if is_test {
            MultiProgress::with_draw_target(ProgressDrawTarget::hidden())
        } else if verbose {
            MultiProgress::new()
        } else {
            MultiProgress::with_draw_target(ProgressDrawTarget::hidden())
        };

        // Pre-add a heading bar at position 0 so it has a reserved slot
        // before any relay bars are added. It stays hidden (draw target is
        // hidden) until the timer reveals it.
        let heading_bar = if !verbose && !is_test {
            let bar = progress_reporter.add(
                ProgressBar::new(0).with_style(ProgressStyle::with_template("{msg}").unwrap()),
            );
            Some(bar)
        } else {
            None
        };

        // Track whether the detail view has been revealed. Bars that finish
        // before reveal have their finish_with_message deferred so they render
        // correctly once the draw target switches from hidden to stderr.
        let reveal_state: Option<Arc<BarRevealState>> = if !verbose && !is_test {
            Some(Arc::new(BarRevealState {
                revealed: AtomicBool::new(false),
                deferred: Mutex::new(Vec::new()),
            }))
        } else {
            None
        };

        // Spawn a background timer that transitions from spinner to detail view
        let detail_multi_for_timer = progress_reporter.clone();
        let spinner_for_timer = spinner_multi.as_ref().map(|(_, s)| s.clone());
        let reveal_state_for_timer = reveal_state.clone();
        let heading_bar_for_timer = heading_bar.clone();
        let timer_handle = if !verbose && !is_test {
            let handle = tokio::spawn(async move {
                tokio::time::sleep(Duration::from_millis(SPINNER_EXPAND_DELAY_MS)).await;
                // Transition: finish spinner, show heading, reveal detail bars
                if let Some(spinner) = spinner_for_timer {
                    spinner.finish_and_clear();
                }
                // Switch draw target to make bars visible
                detail_multi_for_timer.set_draw_target(ProgressDrawTarget::stderr());
                // Finish the pre-added heading bar now that the draw target
                // is visible so indicatif actually renders it.
                if let Some(heading) = heading_bar_for_timer {
                    heading.finish_with_message("Checking nostr relays...");
                }
                // Mark as revealed and flush all bars that finished while
                // the draw target was hidden. Hold the lock across the flag
                // update and drain so no bar can slip through unseen (see
                // the corresponding lock in finish_bar).
                if let Some(state) = reveal_state_for_timer {
                    let mut deferred = state.deferred.lock().unwrap();
                    state.revealed.store(true, Ordering::Release);
                    for df in deferred.drain(..) {
                        df.bar.finish_with_message(df.message);
                    }
                }
            });
            Some(handle)
        } else {
            None
        };

        let success_count = Arc::new(AtomicU64::new(0));
        let current_timeout = Arc::new(AtomicU64::new(long_timeout()));

        let mut processed_relays = HashSet::new();

        let mut relay_reports: Vec<Result<FetchReport>> = vec![];

        loop {
            let relays = request
                .repo_relays
                .union(&request.user_relays_for_profiles)
                .filter(|&r| !r.as_str().contains("nostr.mutinywallet.com"))
                .cloned()
                .collect::<HashSet<RelayUrl>>()
                .difference(&processed_relays)
                .cloned()
                .collect::<HashSet<RelayUrl>>();
            if relays.is_empty() {
                break;
            }
            let profile_relays_only = request
                .user_relays_for_profiles
                .difference(&request.repo_relays)
                .collect::<HashSet<&RelayUrl>>();
            for relay in &request.repo_relays {
                self.client
                    .add_relay(relay.as_str())
                    .await
                    .context("failed to add relay")?;
            }

            let success_count_for_loop = success_count.clone();
            let current_timeout_for_loop = current_timeout.clone();
            let total_relays = relays.len() as u64;

            let futures: Vec<_> = relays
                .iter()
                .map(|r| {
                    if profile_relays_only.contains(r) {
                        FetchRequest {
                            selected_relay: Some(r.to_owned()),
                            repo_coordinates_without_relays: vec![],
                            proposals: HashSet::new(),
                            missing_contributor_profiles: request
                                .missing_contributor_profiles
                                .union(
                                    &request
                                        .profiles_to_fetch_from_user_relays
                                        .clone()
                                        .into_keys()
                                        .collect(),
                                )
                                .copied()
                                .collect(),
                            ..request.clone()
                        }
                    } else {
                        FetchRequest {
                            selected_relay: Some(r.to_owned()),
                            ..request.clone()
                        }
                    }
                })
                .map(|request| {
                    let success_count_clone = success_count_for_loop.clone();
                    let current_timeout_clone = current_timeout_for_loop.clone();
                    let progress_reporter_clone = progress_reporter.clone();
                    let total_relays_clone = total_relays;
                    let reveal_state_clone = reveal_state.clone();
                    async move {
                        let relay_column_width = request.relay_column_width;

                        let relay_url = request
                            .selected_relay
                            .clone()
                            .context("fetch_all_from_relay called without a relay")?;

                        // Always create a real progress bar added to the detail
                        // multi. In test mode the multi has a hidden draw target
                        // so nothing is displayed. In concise mode the multi
                        // starts hidden and the background timer reveals it.
                        let pb = progress_reporter_clone.add(
                            ProgressBar::new(1)
                                .with_prefix(
                                    format!(
                                        "{: <relay_column_width$} connecting",
                                        &relay_url
                                    )
                                    .to_string(),
                                )
                                .with_style(pb_style(current_timeout_clone.clone())?),
                        );
                        pb.enable_steady_tick(Duration::from_millis(300));
                        let pb = Some(pb);

                        /// Set error styling on a progress bar without finishing
                        /// it. Returns the error message so the caller can
                        /// finish the bar through the deferred mechanism.
                        fn style_progress_bar_with_error(
                            relay_column_width: usize,
                            relay_url: &RelayUrl,
                            pb: &Option<ProgressBar>,
                            error: &anyhow::Error,
                        ) -> String {
                            let msg = console::style(
                                error.to_string().replace("relay pool error:", "error:"),
                            )
                            .for_stderr()
                            .red()
                            .to_string();
                            if let Some(pb) = pb {
                                pb.set_style(pb_after_style(false));
                                pb.set_prefix(
                                    Style::new()
                                        .color256(247)
                                        .apply_to(format!("{: <relay_column_width$}", &relay_url))
                                        .to_string(),
                                );
                            }
                            msg
                        }

                        if let Some(reason) = self.is_relay_skipped_for_session(&relay_url) {
                            let msg = style_progress_bar_with_error(
                                relay_column_width,
                                &relay_url,
                                &pb,
                                &anyhow!("{reason}"),
                            );
                            if let Some(ref bar) = pb {
                                finish_bar(bar, msg, &reveal_state_clone);
                            }
                            bail!("{reason}");
                        }

                        let pb_clone = pb.clone();
                        let fetch_future = self.fetch_all_from_relay(git_repo_path, request, &pb_clone);
                        tokio::pin!(fetch_future);

                        let timeout_future = async {
                            let check_interval = Duration::from_millis(100);
                            let long_timeout_end = tokio::time::Instant::now() + Duration::from_secs(long_timeout());

                            loop {
                                let current_success_count = success_count_clone.load(Ordering::Relaxed);
                                let threshold = (total_relays_clone as f64 * SUCCESS_THRESHOLD).ceil() as u64;

                                if current_success_count >= threshold {
                                    tokio::time::sleep(Duration::from_secs(short_timeout())).await;
                                    return "short";
                                }

                                if tokio::time::Instant::now() >= long_timeout_end {
                                    return "long";
                                }

                                tokio::time::sleep(check_interval).await;
                            }
                        };

                        #[allow(clippy::large_futures)]
                        let result = tokio::select! {
                            result = &mut fetch_future => {
                                if result.is_ok() {
                                    let new_count = success_count_clone.fetch_add(1, Ordering::Relaxed) + 1;
                                    let threshold = (total_relays_clone as f64 * SUCCESS_THRESHOLD).ceil() as u64;

                                    if new_count >= threshold {
                                        current_timeout_clone.store(short_timeout(), Ordering::Relaxed);
                                    }
                                }
                                result
                            }
                            timeout_type = timeout_future => {
                                Err(anyhow!("timeout after {}s timeout",
                                    if timeout_type == "long" { long_timeout() } else { short_timeout() }))
                            }
                        };

                        match result {
                            Err(error) => {
                                if error.to_string().contains("connection timeout") || error.to_string().contains("timeout after") {
                                    self.skip_relay_for_session(relay_url.clone(), error.to_string());
                                }
                                let msg = style_progress_bar_with_error(
                                    relay_column_width,
                                    &relay_url,
                                    &pb,
                                    &error,
                                );
                                if let Some(ref bar) = pb {
                                    finish_bar(bar, msg, &reveal_state_clone);
                                }
                                Err(error)
                            }
                            Ok(res) => {
                                // The bar's style and prefix were already set
                                // by fetch_all_from_relay; finish it through
                                // the deferred mechanism.
                                if let Some(ref bar) = pb {
                                    finish_bar(bar, String::new(), &reveal_state_clone);
                                }
                                Ok(res)
                            }
                        }
                    }
                })
                .collect();

            for report in stream::iter(futures)
                .buffer_unordered(15)
                .collect::<Vec<Result<FetchReport>>>()
                .await
            {
                relay_reports.push(report);
            }
            processed_relays.extend(relays.clone());

            if let Some(trusted_maintainer_coordinate) = trusted_maintainer_coordinate {
                if let Ok(repo_ref) =
                    get_repo_ref_from_cache(git_repo_path, trusted_maintainer_coordinate).await
                {
                    request.repo_relays = repo_ref.relays.iter().cloned().collect();
                }
            }

            request.user_relays_for_profiles = {
                let mut set = HashSet::new();
                for user in &request
                    .profiles_to_fetch_from_user_relays
                    .clone()
                    .into_keys()
                    .collect::<Vec<PublicKey>>()
                {
                    if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, user).await {
                        for r in user_ref.relays.write() {
                            if let Ok(url) = RelayUrl::parse(&r) {
                                set.insert(url);
                            }
                        }
                    }
                }
                set
            };
        }

        // Cancel the background timer if it hasn't fired yet, and clean up
        // the spinner. If the timer already fired, the abort is a no-op.
        if let Some(handle) = timer_handle {
            handle.abort();
        }
        // Clear the spinner (no-op if timer already cleared it)
        if let Some((_, spinner)) = &spinner_multi {
            spinner.finish_and_clear();
        }

        Ok((relay_reports, progress_reporter))
    }

    async fn fetch_all_from_relay<'a>(
        &self,
        git_repo_path: Option<&'a Path>,
        request: FetchRequest,
        pb: &Option<ProgressBar>,
    ) -> Result<FetchReport> {
        let mut fresh_coordinates: HashSet<Nip19Coordinate> = HashSet::new();
        for (c, _) in request.repo_coordinates_without_relays.clone() {
            fresh_coordinates.insert(c);
        }
        let mut fresh_proposal_roots = request.proposals.clone();
        let mut fresh_issue_roots = request.issue_ids.clone();
        let mut fresh_profiles: HashSet<PublicKey> = request
            .missing_contributor_profiles
            .union(
                &request
                    .profiles_to_fetch_from_user_relays
                    .clone()
                    .into_keys()
                    .collect(),
            )
            .copied()
            .collect();
        // Only request non-proposal event deletions on the first loop iteration;
        // cleared after first use so subsequent iterations don't re-request them.
        let mut fresh_non_proposal_event_ids = request.non_proposal_event_ids.clone();

        let mut report = FetchReport::default();

        let relay_url = request
            .selected_relay
            .clone()
            .context("fetch_all_from_relay called without a relay")?;

        let relay_column_width = request.relay_column_width;

        let _ = self.client.add_relay(&relay_url).await;

        let dim = Style::new().color256(247);

        loop {
            let filters = get_fetch_filters(
                &fresh_coordinates,
                &fresh_proposal_roots,
                &fresh_issue_roots,
                &fresh_non_proposal_event_ids,
                &fresh_profiles,
            );
            fresh_non_proposal_event_ids = HashSet::new();

            if let Some(pb) = &pb {
                pb.set_prefix(
                    dim.apply_to(format!(
                        "{: <relay_column_width$} {}",
                        &relay_url,
                        if report.to_string().is_empty() {
                            "fetching".to_string()
                        } else {
                            format!("fetching... updates: {report}")
                        },
                    ))
                    .for_stderr()
                    .to_string(),
                );
            }

            fresh_coordinates = HashSet::new();
            fresh_proposal_roots = HashSet::new();
            fresh_issue_roots = HashSet::new();
            fresh_profiles = HashSet::new();

            let relay = self.client.relay(&relay_url).await?;
            let events: Vec<nostr::Event> = get_events_of(&relay, filters.clone(), pb).await?;
            // TODO: try reconcile

            // Track the best state event seen from this relay so callers can
            // determine which relays have a stale or absent state event.
            // We must do this before process_fetched_events because the local
            // database only stores the canonical latest event; per-relay
            // visibility is only available here.
            for event in &events {
                if event.kind.eq(&STATE_KIND) {
                    let entry = report
                        .state_per_relay
                        .entry(relay_url.clone())
                        .or_insert(None);
                    let is_newer = entry.as_ref().is_none_or(|existing: &nostr::Event| {
                        event.created_at.gt(&existing.created_at)
                            || (event.created_at.eq(&existing.created_at)
                                && event.id.gt(&existing.id))
                    });
                    if is_newer {
                        *entry = Some(event.clone());
                    }
                }
            }
            // Mark relay as queried even if no state event was returned.
            report
                .state_per_relay
                .entry(relay_url.clone())
                .or_insert(None);

            process_fetched_events(
                events,
                &request,
                git_repo_path,
                &mut fresh_coordinates,
                &mut fresh_proposal_roots,
                &mut fresh_issue_roots,
                &mut fresh_profiles,
                &mut report,
            )
            .await?;

            if fresh_coordinates.is_empty()
                && fresh_proposal_roots.is_empty()
                && fresh_issue_roots.is_empty()
                && fresh_profiles.is_empty()
            {
                break;
            }
        }
        if let Some(pb) = pb {
            pb.set_style(pb_after_style(true));
            pb.set_prefix(format!(
                "{} {}",
                dim.apply_to(format!("{: <relay_column_width$}", &relay_url))
                    .for_stderr(),
                if report.to_string().is_empty() {
                    "no new events".to_string()
                } else {
                    format!("new events: {report}")
                },
            ));
            // Don't call finish_with_message here — the caller handles
            // finishing through the deferred mechanism so bars that complete
            // before the detail view is revealed still appear correctly.
        }
        Ok(report)
    }
}

static SUCCESS_THRESHOLD: f64 = 0.5; // 50% of relays must succeed to switch to short timeout

fn long_timeout() -> u64 {
    if std::env::var("NGITTEST").is_ok() {
        1
    } else {
        45
    }
}

fn short_timeout() -> u64 {
    if std::env::var("NGITTEST").is_ok() {
        1
    } else {
        7
    }
}

async fn get_events_of(
    relay: &nostr_sdk::Relay,
    filters: Vec<nostr::Filter>,
    pb: &Option<ProgressBar>,
) -> Result<Vec<Event>> {
    // relay.reconcile(filter, opts).await?;

    let mut retry_delay = Duration::from_secs(2);
    let start_time = std::time::Instant::now();
    let max_timeout = Duration::from_secs(long_timeout());
    let mut last_error = None;
    let mut attempt_num = 0;
    let dim = Style::new().color256(247);

    if let Some(pb) = pb {
        pb.set_prefix(
            console::style(relay.url())
                .for_stderr()
                .yellow()
                .to_string(),
        );
        pb.set_message("connecting");
    }
    while !relay.is_connected() {
        attempt_num += 1;
        #[allow(clippy::large_futures)]
        match relay
            .try_connect(Duration::from_secs(short_timeout()))
            .await
        {
            Ok(_) => {
                if relay.is_connected() {
                    break;
                }
            }
            Err(e) => {
                last_error = Some(e);
            }
        }
        // Check if we have time for another retry
        if start_time.elapsed() + retry_delay >= max_timeout {
            break;
        }

        // For short delays (< 2s), just show a simple message and sleep
        // For longer delays, show a countdown to provide feedback
        if retry_delay < Duration::from_secs(2) {
            if let Some(pb) = pb {
                let retry_msg = if attempt_num > 1 {
                    format!("retrying (attempt {attempt_num})")
                } else {
                    "retrying".to_string()
                };
                pb.set_message(format!(
                    "{} {}",
                    console::style("connection failed").for_stderr().red(),
                    dim.apply_to(retry_msg).for_stderr()
                ));
            }
            tokio::time::sleep(retry_delay).await;
        } else {
            // Countdown with dynamic updates for longer delays
            let retry_start = std::time::Instant::now();
            let mut interval = tokio::time::interval(Duration::from_millis(100));
            interval.tick().await; // First tick completes immediately

            loop {
                let elapsed = retry_start.elapsed();
                let remaining = retry_delay.saturating_sub(elapsed);

                if let Some(pb) = pb {
                    let retry_msg = if attempt_num > 1 {
                        format!(
                            "retrying in {:.0}s (attempt {attempt_num})",
                            remaining.as_secs_f64()
                        )
                    } else {
                        format!("retrying in {:.0}s", remaining.as_secs_f64())
                    };
                    pb.set_message(format!(
                        "{} {}",
                        console::style("connection failed").for_stderr().red(),
                        dim.apply_to(retry_msg).for_stderr()
                    ));
                }

                if elapsed >= retry_delay {
                    break;
                }

                interval.tick().await;
            }
        }

        // Check again after sleep
        if start_time.elapsed() >= max_timeout {
            break;
        }

        retry_delay = Duration::from_secs_f64(retry_delay.as_secs_f64() * 1.5);
    }

    if !relay.is_connected() {
        if let Some(e) = last_error {
            bail!("connection timeout: {}", e);
        } else {
            bail!("connection timeout here");
        }
    } else if let Some(pb) = pb {
        pb.set_prefix(
            console::style(relay.url())
                .for_stderr()
                .yellow()
                .to_string(),
        );
        pb.set_message("connected");
    }

    let events_res = join_all(filters.into_iter().map(|filter| async {
        relay
            .fetch_events(
                filter,
                // Use a very long timeout; actual timeout is controlled by outer tokio::select!
                std::time::Duration::from_secs(long_timeout()),
                ReqExitPolicy::ExitOnEOSE,
            )
            .await
    }))
    .await;

    // no Event is being mutated, just new items added to the set
    #[allow(clippy::mutable_key_type)]
    let mut events: HashSet<Event> = HashSet::new();

    for res in events_res {
        events.extend(res?);
    }
    Ok(events.into_iter().collect())
}

pub struct Params {
    pub keys: Option<nostr::Keys>,
    pub relay_default_set: Vec<String>,
    pub blaster_relays: Vec<String>,
    pub fallback_signer_relays: Vec<String>,
    pub grasp_default_set: Vec<String>,
}

impl Default for Params {
    fn default() -> Self {
        Params {
            keys: None,
            relay_default_set: if std::env::var("NGITTEST").is_ok() {
                vec![
                    "ws://localhost:8051".to_string(),
                    "ws://localhost:8052".to_string(),
                ]
            } else {
                vec![
                    "wss://relay.damus.io".to_string(), /* free, good reliability, have been
                                                         * known
                                                         * to delete all messages */
                    "wss://relay.ditto.pub".to_string(),
                    // "wss://nos.lol".to_string(), // always prompts for nip42 auth even for
                    // reading
                ]
            },
            blaster_relays: if std::env::var("NGITTEST").is_ok() {
                vec!["ws://localhost:8057".to_string()]
            } else {
                vec![]
            },
            fallback_signer_relays: if std::env::var("NGITTEST").is_ok() {
                vec!["ws://localhost:8051".to_string()]
            } else {
                vec![
                    "wss://bucket.coracle.social".to_string(),
                    "wss://nos.lol".to_string(),
                    "wss://relay.ditto.pub".to_string(),
                ]
            },
            grasp_default_set: if std::env::var("NGITTEST").is_ok() {
                vec![]
            } else {
                vec!["relay.ngit.dev".to_string(), "gitnostr.com".to_string()]
            },
        }
    }
}
impl Params {
    pub fn with_git_config_relay_defaults(git_repo: &Option<&Repo>) -> Self {
        let mut params = Params::default();
        if std::env::var("NGITTEST").is_err() {
            // ignore git config settings under test
            if let Ok(Some(relay_defaults)) =
                get_git_config_item(git_repo, "nostr.relay-default-set")
            {
                let new_default_relays: Vec<String> = relay_defaults
                    .split(';')
                    .filter_map(|url| RelayUrl::parse(url).ok()) // Attempt to parse and filter out errors
                    .map(|relay_url| relay_url.to_string()) // Convert RelayUrl back to String
                    .collect();
                // elsewhere it is assumed this isn't empty
                if !new_default_relays.is_empty() {
                    params.relay_default_set = new_default_relays;
                }
            }
            if let Ok(Some(relay_blasters)) =
                get_git_config_item(git_repo, "nostr.relay-blaster-set")
            {
                params.blaster_relays = relay_blasters
                    .split(';')
                    .filter_map(|url| RelayUrl::parse(url).ok()) // Attempt to parse and filter out errors
                    .map(|relay_url| relay_url.to_string()) // Convert RelayUrl back to String
                    .collect();
            }
            if let Ok(Some(relay_signer)) =
                get_git_config_item(git_repo, "nostr.relay-signer-fallback-set")
            {
                params.fallback_signer_relays = relay_signer
                    .split(';')
                    .filter_map(|url| RelayUrl::parse(url).ok()) // Attempt to parse and filter out errors
                    .map(|relay_url| relay_url.to_string()) // Convert RelayUrl back to String
                    .collect();
            }
            if let Ok(Some(grasp_default_servers)) =
                get_git_config_item(git_repo, "nostr.grasp-default-set")
            {
                let new_default_grasp_servers: Vec<String> = grasp_default_servers
                    .split(';')
                    .filter_map(|url| normalize_grasp_server_url(url).ok()) // Attempt to parse and filter out errors
                    .collect();
                if !new_default_grasp_servers.is_empty() {
                    params.grasp_default_set = new_default_grasp_servers;
                }
            }
        }
        params
    }
}

fn get_dedup_events(relay_results: Vec<Result<Vec<nostr::Event>>>) -> Vec<Event> {
    let mut dedup_events: Vec<Event> = vec![];
    for events in relay_results.into_iter().flatten() {
        for event in events {
            if !dedup_events.iter().any(|e| event.id.eq(&e.id)) {
                dedup_events.push(event);
            }
        }
    }
    dedup_events
}

pub async fn sign_event(
    event_builder: EventBuilder,
    signer: &Arc<dyn NostrSigner>,
    description: String,
) -> Result<nostr::Event> {
    if signer.backend() == SignerBackend::NostrConnect {
        let term = console::Term::stderr();
        term.write_line(&format!(
            "signing event ({description}) with remote signer..."
        ))?;
        let event = signer
            .sign_event(event_builder.build(signer.get_public_key().await?))
            .await
            .context("failed to sign event")?;
        term.clear_last_lines(1)?;
        Ok(event)
    } else {
        signer
            .sign_event(event_builder.build(signer.get_public_key().await?))
            .await
            .context("failed to sign event")
    }
}

pub async fn sign_draft_event(
    draft_event: UnsignedEvent,
    signer: &Arc<dyn NostrSigner>,
    description: String,
) -> Result<nostr::Event> {
    if signer.backend() == SignerBackend::NostrConnect {
        let term = console::Term::stderr();
        term.write_line(&format!(
            "signing event ({description}) with remote signer..."
        ))?;
        let event = signer
            .sign_event(draft_event)
            .await
            .context("failed to sign event")?;
        term.clear_last_lines(1)?;
        Ok(event)
    } else {
        signer
            .sign_event(draft_event)
            .await
            .context("failed to sign event")
    }
}

pub async fn fetch_public_key(signer: &Arc<dyn NostrSigner>) -> Result<nostr::PublicKey> {
    if signer.backend() == SignerBackend::NostrConnect {
        let term = console::Term::stderr();
        term.write_line("fetching npub from remote signer...")?;
        let public_key = signer
            .get_public_key()
            .await
            .context("failed to get npub from remote signer")?;
        term.clear_last_lines(1)?;
        Ok(public_key)
    } else {
        signer
            .get_public_key()
            .await
            .context("failed to get public key from local keys")
    }
}

pub async fn nip05_query(nip05_addr: &str) -> Result<Nip05Profile> {
    let addr_deconstructed = Nip05Address::parse(nip05_addr)
        .context(format!("cannot parse nip05 address: {nip05_addr}"))?;
    let json_res: Value = reqwest::Client::new()
        .get(addr_deconstructed.url().to_string())
        .send()
        .await
        .context(format!(
            "nip05 server is not responding for address: {nip05_addr}"
        ))?
        .json()
        .await
        .context(format!(
            "nip05 server response did not respond with json when querying address: {nip05_addr}"
        ))?;
    Nip05Profile::from_json(&addr_deconstructed, &json_res).context(format!(
        "cannot get public key for nip05 address: {nip05_addr}"
    ))
}

fn pb_style(current_timeout: Arc<AtomicU64>) -> Result<ProgressStyle> {
    Ok(
        ProgressStyle::with_template(" {spinner} {prefix} {msg} {timeout_in}")?.with_key(
            "timeout_in",
            move |state: &ProgressState, w: &mut dyn Write| {
                let elapsed = state.elapsed().as_secs();
                // Adaptive timeout display: reads the actual current timeout value
                // which starts at LONG_TIMEOUT and switches to SHORT_TIMEOUT after
                // the first relay succeeds
                if elapsed > 3 {
                    let dim = Style::new().color256(247);
                    let timeout = current_timeout.load(Ordering::Relaxed);
                    if elapsed < timeout {
                        write!(
                            w,
                            "{}",
                            dim.apply_to(format!("timeout in {:.1}s", timeout - elapsed))
                                .for_stderr()
                        )
                        .unwrap();
                    }
                }
            },
        ),
    )
}

fn pb_after_style(succeed: bool) -> indicatif::ProgressStyle {
    ProgressStyle::with_template(
        format!(
            " {} {}",
            if succeed {
                console::style("".to_string())
                    .for_stderr()
                    .green()
                    .to_string()
            } else {
                console::style("".to_string())
                    .for_stderr()
                    .red()
                    .to_string()
            },
            "{prefix} {msg}",
        )
        .as_str(),
    )
    .unwrap()
}

async fn get_local_cache_database(git_repo_path: &Path) -> Result<NostrLMDB> {
    let git_dir = git2::Repository::discover(git_repo_path)
        .context("failed to discover git repository")?
        .commondir()
        .to_path_buf();
    NostrLMDB::open(git_dir.join("nostr-cache.lmdb"))
        .context("failed to open or create nostr cache database at <git-dir>/nostr-cache.lmdb")
}

async fn get_global_cache_database(git_repo_path: Option<&Path>) -> Result<NostrLMDB> {
    let path = if std::env::var("NGITTEST").is_ok() {
        if let Some(git_repo_path) = git_repo_path {
            let git_dir = git2::Repository::discover(git_repo_path)
                .context("failed to discover git repository")?
                .commondir()
                .to_path_buf();
            git_dir.join("test-global-cache.lmdb")
        } else {
            bail!("git_repo must be supplied to get_global_cache_database during integration tests")
        }
    } else {
        create_dir_all(get_dirs()?.cache_dir()).context(format!(
            "failed to create cache directory in: {:?}",
            get_dirs()?.cache_dir()
        ))?;
        get_dirs()?.cache_dir().join("nostr-cache.lmdb")
    };

    NostrLMDB::open(path).context("failed to open ngit global nostr cache database")
}

pub async fn get_events_from_local_cache(
    git_repo_path: &Path,
    filters: Vec<nostr::Filter>,
) -> Result<Vec<nostr::Event>> {
    let db = get_local_cache_database(git_repo_path).await?;

    let query_results = join_all(filters.into_iter().map(|filter| async {
        db.query(filter)
            .await
            .context("failed to execute query on opened ngit nostr cache database")
    }))
    .await;

    // no Event is being mutated, just new items added to the set
    #[allow(clippy::mutable_key_type)]
    let mut events: HashSet<Event> = HashSet::new();

    for result in query_results {
        events.extend(result?);
    }

    Ok(events.into_iter().collect())
}

pub async fn get_event_from_global_cache(
    git_repo_path: Option<&Path>,
    filters: Vec<nostr::Filter>,
) -> Result<Vec<nostr::Event>> {
    let db = get_global_cache_database(git_repo_path).await?;

    let query_results = join_all(filters.into_iter().map(|filter| async {
        db.query(filter)
            .await
            .context("failed to execute query on opened ngit nostr cache database")
    }))
    .await;

    // no Event is being mutated, just new items added to the set
    #[allow(clippy::mutable_key_type)]
    let mut events: HashSet<Event> = HashSet::new();

    for result in query_results {
        events.extend(result?);
    }

    Ok(events.into_iter().collect())
}

pub async fn save_event_in_local_cache(git_repo_path: &Path, event: &nostr::Event) -> Result<bool> {
    match get_local_cache_database(git_repo_path)
        .await?
        .save_event(event)
        .await
        .context("failed to save event in local cache")?
    {
        SaveEventStatus::Success => Ok(true),
        _ => Ok(false),
    }
}

pub async fn save_event_in_global_cache(
    git_repo_path: Option<&Path>,
    event: &nostr::Event,
) -> Result<bool> {
    match get_global_cache_database(git_repo_path)
        .await?
        .save_event(event)
        .await
        .context("failed to save event in local cache")
    {
        Ok(SaveEventStatus::Success) => Ok(true),
        Ok(_) => Ok(false),
        Err(e) => Err(e).context("failed to save event in local cache"),
    }
}

// use annoucement from trusted maintainer but recursively add maintainers, git
// servers and relays
pub async fn get_repo_ref_from_cache(
    git_repo_path: Option<&Path>,
    repo_coordinate: &Nip19Coordinate,
) -> Result<RepoRef> {
    let mut maintainers = HashSet::new();
    let mut new_coordinate: bool;

    maintainers.insert(repo_coordinate.public_key);
    let mut repo_events = vec![];
    loop {
        new_coordinate = false;
        let repo_events_filter = get_filter_repo_ann_events(
            &HashSet::from_iter(maintainers.iter().map(|m| Nip19Coordinate {
                coordinate: Coordinate {
                    kind: Kind::GitRepoAnnouncement,
                    public_key: *m,
                    identifier: repo_coordinate.identifier.to_string(),
                },
                relays: vec![],
            })),
            true,
        );

        let events = [
            get_event_from_global_cache(git_repo_path, vec![repo_events_filter.clone()]).await?,
            if let Some(git_repo_path) = git_repo_path {
                get_events_from_local_cache(git_repo_path, vec![repo_events_filter]).await?
            } else {
                vec![]
            },
        ]
        .concat();
        for e in events {
            if let Ok(repo_ref) = RepoRef::try_from((e.clone(), None)) {
                for m in repo_ref.maintainers {
                    if maintainers.insert(m) {
                        new_coordinate = true;
                    }
                }
                repo_events.push(e);
            }
        }
        if !new_coordinate {
            break;
        }
    }
    repo_events.sort_by_key(|e| e.created_at);
    let repo_ref = RepoRef::try_from((
        repo_events
            .iter()
            .find(|e| e.pubkey == repo_coordinate.public_key)
            .context("no repo announcement event found at specified Nip19Coordinates. if you are the repository maintainer consider running `ngit init` to create one")?
            .clone(),
        Some(repo_coordinate.public_key),
    ))?;

    // Use name/description/web from the latest event across all maintainers
    let latest_metadata = repo_events
        .last()
        .and_then(|e| RepoRef::try_from((e.clone(), None)).ok());

    let mut events: HashMap<Nip19Coordinate, nostr::Event> = HashMap::new();
    for m in &maintainers {
        if let Some(e) = repo_events.iter().find(|e| e.pubkey.eq(m)) {
            events.insert(
                Nip19Coordinate {
                    coordinate: Coordinate {
                        kind: e.kind,
                        identifier: e.tags.identifier().unwrap().to_string(),
                        public_key: e.pubkey,
                    },
                    relays: vec![],
                },
                e.clone(),
            );
        }
    }

    // Use relays, git and blossom servers from all maintainer announcement events
    // we use Vec and HashSet to remove duplicates and preserve order
    let mut relays: Vec<RelayUrl> = repo_ref.relays.clone();
    let mut git_server: Vec<String> = repo_ref.git_server.clone();
    let mut blossoms: Vec<Url> = repo_ref.blossoms.clone();
    let mut seen_relays: HashSet<RelayUrl> = HashSet::from_iter(relays.iter().cloned());
    let mut seen_git_server: HashSet<String> = git_server
        .iter()
        .map(|server| server.trim_end_matches('/').to_string())
        .collect();
    let mut seen_blossoms: HashSet<Url> = HashSet::from_iter(blossoms.iter().cloned());

    // also set maintainers_without_annoucnement
    let mut maintainers_without_annoucnement: Vec<PublicKey> = vec![];

    for m in &maintainers {
        if let Some(event) = repo_events.iter().find(|e| e.pubkey == *m) {
            if let Ok(m_repo_ref) = RepoRef::try_from((event.clone(), None)) {
                for relay in m_repo_ref.relays {
                    if seen_relays.insert(relay.clone()) {
                        relays.push(relay);
                    }
                }
                for server in m_repo_ref.git_server {
                    if seen_git_server.insert(server.trim_end_matches('/').to_string()) {
                        git_server.push(server);
                    }
                }
                for blossom in m_repo_ref.blossoms {
                    if seen_blossoms.insert(blossom.clone()) {
                        blossoms.push(blossom);
                    }
                }
            }
        } else {
            maintainers_without_annoucnement.push(*m);
        }
    }

    Ok(RepoRef {
        // use all maintainers from all events found, not just maintainers in the most
        // recent event
        maintainers: maintainers.iter().copied().collect::<Vec<PublicKey>>(),
        relays,
        git_server,
        events,
        maintainers_without_annoucnement: Some(maintainers_without_annoucnement),
        name: latest_metadata
            .as_ref()
            .map_or_else(|| repo_ref.name.clone(), |r| r.name.clone()),
        description: latest_metadata
            .as_ref()
            .map_or_else(|| repo_ref.description.clone(), |r| r.description.clone()),
        web: latest_metadata
            .as_ref()
            .map_or_else(|| repo_ref.web.clone(), |r| r.web.clone()),
        ..repo_ref
    })
}

pub async fn get_state_from_cache(
    git_repo_path: Option<&Path>,
    repo_ref: &RepoRef,
) -> Result<RepoState> {
    if let Some(git_repo_path) = git_repo_path {
        RepoState::try_from(
            get_events_from_local_cache(
                git_repo_path,
                vec![get_filter_state_events(&repo_ref.coordinates(), true)],
            )
            .await?,
        )
    } else {
        RepoState::try_from(
            get_event_from_global_cache(
                git_repo_path,
                vec![get_filter_state_events(&repo_ref.coordinates(), true)],
            )
            .await?,
        )
    }
}

#[allow(clippy::too_many_lines)]
async fn create_relays_request(
    git_repo_path: Option<&Path>,
    trusted_maintainer_coordinate: Option<&Nip19Coordinate>,
    user_profiles: &HashSet<PublicKey>,
    fallback_relays: HashSet<RelayUrl>,
) -> Result<FetchRequest> {
    let repo_ref = if let Some(trusted_maintainer_coordinate) = trusted_maintainer_coordinate {
        (get_repo_ref_from_cache(git_repo_path, trusted_maintainer_coordinate).await).ok()
    } else {
        None
    };

    let repo_coordinates = {
        // add Nip19Coordinates of users listed in maintainers to explicitly
        // specified coodinates
        let mut set: HashSet<Nip19Coordinate> = HashSet::new();
        if let Some(trusted_maintainer_coordinate) = trusted_maintainer_coordinate {
            set.insert(trusted_maintainer_coordinate.clone());
        }
        if let Some(repo_ref) = &repo_ref {
            for c in repo_ref.coordinates() {
                if !set
                    .iter()
                    .any(|e| e.identifier.eq(&c.identifier) && e.public_key.eq(&c.public_key))
                {
                    set.insert(c);
                }
            }
        }
        set
    };

    let repo_coordinates_without_relays = {
        let mut set = HashSet::new();
        for c in &repo_coordinates {
            set.insert(Nip19Coordinate {
                coordinate: Coordinate {
                    kind: c.kind,
                    identifier: c.identifier.clone(),
                    public_key: c.public_key,
                },
                relays: vec![],
            });
        }
        set
    };

    let mut proposals: HashSet<EventId> = HashSet::new();
    let mut issue_ids: HashSet<EventId> = HashSet::new();
    let mut missing_contributor_profiles: HashSet<PublicKey> = HashSet::new();
    let mut contributors: HashSet<PublicKey> = HashSet::new();

    if !repo_coordinates_without_relays.is_empty() {
        if let Some(repo_ref) = &repo_ref {
            for m in &repo_ref.maintainers {
                contributors.insert(m.to_owned());
            }
        }

        if let Some(git_repo_path) = git_repo_path {
            for event in &get_events_from_local_cache(
                git_repo_path,
                vec![
                    nostr::Filter::default()
                        .kinds(vec![Kind::GitPatch, KIND_PULL_REQUEST, Kind::GitIssue])
                        .custom_tags(
                            SingleLetterTag::lowercase(nostr_sdk::Alphabet::A),
                            repo_coordinates_without_relays
                                .iter()
                                .map(|c| c.coordinate.to_string())
                                .collect::<Vec<String>>(),
                        ),
                ],
            )
            .await?
            {
                if event_is_patch_set_root(event)
                    || event_is_revision_root(event)
                    || event.kind.eq(&KIND_PULL_REQUEST)
                {
                    proposals.insert(event.id);
                    contributors.insert(event.pubkey);
                } else if event.kind.eq(&Kind::GitIssue) {
                    issue_ids.insert(event.id);
                    contributors.insert(event.pubkey);
                }
            }
        }

        let profile_events = get_event_from_global_cache(
            git_repo_path,
            vec![get_filter_contributor_profiles(contributors.clone())],
        )
        .await?;
        for c in &contributors {
            if let Some(event) = profile_events
                .iter()
                .find(|e| e.kind == Kind::Metadata && e.pubkey.eq(c))
            {
                if let Some(git_repo_path) = git_repo_path {
                    save_event_in_local_cache(git_repo_path, event).await?;
                }
            } else {
                missing_contributor_profiles.insert(c.to_owned());
            }
        }
    }

    let profiles_to_fetch_from_user_relays = {
        let mut user_profiles = user_profiles.clone();
        if let Some(git_repo_path) = git_repo_path {
            if let Ok(Some(current_user)) = get_likely_logged_in_user(git_repo_path).await {
                user_profiles.insert(current_user);
            }
        }
        let mut map: HashMap<PublicKey, (Timestamp, Timestamp, Timestamp)> = HashMap::new();
        for public_key in &user_profiles {
            if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await {
                map.insert(
                    public_key.to_owned(),
                    (
                        user_ref.metadata.created_at,
                        user_ref.relays.created_at,
                        user_ref.grasp_list.created_at,
                    ),
                );
            } else {
                map.insert(
                    public_key.to_owned(),
                    (Timestamp::from(0), Timestamp::from(0), Timestamp::from(0)),
                );
            }
        }
        map
    };

    let user_relays_for_profiles = {
        let mut set = HashSet::new();
        for user in &profiles_to_fetch_from_user_relays
            .clone()
            .into_keys()
            .collect::<Vec<PublicKey>>()
        {
            if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, user).await {
                for r in user_ref.relays.write() {
                    if let Ok(url) = RelayUrl::parse(&r) {
                        set.insert(url);
                    }
                }
            } else {
                missing_contributor_profiles.insert(user.to_owned());
            }
        }
        set
    };

    let existing_events: HashSet<EventId> = {
        let mut existing_events: HashSet<EventId> = HashSet::new();
        for filter in get_fetch_filters(
            &repo_coordinates_without_relays,
            &proposals,
            &issue_ids,
            &HashSet::new(), /* non_proposal_event_ids not yet computed; deletion events are not
                              * cached locally */
            &missing_contributor_profiles
                .union(
                    &profiles_to_fetch_from_user_relays
                        .clone()
                        .into_keys()
                        .collect::<HashSet<PublicKey>>(),
                )
                .copied()
                .collect(),
        ) {
            if let Some(git_repo_path) = git_repo_path {
                for (id, _) in get_local_cache_database(git_repo_path)
                    .await?
                    .negentropy_items(filter.clone())
                    .await?
                {
                    existing_events.insert(id);
                }
            }
            // Also check global cache for profile events to avoid re-fetching
            if filter.kinds.as_ref().is_some_and(|kinds| {
                kinds.iter().any(|k| {
                    k.eq(&Kind::Metadata) || k.eq(&Kind::RelayList) || k.eq(&KIND_USER_GRASP_LIST)
                })
            }) {
                for (id, _) in get_global_cache_database(git_repo_path)
                    .await?
                    .negentropy_items(filter)
                    .await?
                {
                    existing_events.insert(id);
                }
            }
        }
        existing_events
    };

    let relays = {
        // Only use fallback relays for bootstrapping (no repo context).
        // When we have a repo coordinate, rely on repo relays and coordinate
        // hint relays instead of always merging in the default set.
        let mut relays = if trusted_maintainer_coordinate.is_none() {
            fallback_relays.clone()
        } else {
            HashSet::new()
        };
        if let Some(repo_ref) = &repo_ref {
            for r in repo_ref.relays.clone() {
                relays.insert(r);
            }
        }
        for c in repo_coordinates {
            for r in &c.relays {
                relays.insert(r.clone());
            }
        }
        // Fall back to fallback relays when the coordinate had no relay hints
        // and nothing is cached yet (e.g. fresh clone with a bare npub URL).
        if relays.is_empty() {
            relays = fallback_relays;
        }
        relays
    };

    let relay_column_width = relays
        .union(&user_relays_for_profiles)
        .reduce(|a, r| {
            if r.to_string()
                .chars()
                .count()
                .gt(&a.to_string().chars().count())
            {
                r
            } else {
                a
            }
        })
        .map_or(0, |r| r.to_string().chars().count() + 2);

    Ok(FetchRequest {
        selected_relay: None,
        repo_relays: relays,
        relay_column_width,
        repo_coordinates_without_relays: if let Some(repo_ref) = &repo_ref {
            repo_ref.coordinates_with_timestamps()
        } else {
            repo_coordinates_without_relays
                .iter()
                .map(|c| (c.clone(), None))
                .collect()
        },
        state: if let Some(repo_ref) = &repo_ref {
            if let Ok(existing_state) = get_state_from_cache(git_repo_path, repo_ref).await {
                Some((existing_state.event.created_at, existing_state.event.id))
            } else {
                None
            }
        } else {
            None
        },
        non_proposal_event_ids: {
            let mut ids: HashSet<EventId> = HashSet::new();
            // Include repo announcement event IDs so we can request kind-5
            // deletions for them by #e tag (NIP-09 style).
            if let Some(repo_ref) = &repo_ref {
                for event in repo_ref.events.values() {
                    ids.insert(event.id);
                }
                // Also include the state event ID if we have one.
                if let Ok(existing_state) = get_state_from_cache(git_repo_path, repo_ref).await {
                    ids.insert(existing_state.event.id);
                }
            }
            ids
        },
        proposals,
        issue_ids,
        contributors,
        missing_contributor_profiles,
        existing_events,
        profiles_to_fetch_from_user_relays,
        user_relays_for_profiles,
    })
}

#[allow(clippy::too_many_lines, clippy::too_many_arguments)]
async fn process_fetched_events(
    events: Vec<nostr::Event>,
    request: &FetchRequest,
    git_repo_path: Option<&Path>,
    fresh_coordinates: &mut HashSet<Nip19Coordinate>,
    fresh_proposal_roots: &mut HashSet<EventId>,
    fresh_issue_roots: &mut HashSet<EventId>,
    fresh_profiles: &mut HashSet<PublicKey>,
    report: &mut FetchReport,
) -> Result<()> {
    for event in &events {
        if !request.existing_events.contains(&event.id) {
            if let Some(git_repo_path) = git_repo_path {
                save_event_in_local_cache(git_repo_path, event).await?;
            }
            if event.kind.eq(&Kind::GitRepoAnnouncement) {
                save_event_in_global_cache(git_repo_path, event).await?;
                let new_coordinate = !request
                    .repo_coordinates_without_relays
                    .iter()
                    .map(|(c, _)| c.clone())
                    .any(|c| {
                        c.identifier.eq(event.tags.identifier().unwrap())
                            && c.public_key.eq(&event.pubkey)
                    });
                let update_to_existing = !new_coordinate
                    && request
                        .repo_coordinates_without_relays
                        .iter()
                        .any(|(c, t)| {
                            c.identifier.eq(event.tags.identifier().unwrap())
                                && c.public_key.eq(&event.pubkey)
                                && if let Some(t) = t {
                                    event.created_at.gt(t)
                                } else {
                                    true
                                }
                        });
                if update_to_existing {
                    report.updated_repo_announcements.push((
                        Nip19Coordinate {
                            coordinate: Coordinate {
                                kind: event.kind,
                                public_key: event.pubkey,
                                identifier: event.tags.identifier().unwrap().to_owned(),
                            },
                            relays: vec![],
                        },
                        event.created_at,
                    ));
                }
                // if contains announcement
                if let Ok(repo_ref) = &RepoRef::try_from((event.clone(), None)) {
                    for m in &repo_ref.maintainers {
                        if !request
                            .repo_coordinates_without_relays // prexisting maintainers
                            .iter()
                            .map(|(c, _)| c.clone())
                            .collect::<HashSet<Nip19Coordinate>>()
                            .union(&report.repo_coordinates_without_relays) // already added maintainers
                            .any(|c| c.identifier.eq(&repo_ref.identifier) && m.eq(&c.public_key))
                        {
                            let c = Nip19Coordinate {
                                coordinate: Coordinate {
                                    kind: event.kind,
                                    public_key: *m,
                                    identifier: repo_ref.identifier.clone(),
                                },
                                relays: vec![],
                            };
                            fresh_coordinates.insert(c.clone());
                            report.repo_coordinates_without_relays.insert(c);

                            if !request.contributors.contains(m)
                                && !request
                                    .profiles_to_fetch_from_user_relays
                                    .clone()
                                    .into_keys()
                                    .collect::<HashSet<PublicKey>>()
                                    .contains(m)
                                && !fresh_profiles.contains(m)
                            {
                                fresh_profiles.insert(m.to_owned());
                            }
                        }
                    }
                }
            } else if event.kind.eq(&STATE_KIND) {
                let existing_state = if report.updated_state.is_some() {
                    report.updated_state
                } else {
                    request.state
                };
                if let Some((timestamp, id)) = existing_state {
                    if event.created_at.gt(&timestamp)
                        || (event.created_at.eq(&timestamp) && event.id.gt(&id))
                    {
                        report.updated_state = Some((event.created_at, event.id));
                    }
                }
            } else if event.kind.eq(&Kind::EventDeletion) {
                report.deletions += 1;
            } else if event_is_patch_set_root(event) || event.kind.eq(&KIND_PULL_REQUEST) {
                fresh_proposal_roots.insert(event.id);
                report.proposals.insert(event.id);
                if !request.contributors.contains(&event.pubkey)
                    && !fresh_profiles.contains(&event.pubkey)
                {
                    fresh_profiles.insert(event.pubkey);
                }
            } else if event.kind.eq(&Kind::GitIssue) {
                fresh_issue_roots.insert(event.id);
                report.issues.insert(event.id);
                if !request.contributors.contains(&event.pubkey)
                    && !fresh_profiles.contains(&event.pubkey)
                {
                    fresh_profiles.insert(event.pubkey);
                }
            } else if event.kind.eq(&KIND_COMMENT) {
                report.comments.insert(event.id);
            } else if event.kind.eq(&KIND_LABEL) {
                report.labels.insert(event.id);
            } else if event.kind.eq(&KIND_COVER_NOTE) {
                report.cover_notes.insert(event.id);
            } else if [Kind::RelayList, Kind::Metadata, KIND_USER_GRASP_LIST].contains(&event.kind)
            {
                if request.missing_contributor_profiles.contains(&event.pubkey) {
                    report.contributor_profiles.insert(event.pubkey);
                } else if let Some((
                    _,
                    (metadata_timestamp, relay_list_timestamp, grasp_list_timestamp),
                )) = request
                    .profiles_to_fetch_from_user_relays
                    .get_key_value(&event.pubkey)
                {
                    if (Kind::Metadata.eq(&event.kind) && event.created_at.gt(metadata_timestamp))
                        || (Kind::RelayList.eq(&event.kind)
                            && event.created_at.gt(relay_list_timestamp))
                        || (KIND_USER_GRASP_LIST.eq(&event.kind)
                            && event.created_at.gt(grasp_list_timestamp))
                    {
                        report.profile_updates.insert(event.pubkey);
                    }
                }
                save_event_in_global_cache(git_repo_path, event).await?;
            }
        }
    }
    for event in &events {
        if !request.existing_events.contains(&event.id) {
            let tagged_root_id = event.tags.iter().find_map(|t| {
                if t.as_slice().len() > 1 && (t.as_slice()[0].eq("E") || t.as_slice()[0].eq("e")) {
                    EventId::parse(&t.as_slice()[1]).ok()
                } else {
                    None
                }
            });
            if status_kinds().contains(&event.kind) {
                // Route status events to the correct counter based on whether
                // the root event is a known issue or a proposal (patch/PR).
                // Don't double-count statuses that arrived in the same batch
                // as their parent (new issues/proposals already inflate the count).
                if let Some(root_id) = &tagged_root_id {
                    if report.issues.contains(root_id) {
                        // status for a new issue in this batch — skip (counted
                        // via issues)
                    } else if report.proposals.contains(root_id) {
                        // status for a new proposal in this batch — skip
                        // (counted via proposals)
                    } else if request.issue_ids.contains(root_id) {
                        report.issue_statuses.insert(event.id);
                    } else {
                        report.statuses.insert(event.id);
                    }
                }
            } else {
                // Non-status events: commits/PR-updates for proposals only.
                let not_tagged_with_new_proposal = tagged_root_id
                    .as_ref()
                    .is_none_or(|id| !report.proposals.contains(id));
                if not_tagged_with_new_proposal
                    && ((event.kind.eq(&Kind::GitPatch) && !event_is_patch_set_root(event))
                        || event.kind.eq(&KIND_PULL_REQUEST_UPDATE))
                {
                    report.commits.insert(event.id);
                }
            }
        }
    }
    Ok(())
}

pub fn consolidate_fetch_reports(reports: Vec<Result<FetchReport>>) -> FetchReport {
    let mut report = FetchReport::default();
    for relay_report in reports.into_iter().flatten() {
        for c in relay_report.repo_coordinates_without_relays {
            if !report
                .repo_coordinates_without_relays
                .iter()
                .any(|e| e.eq(&c))
            {
                report.repo_coordinates_without_relays.insert(c);
            }
        }
        for (r, t) in relay_report.updated_repo_announcements {
            if let Some(i) = report
                .updated_repo_announcements
                .iter()
                .position(|(e, _)| e.eq(&r))
            {
                let (_, existing_t) = &report.updated_repo_announcements[i];
                if t.gt(existing_t) {
                    report.updated_repo_announcements[i] = (r, t);
                }
            } else {
                report.updated_repo_announcements.push((r, t));
            }
        }
        if let Some((timestamp, id)) = relay_report.updated_state {
            if let Some((existing_timestamp, existing_id)) = report.updated_state {
                if timestamp.gt(&existing_timestamp)
                    || (timestamp.eq(&existing_timestamp) && id.gt(&existing_id))
                {
                    report.updated_state = Some((timestamp, id));
                }
            } else {
                report.updated_state = Some((timestamp, id));
            }
        }
        for c in relay_report.proposals {
            report.proposals.insert(c);
        }
        for c in relay_report.commits {
            report.commits.insert(c);
        }
        for c in relay_report.statuses {
            report.statuses.insert(c);
        }
        for c in relay_report.issues {
            report.issues.insert(c);
        }
        for c in relay_report.issue_statuses {
            report.issue_statuses.insert(c);
        }
        for c in relay_report.comments {
            report.comments.insert(c);
        }
        for c in relay_report.labels {
            report.labels.insert(c);
        }
        for c in relay_report.cover_notes {
            report.cover_notes.insert(c);
        }
        report.deletions += relay_report.deletions;
        for c in relay_report.contributor_profiles {
            report.contributor_profiles.insert(c);
        }
        for c in relay_report.profile_updates {
            report.profile_updates.insert(c);
        }
        // Per-relay state events are independent: each relay entry is kept as-is.
        // If a relay appears in multiple per-relay reports (shouldn't happen in
        // practice but possible in tests), keep the newer event.
        for (relay_url, maybe_event) in relay_report.state_per_relay {
            match report.state_per_relay.entry(relay_url) {
                std::collections::hash_map::Entry::Vacant(e) => {
                    e.insert(maybe_event);
                }
                std::collections::hash_map::Entry::Occupied(mut e) => {
                    let keep = match (e.get(), &maybe_event) {
                        (None, Some(_)) => true,
                        (Some(existing), Some(incoming)) => {
                            incoming.created_at.gt(&existing.created_at)
                                || (incoming.created_at.eq(&existing.created_at)
                                    && incoming.id.gt(&existing.id))
                        }
                        _ => false,
                    };
                    if keep {
                        e.insert(maybe_event);
                    }
                }
            }
        }
    }
    report
}
pub fn get_fetch_filters(
    repo_coordinates: &HashSet<Nip19Coordinate>,
    proposal_ids: &HashSet<EventId>,
    issue_ids: &HashSet<EventId>,
    non_proposal_event_ids: &HashSet<EventId>,
    required_profiles: &HashSet<PublicKey>,
) -> Vec<nostr::Filter> {
    [
        if repo_coordinates.is_empty() {
            vec![]
        } else {
            vec![
                get_filter_state_events(repo_coordinates, false),
                get_filter_repo_ann_events(repo_coordinates, false),
                nostr::Filter::default()
                    .kinds(vec![
                        Kind::GitPatch,
                        Kind::EventDeletion,
                        KIND_PULL_REQUEST,
                        Kind::GitIssue,
                    ])
                    .custom_tags(
                        SingleLetterTag::lowercase(nostr_sdk::Alphabet::A),
                        repo_coordinates
                            .iter()
                            .map(|c| c.coordinate.to_string())
                            .collect::<Vec<String>>(),
                    ),
            ]
        },
        if proposal_ids.is_empty() {
            vec![]
        } else {
            vec![
                nostr::Filter::default().events(proposal_ids.clone()).kinds(
                    [
                        vec![
                            Kind::GitPatch,
                            Kind::EventDeletion,
                            KIND_PULL_REQUEST_UPDATE,
                        ],
                        status_kinds(),
                    ]
                    .concat(),
                ),
                nostr::Filter::default()
                    .custom_tags(
                        SingleLetterTag::uppercase(Alphabet::E),
                        proposal_ids.clone(),
                    )
                    .kinds(
                        [
                            vec![Kind::EventDeletion, KIND_PULL_REQUEST_UPDATE],
                            status_kinds(),
                        ]
                        .concat(),
                    ),
            ]
        },
        // Fetch status events for known issues.
        if issue_ids.is_empty() {
            vec![]
        } else {
            vec![
                nostr::Filter::default()
                    .events(issue_ids.clone())
                    .kinds(status_kinds()),
                nostr::Filter::default()
                    .custom_tags(SingleLetterTag::uppercase(Alphabet::E), issue_ids.clone())
                    .kinds(status_kinds()),
            ]
        },
        // Fetch NIP-22 kind-1111 comments for issues and proposals (patches/PRs).
        // Comments use an uppercase `E` tag pointing to the root event ID.
        {
            let all_root_ids: HashSet<EventId> = issue_ids
                .iter()
                .chain(proposal_ids.iter())
                .copied()
                .collect();
            if all_root_ids.is_empty() {
                vec![]
            } else {
                vec![
                    nostr::Filter::default()
                        .custom_tags(SingleLetterTag::uppercase(Alphabet::E), all_root_ids)
                        .kind(KIND_COMMENT),
                ]
            }
        },
        // Fetch NIP-32 kind-1985 label events for issues and proposals.
        // Label events reference the target via a lowercase `e` tag.
        {
            let all_root_ids: HashSet<EventId> = issue_ids
                .iter()
                .chain(proposal_ids.iter())
                .copied()
                .collect();
            if all_root_ids.is_empty() {
                vec![]
            } else {
                vec![
                    nostr::Filter::default()
                        .events(all_root_ids)
                        .kind(KIND_LABEL),
                ]
            }
        },
        // Fetch kind-1624 cover note events for issues and proposals.
        // Cover notes reference the target via a lowercase `e` tag.
        {
            let all_root_ids: HashSet<EventId> = issue_ids
                .iter()
                .chain(proposal_ids.iter())
                .copied()
                .collect();
            if all_root_ids.is_empty() {
                vec![]
            } else {
                vec![
                    nostr::Filter::default()
                        .events(all_root_ids)
                        .kind(KIND_COVER_NOTE),
                ]
            }
        },
        // Request kind-5 deletions for state events and repo announcements by
        // their event ID (#e tag), as per NIP-09. The #a-tagged filter above
        // covers addressable-event deletions; this covers the specific event IDs
        // of the state and announcement events we already have cached.
        if non_proposal_event_ids.is_empty() {
            vec![]
        } else {
            vec![
                nostr::Filter::default()
                    .kind(Kind::EventDeletion)
                    .events(non_proposal_event_ids.clone()),
            ]
        },
        if required_profiles.is_empty() {
            vec![]
        } else {
            vec![get_filter_contributor_profiles(required_profiles.clone())]
        },
    ]
    .concat()
}

pub fn get_filter_repo_ann_events(
    repo_coordinates: &HashSet<Nip19Coordinate>,
    maintainers_only: bool,
) -> nostr::Filter {
    let filter = nostr::Filter::default()
        .kind(Kind::GitRepoAnnouncement)
        .identifiers(
            repo_coordinates
                .iter()
                .map(|c| c.identifier.clone())
                .collect::<Vec<String>>(),
        );
    if maintainers_only {
        filter.authors(
            repo_coordinates
                .iter()
                .map(|c| c.coordinate.public_key)
                .collect::<Vec<PublicKey>>(),
        )
    } else {
        filter
    }
}

pub static STATE_KIND: nostr::Kind = Kind::Custom(30618);
pub fn get_filter_state_events(
    repo_coordinates: &HashSet<Nip19Coordinate>,
    maintainers_only: bool,
) -> nostr::Filter {
    let filter = nostr::Filter::default().kind(STATE_KIND).identifiers(
        repo_coordinates
            .iter()
            .map(|c| c.identifier.clone())
            .collect::<Vec<String>>(),
    );
    if maintainers_only {
        filter.authors(
            repo_coordinates
                .iter()
                .map(|c| c.coordinate.public_key)
                .collect::<Vec<PublicKey>>(),
        )
    } else {
        filter
    }
}

pub fn get_filter_contributor_profiles(contributors: HashSet<PublicKey>) -> nostr::Filter {
    nostr::Filter::default()
        .kinds(vec![Kind::Metadata, Kind::RelayList, KIND_USER_GRASP_LIST])
        .authors(contributors)
}

#[derive(Default)]
pub struct FetchReport {
    repo_coordinates_without_relays: HashSet<Nip19Coordinate>,
    updated_repo_announcements: Vec<(Nip19Coordinate, Timestamp)>,
    updated_state: Option<(Timestamp, EventId)>,
    proposals: HashSet<EventId>,
    /// commits against existing propoals
    commits: HashSet<EventId>,
    statuses: HashSet<EventId>,
    issues: HashSet<EventId>,
    issue_statuses: HashSet<EventId>,
    /// NIP-22 kind-1111 comments against issues, patches, and PRs.
    comments: HashSet<EventId>,
    /// NIP-32 kind-1985 label events for issues and proposals.
    labels: HashSet<EventId>,
    /// Kind-1624 cover note events for issues, patches, and PRs.
    cover_notes: HashSet<EventId>,
    /// Count of kind-5 deletion events received (for display purposes).
    deletions: u32,
    contributor_profiles: HashSet<PublicKey>,
    profile_updates: HashSet<PublicKey>,
    /// The best (newest) state event seen on each relay during the fetch.
    /// `None` as a value means the relay was queried but returned no state
    /// event at all.  Relays that were never queried are absent from the map.
    /// This is the only point at which per-relay state visibility is available;
    /// the local database only stores the canonical latest event.
    pub state_per_relay: HashMap<RelayUrl, Option<nostr::Event>>,
}

impl Display for FetchReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // report: "1 announcement, 1 announcement, 1 proposal, 3 commits, 2
        // statuses"
        let mut display_items: Vec<String> = vec![];
        if !self.repo_coordinates_without_relays.is_empty() {
            display_items.push(format!(
                "{} announcement{}",
                self.repo_coordinates_without_relays.len(),
                if self.repo_coordinates_without_relays.len() > 1 {
                    "s"
                } else {
                    ""
                },
            ));
        }
        if !self.updated_repo_announcements.is_empty() {
            display_items.push(format!(
                "{} announcement update{}",
                self.updated_repo_announcements.len(),
                if self.updated_repo_announcements.len() > 1 {
                    "s"
                } else {
                    ""
                },
            ));
        }
        if self.updated_state.is_some() {
            display_items.push("new state".to_string());
        }
        if !self.proposals.is_empty() {
            display_items.push(format!(
                "{} proposal{}",
                self.proposals.len(),
                if self.proposals.len() > 1 { "s" } else { "" },
            ));
        }
        if !self.commits.is_empty() {
            display_items.push(format!(
                "{} commit{}",
                self.commits.len(),
                if self.commits.len() > 1 { "s" } else { "" },
            ));
        }
        if !self.statuses.is_empty() {
            display_items.push(format!(
                "{} status{}",
                self.statuses.len(),
                if self.statuses.len() > 1 { "es" } else { "" },
            ));
        }
        if !self.issues.is_empty() {
            display_items.push(format!(
                "{} issue{}",
                self.issues.len(),
                if self.issues.len() > 1 { "s" } else { "" },
            ));
        }
        if !self.issue_statuses.is_empty() {
            display_items.push(format!(
                "{} issue status{}",
                self.issue_statuses.len(),
                if self.issue_statuses.len() > 1 {
                    "es"
                } else {
                    ""
                },
            ));
        }
        if !self.comments.is_empty() {
            display_items.push(format!(
                "{} comment{}",
                self.comments.len(),
                if self.comments.len() > 1 { "s" } else { "" },
            ));
        }
        if !self.labels.is_empty() {
            display_items.push(format!(
                "{} label{}",
                self.labels.len(),
                if self.labels.len() > 1 { "s" } else { "" },
            ));
        }
        if !self.cover_notes.is_empty() {
            display_items.push(format!(
                "{} cover note{}",
                self.cover_notes.len(),
                if self.cover_notes.len() > 1 { "s" } else { "" },
            ));
        }
        if self.deletions > 0 {
            display_items.push(format!(
                "{} deletion{}",
                self.deletions,
                if self.deletions > 1 { "s" } else { "" },
            ));
        }
        if !self.contributor_profiles.is_empty() {
            display_items.push(format!(
                "{} user profile{}",
                self.contributor_profiles.len(),
                if self.contributor_profiles.len() > 1 {
                    "s"
                } else {
                    ""
                },
            ));
        }
        if !self.profile_updates.is_empty() {
            display_items.push(format!(
                "{} profile update{}",
                self.profile_updates.len(),
                if self.profile_updates.len() > 1 {
                    "s"
                } else {
                    ""
                },
            ));
        }
        write!(f, "{}", display_items.join(", "))
    }
}

#[derive(Default, Clone)]
pub struct FetchRequest {
    repo_relays: HashSet<RelayUrl>,
    selected_relay: Option<RelayUrl>,
    relay_column_width: usize,
    repo_coordinates_without_relays: Vec<(Nip19Coordinate, Option<Timestamp>)>,
    state: Option<(Timestamp, EventId)>,
    proposals: HashSet<EventId>,
    /// Known issue event IDs, used to fetch their status events.
    issue_ids: HashSet<EventId>,
    /// Event IDs of non-proposal events (state events, repo announcements) for
    /// which we should also request kind-5 deletion events by `#e` tag.
    non_proposal_event_ids: HashSet<EventId>,
    contributors: HashSet<PublicKey>,
    missing_contributor_profiles: HashSet<PublicKey>,
    existing_events: HashSet<EventId>,
    profiles_to_fetch_from_user_relays: HashMap<PublicKey, (Timestamp, Timestamp, Timestamp)>,
    user_relays_for_profiles: HashSet<RelayUrl>,
}

pub async fn fetching_with_report(
    git_repo_path: &Path,
    #[cfg(test)] client: &crate::client::MockConnect,
    #[cfg(not(test))] client: &Client,
    trusted_maintainer_coordinate: &Nip19Coordinate,
) -> Result<FetchReport> {
    let verbose = is_verbose();
    if verbose {
        let term = console::Term::stderr();
        term.write_line("Checking nostr relays...")?;
    }
    let (relay_reports, progress_reporter) = client
        .fetch_all(
            Some(git_repo_path),
            Some(trusted_maintainer_coordinate),
            &HashSet::new(),
        )
        .await?;
    if !relay_reports.iter().any(std::result::Result::is_err) {
        let _ = progress_reporter.clear();
    }
    let report = consolidate_fetch_reports(relay_reports);
    if report.to_string().is_empty() {
        println!("no updates");
    } else {
        println!("updates: {report}");
    }
    Ok(report)
}

/// Like `fetching_with_report` but suppresses the "no updates" / "updates: X"
/// summary line. Returns `true` if any relay reported an error (so the caller
/// can print a blank line to visually separate relay-error output from
/// subsequent content).
pub async fn fetching_quietly(
    git_repo_path: &Path,
    #[cfg(test)] client: &crate::client::MockConnect,
    #[cfg(not(test))] client: &Client,
    trusted_maintainer_coordinate: &Nip19Coordinate,
) -> Result<(FetchReport, bool)> {
    let verbose = is_verbose();
    if verbose {
        let term = console::Term::stderr();
        term.write_line("Checking nostr relays...")?;
    }
    let (relay_reports, progress_reporter) = client
        .fetch_all(
            Some(git_repo_path),
            Some(trusted_maintainer_coordinate),
            &HashSet::new(),
        )
        .await?;
    let had_errors = relay_reports.iter().any(std::result::Result::is_err);
    if !had_errors {
        let _ = progress_reporter.clear();
    }
    // Drop the MultiProgress now so all buffered stderr output is flushed
    // before we write the separator blank line.
    drop(progress_reporter);
    if had_errors {
        let _ = console::Term::stderr().write_line("");
    }
    let report = consolidate_fetch_reports(relay_reports);
    Ok((report, had_errors))
}

pub async fn get_issues_from_cache(
    git_repo_path: &Path,
    repo_coordinates: HashSet<Nip19Coordinate>,
) -> Result<Vec<nostr::Event>> {
    let mut issues = get_events_from_local_cache(
        git_repo_path,
        vec![
            nostr::Filter::default()
                .kinds([nostr::Kind::GitIssue])
                .custom_tags(
                    nostr::SingleLetterTag::lowercase(nostr_sdk::Alphabet::A),
                    repo_coordinates
                        .iter()
                        .map(|c| c.coordinate.to_string())
                        .collect::<Vec<String>>(),
                ),
        ],
    )
    .await?;
    issues.sort_by_key(|e| e.created_at);
    issues.reverse();
    Ok(issues)
}

pub async fn get_proposals_and_revisions_from_cache(
    git_repo_path: &Path,
    repo_coordinates: HashSet<Nip19Coordinate>,
) -> Result<Vec<nostr::Event>> {
    let mut proposals = get_events_from_local_cache(
        git_repo_path,
        vec![
            nostr::Filter::default()
                .kinds([nostr::Kind::GitPatch, KIND_PULL_REQUEST])
                .custom_tags(
                    nostr::SingleLetterTag::lowercase(nostr_sdk::Alphabet::A),
                    repo_coordinates
                        .iter()
                        .map(|c| c.coordinate.to_string())
                        .collect::<Vec<String>>(),
                ),
        ],
    )
    .await?
    .iter()
    .filter(|e| event_is_patch_set_root(e) || e.kind.eq(&KIND_PULL_REQUEST))
    .filter(|e| e.kind.eq(&Kind::GitPatch) || event_is_valid_pr_or_pr_update(e))
    .cloned()
    .collect::<Vec<nostr::Event>>();
    proposals.sort_by_key(|e| e.created_at);
    proposals.reverse();
    Ok(proposals)
}

pub async fn get_all_proposal_patch_pr_pr_update_events_from_cache(
    git_repo_path: &Path,
    repo_ref: &RepoRef,
    proposal_id: &nostr::EventId,
) -> Result<Vec<nostr::Event>> {
    let mut commit_events = get_events_from_local_cache(
        git_repo_path,
        vec![
            nostr::Filter::default()
                .kinds([
                    nostr::Kind::GitPatch,
                    KIND_PULL_REQUEST,
                    KIND_PULL_REQUEST_UPDATE,
                ])
                .event(*proposal_id),
            nostr::Filter::default()
                .kinds([
                    nostr::Kind::GitPatch,
                    KIND_PULL_REQUEST,
                    KIND_PULL_REQUEST_UPDATE,
                ])
                .custom_tag(SingleLetterTag::uppercase(Alphabet::E), *proposal_id),
            nostr::Filter::default()
                .kinds([nostr::Kind::GitPatch, KIND_PULL_REQUEST])
                .id(*proposal_id),
        ],
    )
    .await?;

    let permissioned_users: HashSet<PublicKey> = [
        repo_ref.maintainers.clone(),
        vec![
            commit_events
                .iter()
                .find(|e| e.id.eq(proposal_id))
                .context("proposal not in cache")?
                .pubkey,
        ],
    ]
    .concat()
    .iter()
    .copied()
    .collect();

    commit_events.retain(|e| {
        permissioned_users.contains(&e.pubkey)
            && (e.kind.eq(&Kind::GitPatch) || event_is_valid_pr_or_pr_update(e))
    });

    let revision_roots: HashSet<nostr::EventId> = commit_events
        .iter()
        .filter(|e| event_is_revision_root(e))
        .map(|e| e.id)
        .collect();

    if !revision_roots.is_empty() {
        for event in get_events_from_local_cache(
            git_repo_path,
            vec![
                nostr::Filter::default()
                    .kinds([
                        nostr::Kind::GitPatch,
                        KIND_PULL_REQUEST,
                        KIND_PULL_REQUEST_UPDATE,
                    ])
                    .events(revision_roots.clone())
                    .authors(permissioned_users.clone()),
                nostr::Filter::default()
                    .kinds([
                        nostr::Kind::GitPatch,
                        KIND_PULL_REQUEST,
                        KIND_PULL_REQUEST_UPDATE,
                    ])
                    .custom_tags(SingleLetterTag::uppercase(Alphabet::E), revision_roots)
                    .authors(permissioned_users.clone()),
            ],
        )
        .await?
        {
            commit_events.push(event);
        }
    }

    Ok(commit_events
        .iter()
        .filter(|e| !event_is_cover_letter(e) && permissioned_users.contains(&e.pubkey))
        .cloned()
        .collect())
}

pub async fn get_event_from_cache_by_id(git_repo: &Repo, event_id: &EventId) -> Result<Event> {
    Ok(get_events_from_local_cache(
        git_repo.get_path()?,
        vec![nostr::Filter::default().id(*event_id)],
    )
    .await?
    .first()
    .context("failed to find event in cache")?
    .clone())
}

#[allow(clippy::module_name_repetitions)]
#[allow(clippy::too_many_lines)]
pub async fn send_events(
    #[cfg(test)] client: &crate::client::MockConnect,
    #[cfg(not(test))] client: &Client,
    git_repo_path: Option<&Path>,
    events: Vec<nostr::Event>,
    my_write_relays: Vec<String>,
    repo_read_relays: Vec<RelayUrl>,
    animate: bool,
    silent: bool,
) -> Result<Vec<(String, bool)>> {
    // Only include default relays as fallback when there are no repo relays
    // (bootstrapping case, e.g. new account signup). When repo relays exist,
    // trust the repo and user relay configuration.
    let fallback = [
        if repo_read_relays.is_empty() && my_write_relays.is_empty() {
            client.get_relay_default_set().clone()
        } else {
            vec![]
        },
        if events.iter().any(|e| e.kind.eq(&Kind::GitRepoAnnouncement)) {
            client.get_blaster_relays().clone()
        } else {
            vec![]
        },
    ]
    .concat();
    let mut relays: Vec<&str> = vec![];

    let repo_read_relays = repo_read_relays
        .iter()
        .map(|r| r.to_string())
        .collect::<Vec<String>>();

    let all = &[
        repo_read_relays.clone(),
        my_write_relays.clone(),
        fallback.clone(),
    ]
    .concat();
    // add duplicates first
    for r in &repo_read_relays {
        let r_clean = remove_trailing_slash(r);
        if !my_write_relays
            .iter()
            .filter(|x| r_clean.eq(&remove_trailing_slash(x)))
            .count()
            > 1
            && !relays.iter().any(|x| r_clean.eq(&remove_trailing_slash(x)))
        {
            relays.push(r);
        }
    }

    for r in all {
        let r_clean = remove_trailing_slash(r);
        if !relays.iter().any(|x| r_clean.eq(&remove_trailing_slash(x))) {
            relays.push(r);
        }
    }

    let verbose = is_verbose();
    let is_test = std::env::var("NGITTEST").is_ok();
    let use_concise = !is_test && !verbose && !silent && animate;

    let events_description = describe_events(&events);

    // Set up the two-MultiProgress pattern (same as fetch_all):
    // 1. A spinner MultiProgress shown immediately (concise mode only)
    // 2. A detail MultiProgress that starts hidden and becomes visible after a
    //    delay
    let spinner_multi = if use_concise {
        let sm = MultiProgress::new();
        let spinner = sm.add(
            ProgressBar::new_spinner()
                .with_style(
                    ProgressStyle::with_template("{spinner} {msg}")
                        .unwrap()
                        .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈"),
                )
                .with_message(format!(
                    "Publishing {events_description} to nostr relays..."
                )),
        );
        spinner.enable_steady_tick(Duration::from_millis(100));
        Some((sm, spinner))
    } else {
        None
    };

    let m = if silent || use_concise {
        MultiProgress::with_draw_target(ProgressDrawTarget::hidden())
    } else {
        MultiProgress::new()
    };

    // Pre-add a heading bar at position 0 so it has a reserved slot
    // before any relay bars are added.
    let heading_bar = {
        let bar =
            m.add(ProgressBar::new(0).with_style(ProgressStyle::with_template("{msg}").unwrap()));
        if !is_test {
            bar.set_message(format!(
                "Publishing {events_description} to nostr relays..."
            ));
        }
        Some(bar)
    };

    let reveal_state: Option<Arc<BarRevealState>> = if use_concise {
        Some(Arc::new(BarRevealState {
            revealed: AtomicBool::new(false),
            deferred: Mutex::new(Vec::new()),
        }))
    } else {
        None
    };

    // Spawn a background timer that transitions from spinner to detail view
    let detail_multi_for_timer = m.clone();
    let spinner_for_timer = spinner_multi.as_ref().map(|(_, s)| s.clone());
    let reveal_state_for_timer = reveal_state.clone();
    let heading_bar_for_timer = heading_bar.clone();
    let events_description_for_timer = events_description.clone();
    let timer_handle = if use_concise {
        let handle = tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(SPINNER_EXPAND_DELAY_MS)).await;
            if let Some(spinner) = spinner_for_timer {
                spinner.finish_and_clear();
            }
            detail_multi_for_timer.set_draw_target(ProgressDrawTarget::stderr());
            if let Some(heading) = heading_bar_for_timer {
                heading.finish_with_message(format!(
                    "Publishing {events_description_for_timer} to nostr relays..."
                ));
            }
            if let Some(state) = reveal_state_for_timer {
                let mut deferred = state.deferred.lock().unwrap();
                state.revealed.store(true, Ordering::Release);
                for df in deferred.drain(..) {
                    df.bar.finish_with_message(df.message);
                }
            }
        });
        Some(handle)
    } else {
        None
    };

    let pb_style = ProgressStyle::with_template(if animate {
        " {spinner} {prefix} {bar} {pos}/{len} {msg}"
    } else {
        " - {prefix} {bar} {pos}/{len} {msg}"
    })?
    .progress_chars("##-");

    let pb_after_style =
        |symbol| ProgressStyle::with_template(format!(" {symbol} {}", "{prefix} {msg}",).as_str());
    let pb_after_style_succeeded = pb_after_style(if animate {
        console::style("".to_string())
            .for_stderr()
            .green()
            .to_string()
    } else {
        "y".to_string()
    })?;

    let pb_after_style_failed = pb_after_style(if animate {
        console::style("".to_string())
            .for_stderr()
            .red()
            .to_string()
    } else {
        "x".to_string()
    })?;

    #[allow(clippy::borrow_deref_ref)]
    let relay_results: Vec<(String, bool)> = join_all(relays.iter().map(|&relay| {
        let reveal_state_clone = reveal_state.clone();
        let my_write_relays = my_write_relays.clone();
        let repo_read_relays = repo_read_relays.clone();
        let fallback = fallback.clone();
        let m = m.clone();
        let events = events.clone();
        let pb_style = pb_style.clone();
        let pb_after_style_failed = pb_after_style_failed.clone();
        let pb_after_style_succeeded = pb_after_style_succeeded.clone();
        async move {
            let relay_clean = remove_trailing_slash(relay);
            let details = format!(
                "{}{}{} {}",
                if my_write_relays
                    .iter()
                    .any(|r| relay_clean.eq(&remove_trailing_slash(r)))
                {
                    " [my-relay]"
                } else {
                    ""
                },
                if repo_read_relays
                    .iter()
                    .any(|r| relay_clean.eq(&remove_trailing_slash(&r.to_string())))
                {
                    " [repo-relay]"
                } else {
                    ""
                },
                if fallback
                    .iter()
                    .any(|r| relay_clean.eq(&remove_trailing_slash(r)))
                {
                    " [default]"
                } else {
                    ""
                },
                relay_clean,
            );
            let pb = m.add(
                ProgressBar::new(events.len() as u64)
                    .with_prefix(details.to_string())
                    .with_style(pb_style.clone()),
            );
            if animate {
                pb.enable_steady_tick(Duration::from_millis(300));
            }
            pb.inc(0); // need to make pb display intially
            let mut failed = false;
            for event in &events {
                match client
                    .send_event_to(git_repo_path, relay, event.clone())
                    .await
                {
                    Ok(_) => pb.inc(1),
                    Err(e) => {
                        pb.set_style(pb_after_style_failed.clone());
                        let msg = console::style(format!(
                            "error: {}",
                            e.to_string()
                                .replace("relay pool error:", "")
                                .replace("event not published: ", "")
                        ))
                        .for_stderr()
                        .red()
                        .to_string();
                        finish_bar(&pb, msg, &reveal_state_clone);
                        failed = true;
                        break;
                    }
                };
            }
            if !failed {
                pb.set_style(pb_after_style_succeeded.clone());
                finish_bar(&pb, String::new(), &reveal_state_clone);
            }
            (relay_clean.to_string(), !failed)
        }
    }))
    .await;

    // Cancel the background timer if it hasn't fired yet, and clean up
    // the spinner. If the timer already fired, the abort is a no-op.
    if let Some(handle) = timer_handle {
        handle.abort();
    }

    let succeeded_count = relay_results.iter().filter(|(_, ok)| *ok).count();
    let total_count = relay_results.len();
    let failed_relays: Vec<&str> = relay_results
        .iter()
        .filter(|(_, ok)| !*ok)
        .map(|(url, _)| {
            url.strip_prefix("wss://")
                .or_else(|| url.strip_prefix("ws://"))
                .unwrap_or(url)
                .trim_end_matches('/')
        })
        .collect();

    let finish_message = if succeeded_count == total_count {
        format!("Published {events_description} to {total_count} relays")
    } else if succeeded_count > 0 {
        format!(
            "Published {events_description} to {succeeded_count}/{total_count} relays (failed: {})",
            failed_relays.join(" ")
        )
    } else {
        format!(
            "failed to publish {events_description} to any relay (failed: {})",
            failed_relays.join(" ")
        )
    };

    if let Some((_, spinner)) = &spinner_multi {
        spinner.set_style(ProgressStyle::with_template("{msg}").unwrap());
        spinner.finish_with_message(finish_message);
    }

    Ok(relay_results)
}

/// Builds a human-readable description of what is being published, e.g.
/// "3 patches", "1 announcement and 1 state event", "2 patches and 1 cover
/// letter".
fn describe_events(events: &[nostr::Event]) -> String {
    use crate::git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, KIND_USER_GRASP_LIST};

    // key = singular, value = (plural, count)
    let mut counts: std::collections::BTreeMap<&str, (&str, usize)> =
        std::collections::BTreeMap::new();

    for event in events {
        let (singular, plural) = if event.kind.eq(&Kind::GitRepoAnnouncement) {
            ("announcement", "announcements")
        } else if event.kind.eq(&STATE_KIND) {
            ("state event", "state events")
        } else if event_is_cover_letter(event) {
            ("cover letter", "cover letters")
        } else if event.kind.eq(&Kind::GitPatch) {
            ("patch", "patches")
        } else if event.kind.eq(&KIND_PULL_REQUEST) {
            ("PR", "PRs")
        } else if event.kind.eq(&KIND_PULL_REQUEST_UPDATE) {
            ("PR update", "PR updates")
        } else if [
            Kind::GitStatusOpen,
            Kind::GitStatusDraft,
            Kind::GitStatusClosed,
            Kind::GitStatusApplied,
        ]
        .contains(&event.kind)
        {
            ("status update", "status updates")
        } else if event.kind.eq(&KIND_USER_GRASP_LIST) {
            ("user relay list", "user relay lists")
        } else {
            ("event", "events")
        };
        counts
            .entry(singular)
            .and_modify(|(_, c)| *c += 1)
            .or_insert((plural, 1));
    }

    let parts: Vec<String> = counts
        .iter()
        .map(|(singular, (plural, n))| {
            if *n == 1 {
                format!("1 {singular}")
            } else {
                format!("{n} {plural}")
            }
        })
        .collect();

    match parts.len() {
        0 => "0 events".to_string(),
        1 => parts[0].clone(),
        _ => {
            let (last, rest) = parts.split_last().unwrap();
            format!("{} and {last}", rest.join(", "))
        }
    }
}

pub async fn delete_event_from_local_cache(
    git_repo_path: &Path,
    event_id: nostr::EventId,
) -> Result<()> {
    let db = get_local_cache_database(git_repo_path).await?;
    db.delete(nostr::Filter::default().id(event_id))
        .await
        .map_err(|e| anyhow!("failed to delete event from local cache: {e}"))?;
    Ok(())
}

fn remove_trailing_slash(s: &str) -> String {
    match s.strip_suffix('/') {
        Some(s) => s,
        None => s,
    }
    .to_string()
}