research-master 0.1.40

MCP server for searching and downloading academic papers from multiple research sources
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
use anyhow::Result;
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh};
use research_master::config::{find_config_file, get_config, load_config};
use research_master::mcp::server::McpServer;
use research_master::models::{
    CitationRequest, DownloadRequest, ReadRequest, SearchQuery, SortBy, SortOrder,
};
use research_master::sources::{SourceCapabilities, SourceRegistry};
use research_master::utils::{
    deduplicate_papers, find_duplicates, CacheService, DuplicateStrategy,
};
use std::io::IsTerminal;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

/// Research Master - Search and download academic papers from multiple research sources
#[derive(Parser, Debug)]
#[command(name = "research-master")]
#[command(version = env!("CARGO_PKG_VERSION"))]
#[command(author = "hongkongkiwi")]
#[command(about = "Search and download academic papers from multiple research sources", long_about = None)]
#[command(after_help = "EXAMPLES:
    # Search for papers across all sources
    research-master search \"transformer attention mechanism\"

    # Search for papers on arXiv only
    research-master search \"quantum computing\" --source arxiv

    # Search with year filter and limit results
    research-master search \"climate change\" --year 2020-2023 --max-results 5

    # Search by author
    research-master author \"Yoshua Bengio\" --max-results 10

    # Download a paper by arXiv ID
    research-master download 2310.12345 --source arxiv --output ./papers/

    # Read/extract text from a PDF
    research-master read 2310.12345 --source arxiv --path ./paper.pdf

    # Look up a paper by DOI
    research-master lookup 10.1038/nature12373

    # Get citations for a paper
    research-master citations 2310.12345 --source arxiv

    # Get related papers
    research-master related 2310.12345 --source arxiv

    # List all available sources
    research-master sources

    # Run MCP server for Claude Desktop
    research-master mcp

    # Manage configuration
    research-master config init     # Initialize config
    research-master config show     # Show current config
    research-master config edit     # Edit config file

    # Export papers to various formats
    research-master export --input papers.json --format bibtex -O output.bib
    research-master export --input papers.json --format csv -O output.csv
    research-master export --input papers.json --format json -O output.json
    research-master export --input papers.json --format ris -O output.ris

    # Bulk download from a file of paper IDs
    research-master bulk-download ./paper_ids.txt -o ./downloads/

    # Manage API keys
    research-master api-keys list              # List configured keys
    research-master api-keys set --source semantic  # Set key

    # Generate shell completions
    research-master completions bash
    research-master completions zsh
    research-master completions fish
")]
#[command(propagate_version = true)]
struct Cli {
    /// Enable verbose logging (can be used multiple times for more verbosity: -v, -vv, -vvv)
    #[arg(long, short, action = clap::ArgAction::Count)]
    verbose: u8,

    /// Suppress non-error output
    #[arg(long, short)]
    quiet: bool,

    /// Output format
    #[arg(long, short, value_enum, global = true, default_value_t = OutputFormat::Auto)]
    output: OutputFormat,

    /// Configuration file path
    #[arg(long, global = true)]
    config: Option<PathBuf>,

    /// Request timeout in seconds
    #[arg(long, global = true, default_value_t = 30)]
    timeout: u64,

    /// Show all environment variables
    #[arg(long, global = true)]
    env: bool,

    /// Disable caching for this command (useful for testing fresh results)
    #[arg(long, global = true, default_value_t = false)]
    no_cache: bool,

    /// Log to a file instead of stderr
    #[arg(long, global = true, value_name = "FILE")]
    log_file: Option<PathBuf>,

    #[command(subcommand)]
    command: Option<Commands>,
}

/// Output format for results
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum OutputFormat {
    /// Automatic based on terminal (table if TTY, JSON otherwise)
    Auto,
    /// Table format (human-readable)
    Table,
    /// JSON format (machine-readable)
    Json,
    /// Plain text format
    Plain,
}

/// Available research sources
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum Source {
    #[value(name = "arxiv")]
    Arxiv,
    #[value(name = "pubmed")]
    Pubmed,
    #[value(name = "biorxiv")]
    Biorxiv,
    #[value(name = "semantic")]
    Semantic,
    #[value(name = "openalex")]
    OpenAlex,
    #[value(name = "crossref")]
    CrossRef,
    #[value(name = "iacr")]
    Iacr,
    #[value(name = "pmc")]
    Pmc,
    #[value(name = "hal")]
    Hal,
    #[value(name = "dblp")]
    Dblp,
    #[value(name = "ssrn")]
    Ssrn,
    #[value(name = "dimensions")]
    Dimensions,
    #[value(name = "ieee_xplore")]
    IeeeXplore,
    #[value(name = "europe_pmc")]
    EuropePmc,
    #[value(name = "core")]
    Core,
    #[value(name = "zenodo")]
    Zenodo,
    #[value(name = "unpaywall")]
    Unpaywall,
    #[value(name = "mdpi")]
    Mdpi,
    #[value(name = "jstor")]
    Jstor,
    #[value(name = "scispace")]
    Scispace,
    #[value(name = "acm")]
    Acm,
    #[value(name = "connected_papers")]
    ConnectedPapers,
    #[value(name = "doaj")]
    Doaj,
    #[value(name = "worldwidescience")]
    WorldWideScience,
    #[value(name = "osf")]
    Osf,
    #[value(name = "base")]
    Base,
    #[value(name = "springer")]
    Springer,
    #[value(name = "google_scholar")]
    GoogleScholar,
    #[value(name = "all")]
    All,
}

/// Sort field for results
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum SortField {
    /// Sort by relevance
    Relevance,
    /// Sort by publication date
    Date,
    /// Sort by citation count
    Citations,
    /// Sort by title
    Title,
    /// Sort by author
    Author,
}

/// Sort order
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum Order {
    /// Ascending order
    Asc,
    /// Descending order
    Desc,
}

/// Strategy for handling duplicates
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum DedupStrategy {
    /// Keep the first occurrence of each duplicate group
    First,
    /// Keep the last occurrence of each duplicate group
    Last,
    /// Keep all papers but mark duplicates
    Mark,
}

/// Shell for completion generation
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
#[allow(clippy::enum_variant_names)]
enum Shell {
    /// Bash shell
    Bash,
    /// Elvish shell
    Elvish,
    /// Fish shell
    Fish,
    /// PowerShell
    PowerShell,
    /// Zsh shell
    Zsh,
}

/// Export format for papers
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum ExportFormat {
    /// BibTeX format for citation managers
    Bibtex,
    /// CSV spreadsheet format
    Csv,
    /// JSON format
    Json,
    /// RIS format (EndNote, Zotero)
    Ris,
}

/// Config action
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum ConfigAction {
    /// Initialize a new config file
    Init,
    /// Show current configuration
    Show,
    /// Edit configuration file
    Edit,
}

/// API key action
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum ApiKeyAction {
    /// Set an API key
    Set,
    /// List configured API keys
    List,
    /// Remove an API key
    Remove,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Search for papers by query string
    #[command(alias = "s")]
    Search {
        /// Search query string
        query: String,

        /// Source to search (default: all)
        #[arg(long, short, value_enum, default_value_t = Source::All)]
        source: Source,

        /// Maximum number of results
        #[arg(long, short, default_value_t = 10)]
        max_results: usize,

        /// Year filter (e.g., "2020", "2018-2022", "2010-", "-2015")
        #[arg(long)]
        year: Option<String>,

        /// Sort by field
        #[arg(long, value_enum)]
        sort_by: Option<SortField>,

        /// Sort order
        #[arg(long, value_enum)]
        order: Option<Order>,

        /// Category/subject filter
        #[arg(long, short)]
        category: Option<String>,

        /// Author filter
        #[arg(long, short)]
        author: Option<String>,

        /// Deduplicate results across sources
        #[arg(long)]
        dedup: bool,

        /// Deduplication strategy (default: first)
        #[arg(long, value_enum, requires = "dedup")]
        dedup_strategy: Option<DedupStrategy>,

        /// Fetch detailed information (slower but more complete)
        #[arg(long, default_value_t = true)]
        fetch_details: bool,
    },

    /// Search for papers by author
    #[command(alias = "a")]
    Author {
        /// Author name
        author: String,

        /// Source to search (default: all sources that support author search)
        #[arg(long, short, value_enum, default_value_t = Source::All)]
        source: Source,

        /// Maximum number of results per source
        #[arg(long, short, default_value_t = 10)]
        max_results: usize,

        /// Year filter (e.g., "2020", "2018-2022", "2010-", "-2015")
        #[arg(long)]
        year: Option<String>,

        /// Deduplicate results across sources
        #[arg(long)]
        dedup: bool,

        /// Deduplication strategy (default: first)
        #[arg(long, value_enum, requires = "dedup")]
        dedup_strategy: Option<DedupStrategy>,
    },

    /// Download a paper's PDF
    #[command(alias = "d")]
    Download {
        /// Paper ID (source-specific identifier)
        paper_id: String,

        /// Source of the paper
        #[arg(long, short, value_enum)]
        source: Source,

        /// Path where to save the PDF
        #[arg(long)]
        output_path: Option<PathBuf>,

        /// Auto-generate filename from paper title
        #[arg(long)]
        auto_filename: bool,

        /// Create directory if it doesn't exist
        #[arg(long)]
        create_dir: bool,

        /// Paper DOI (optional, for verification)
        #[arg(long)]
        doi: Option<String>,
    },

    /// Read and extract text from a paper's PDF
    #[command(alias = "r")]
    Read {
        /// Paper ID (source-specific identifier)
        paper_id: String,

        /// Source of the paper
        #[arg(long, short, value_enum)]
        source: Source,

        /// Path where PDF is saved (or will be downloaded)
        #[arg(long, short = 'p')]
        path: PathBuf,

        /// Download PDF if not found locally
        #[arg(long, default_value_t = true)]
        download_if_missing: bool,

        /// Number of pages to extract (0 = all)
        #[arg(long)]
        pages: Option<usize>,

        /// Extract text to file instead of stdout
        #[arg(long, short = 'O')]
        output_file: Option<PathBuf>,
    },

    /// Get papers that cite a given paper
    #[command(alias = "c")]
    Citations {
        /// Paper ID (source-specific identifier)
        paper_id: String,

        /// Source of the paper
        #[arg(long, short, value_enum)]
        source: Source,

        /// Maximum number of results
        #[arg(long, short, default_value_t = 20)]
        max_results: usize,
    },

    /// Get papers referenced by a given paper
    #[command(alias = "ref")]
    References {
        /// Paper ID (source-specific identifier)
        paper_id: String,

        /// Source of the paper
        #[arg(long, short, value_enum)]
        source: Source,

        /// Maximum number of results
        #[arg(long, short, default_value_t = 20)]
        max_results: usize,
    },

    /// Get related/similar papers
    #[command(alias = "rel")]
    Related {
        /// Paper ID (source-specific identifier)
        paper_id: String,

        /// Source of the paper
        #[arg(long, short, value_enum)]
        source: Source,

        /// Maximum number of results
        #[arg(long, short, default_value_t = 20)]
        max_results: usize,
    },

    /// Look up a paper by DOI
    #[command(alias = "doi")]
    LookupByDoi {
        /// Digital Object Identifier
        doi: String,

        /// Source to use for lookup (default: all that support DOI lookup)
        #[arg(long, short, value_enum, default_value_t = Source::All)]
        source: Source,

        /// Return JSON output even in terminal
        #[arg(long, short)]
        json: bool,
    },

    /// List available sources and their capabilities
    #[command(alias = "ls")]
    Sources {
        /// Show detailed information about each source
        #[arg(long, short)]
        detailed: bool,

        /// Filter sources by capability
        #[arg(long, value_enum)]
        with_capability: Option<CapabilityFilter>,
    },

    /// Run the MCP server (for Claude Desktop and other MCP clients)
    #[command(alias = "serve")]
    Mcp {
        /// Run in stdio mode (for MCP clients like Claude Desktop)
        #[arg(long, default_value_t = true)]
        stdio: bool,

        /// Run in HTTP/SSE mode (overrides --stdio)
        #[arg(long)]
        http: bool,

        /// Port for SSE mode (if not using stdio)
        #[arg(long, short, default_value_t = 3000)]
        port: u16,

        /// Host to bind to for SSE mode
        #[arg(long, default_value = "127.0.0.1")]
        host: String,
    },

    /// Deduplicate a JSON file containing papers
    #[command(alias = "dedup")]
    Dedupe {
        /// Input JSON file containing papers
        input: PathBuf,

        /// Output file (default: overwrite input)
        #[arg(long, short = 'O')]
        output_file: Option<PathBuf>,

        /// Deduplication strategy
        #[arg(long, value_enum, default_value_t = DedupStrategy::First)]
        strategy: DedupStrategy,

        /// Show duplicate groups without removing
        #[arg(long, short = 'v')]
        show: bool,
    },

    /// Manage local cache
    Cache {
        /// Subcommand
        #[command(subcommand)]
        command: CacheCommands,
    },

    /// Check configuration and source health
    #[command(alias = "diag")]
    Doctor {
        /// Check connectivity to all sources
        #[arg(long)]
        check_connectivity: bool,

        /// Check API keys are configured correctly
        #[arg(long)]
        check_api_keys: bool,

        /// Verbose output
        #[arg(long, short)]
        verbose: bool,
    },

    /// Update to the latest version
    Update {
        /// Force update even if already at latest version
        #[arg(long, short, default_value_t = false)]
        force: bool,

        /// Preview what would be updated without making changes
        #[arg(long, short = 'n', default_value_t = false)]
        dry_run: bool,
    },

    /// Manage configuration
    #[command(alias = "cfg")]
    Config {
        /// Action to perform
        #[arg(value_enum)]
        action: ConfigAction,
    },

    /// Export papers to various formats
    Export {
        /// Input file (JSON with papers) or search results
        #[arg(short, long)]
        input: Option<PathBuf>,

        /// Export format
        #[arg(short, long, value_enum, default_value_t = ExportFormat::Bibtex)]
        format: ExportFormat,

        /// Output file (stdout if not specified)
        #[arg(short, long, short = 'O')]
        output: Option<PathBuf>,

        /// Source to search if no input file provided
        #[arg(long, value_enum)]
        source: Option<Source>,

        /// Search query (requires --source)
        #[arg(long, short = 'q')]
        query: Option<String>,

        /// Maximum number of results to export
        #[arg(long, default_value_t = 100)]
        max_results: usize,
    },

    /// Download multiple papers from a file
    #[command(alias = "bulk-dl")]
    BulkDownload {
        /// File containing paper IDs (one per line, format: source:id or just id)
        input: PathBuf,

        /// Output directory for downloads
        #[arg(long, short = 'o', default_value = "./downloads")]
        output_dir: PathBuf,

        /// Source to use if not specified in file
        #[arg(long, value_enum)]
        source: Option<Source>,

        /// Create source subdirectories
        #[arg(long, default_value_t = true)]
        organize_by_source: bool,

        /// Maximum concurrent downloads
        #[arg(long, default_value_t = 5)]
        concurrency: usize,
    },

    /// Manage API keys
    ApiKeys {
        /// Action to perform
        #[arg(value_enum)]
        action: ApiKeyAction,

        /// Source name (for set/remove)
        #[arg(long, short)]
        source: Option<String>,
    },

    /// Generate shell completion scripts
    #[command(alias = "completion")]
    Completions {
        /// Shell to generate completions for
        #[arg(value_enum)]
        shell: Shell,
    },
}

#[derive(Subcommand, Debug)]
enum CacheCommands {
    /// Show cache status and statistics
    Status,

    /// Clear all cached data
    Clear,

    /// Clear only search cache
    ClearSearches,

    /// Clear only citation cache
    ClearCitations,
}

/// Capability filter for listing sources
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum CapabilityFilter {
    Search,
    Download,
    Read,
    Citations,
    DoiLookup,
    AuthorSearch,
}

/// Print all available environment variables
fn print_env_vars() {
    println!("Research Master MCP - Environment Variables");
    println!();
    println!("API Keys:");
    println!("  SEMANTIC_SCHOLAR_API_KEY    API key for Semantic Scholar (higher rate limits)");
    println!("  CORE_API_KEY                API key for CORE service");
    println!("  OPENALEX_EMAIL              Email for OpenAlex 'polite pool' access");
    println!();
    println!("Source-Specific Rate Limits:");
    println!("  SEMANTIC_SCHOLAR_RATE_LIMIT  Semantic Scholar requests per second (default: 1)");
    println!();
    println!("Global Proxy Settings:");
    println!("  HTTP_PROXY                  HTTP proxy URL (e.g., http://proxy:8080)");
    println!("  HTTPS_PROXY                 HTTPS proxy URL (e.g., https://proxy:8080)");
    println!("  NO_PROXY                    Comma-separated list of hosts to bypass proxy");
    println!();
    println!("Per-Source Proxy Settings:");
    println!("  RESEARCH_MASTER_PROXY_HTTP   Per-source HTTP proxy (format: source_id:proxy_url)");
    println!("  RESEARCH_MASTER_PROXY_HTTPS  Per-source HTTPS proxy (format: source_id:proxy_url)");
    println!();
    println!("Download Settings:");
    println!("  RESEARCH_MASTER_DOWNLOADS_DEFAULT_PATH     Default directory for PDF downloads (default: ./downloads)");
    println!("  RESEARCH_MASTER_DOWNLOADS_ORGANIZE_BY_SOURCE  Create subdirectories per source (default: true)");
    println!("  RESEARCH_MASTER_DOWNLOADS_MAX_FILE_SIZE_MB    Maximum file size for downloads in MB (default: 100)");
    println!();
    println!("Rate Limiting:");
    println!("  RESEARCH_MASTER_RATE_LIMITS_DEFAULT_REQUESTS_PER_SECOND  Default requests per second (default: 5.0)");
    println!("  RESEARCH_MASTER_RATE_LIMITS_MAX_CONCURRENT_REQUESTS     Max concurrent requests (default: 10)");
    println!();
    println!("Cache Settings:");
    println!(
        "  RESEARCH_MASTER_CACHE_ENABLED                Enable local caching (default: disabled)"
    );
    println!("  RESEARCH_MASTER_CACHE_DIRECTORY              Custom cache directory");
    println!("  RESEARCH_MASTER_CACHE_SEARCH_TTL_SECONDS     TTL for search results (default: 1800 = 30 min)");
    println!("  RESEARCH_MASTER_CACHE_CITATION_TTL_SECONDS   TTL for citation results (default: 900 = 15 min)");
    println!();
    println!("Other Settings:");
    println!("  RUST_LOG                    Rust logging level (e.g., debug, info, warn, error)");
    println!();
    println!("Example:");
    println!("  export SEMANTIC_SCHOLAR_API_KEY=\"your-key-here\"");
    println!("  export SEMANTIC_SCHOLAR_RATE_LIMIT=\"5\"");
    println!("  export RESEARCH_MASTER_DOWNLOADS_DEFAULT_PATH=\"./papers\"");
    std::process::exit(0);
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    // Show environment variables and exit if requested
    if cli.env {
        print_env_vars();
    }

    // Initialize tracing based on verbosity
    let log_level = match cli.verbose {
        0 => "info",
        1 => "debug",
        _ => "trace",
    };

    let env_filter = if cli.quiet { "error" } else { log_level };

    let subscriber = tracing_subscriber::registry()
        .with(tracing_subscriber::EnvFilter::new(
            std::env::var("RUST_LOG").unwrap_or_else(|_| format!("research_master={}", env_filter)),
        ))
        .with(tracing_subscriber::fmt::layer());

    // Add file logging if requested
    if let Some(log_path) = &cli.log_file {
        let file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(log_path)
            .map_err(|e| anyhow::anyhow!("Failed to open log file: {}", e))?;

        let file_layer = tracing_subscriber::fmt::layer()
            .with_writer(file)
            .with_ansi(false)
            .json();

        subscriber.with(file_layer).init();
        tracing::info!("Logging to file: {}", log_path.display());
    } else {
        subscriber.with(tracing_subscriber::fmt::layer()).init();
    }

    // Set timeout
    tokio::time::sleep(Duration::from_secs(0)).await; // Just to ensure runtime is initialized

    // Load configuration from file if specified or found in default locations
    let _config = if let Some(config_path) = &cli.config {
        Some(load_config(config_path)?)
    } else if let Some(config_path) = find_config_file() {
        tracing::info!("Using config file: {}", config_path.display());
        Some(load_config(&config_path)?)
    } else {
        None
    };

    // Create source registry
    let registry = SourceRegistry::new();

    // Execute command
    match cli.command {
        Some(Commands::Search {
            query,
            source,
            max_results,
            year,
            sort_by,
            order,
            category,
            author,
            dedup,
            dedup_strategy,
            fetch_details,
        }) => {
            let mut search_query = SearchQuery::new(&query);
            search_query.max_results = max_results;
            search_query.year = year;
            search_query.sort_by = sort_by.map(|s| match s {
                SortField::Relevance => SortBy::Relevance,
                SortField::Date => SortBy::Date,
                SortField::Citations => SortBy::CitationCount,
                SortField::Title => SortBy::Title,
                SortField::Author => SortBy::Author,
            });
            search_query.sort_order = order.map(|o| match o {
                Order::Asc => SortOrder::Ascending,
                Order::Desc => SortOrder::Descending,
            });
            search_query.category = category;
            search_query.author = author;
            search_query.fetch_details = fetch_details;

            let sources = get_sources(&registry, source, SourceCapabilities::SEARCH);
            let all_papers = Arc::new(Mutex::new(Vec::new()));
            let quiet = cli.quiet;

            // Initialize cache if not disabled
            let cache = if cli.no_cache {
                None
            } else {
                let c = CacheService::new();
                let _ = c.initialize();
                Some(c)
            };

            // Create a vector to hold all spawned tasks
            let mut handles = Vec::new();

            for src in sources {
                let src = Arc::clone(src);
                let search_query = search_query.clone();
                let cache = cache.clone();

                // Spawn a task for each source
                let handle = tokio::spawn(async move {
                    let source_id = src.id();
                    let mut papers = Vec::new();

                    // Check cache first
                    if let Some(ref cache_service) = cache {
                        match cache_service.get_search(&search_query, source_id) {
                            research_master::utils::CacheResult::Hit(response) => {
                                if !quiet {
                                    eprintln!(
                                        "[CACHE HIT] Found {} papers from {}",
                                        response.papers.len(),
                                        source_id
                                    );
                                }
                                return response.papers;
                            }
                            research_master::utils::CacheResult::Expired => {
                                if !quiet {
                                    eprintln!(
                                        "[CACHE EXPIRED] Fetching fresh results from {}",
                                        source_id
                                    );
                                }
                            }
                            research_master::utils::CacheResult::Miss => {
                                if !quiet {
                                    eprintln!("[CACHE MISS] Fetching from {}", source_id);
                                }
                            }
                        }
                    }

                    // Fetch from API
                    match src.search(&search_query).await {
                        Ok(response) => {
                            if !quiet {
                                eprintln!(
                                    "Found {} papers from {}",
                                    response.papers.len(),
                                    source_id
                                );
                            }
                            // Cache the result
                            if let Some(ref cache_service) = cache {
                                cache_service.set_search(source_id, &search_query, &response);
                            }
                            papers = response.papers;
                        }
                        Err(e) => {
                            if !quiet {
                                eprintln!("Error searching {}: {}", source_id, e);
                            }
                        }
                    }

                    papers
                });

                handles.push(handle);
            }

            // Wait for all tasks to complete and collect results
            for handle in handles {
                match handle.await {
                    Ok(papers) => {
                        let mut all_papers = all_papers.lock().unwrap();
                        all_papers.extend(papers);
                    }
                    Err(e) => {
                        if !quiet {
                            eprintln!("Task error: {}", e);
                        }
                    }
                }
            }

            // Get the collected papers
            let mut all_papers = {
                let all_papers = all_papers.lock().unwrap();
                all_papers.clone()
            };

            if dedup {
                let strategy = match dedup_strategy.unwrap_or(DedupStrategy::First) {
                    DedupStrategy::First => DuplicateStrategy::First,
                    DedupStrategy::Last => DuplicateStrategy::Last,
                    DedupStrategy::Mark => DuplicateStrategy::Mark,
                };
                all_papers = deduplicate_papers(all_papers, strategy);
            }

            output_papers(&all_papers, cli.output);
        }

        Some(Commands::Author {
            author,
            source,
            max_results,
            year,
            dedup,
            dedup_strategy,
        }) => {
            let sources = get_sources(&registry, source, SourceCapabilities::AUTHOR_SEARCH);
            let all_papers = Arc::new(Mutex::new(Vec::new()));
            let quiet = cli.quiet;

            // Create a vector to hold all spawned tasks
            let mut handles = Vec::new();

            for src in sources {
                let src = Arc::clone(src);
                let author = author.clone();
                let year = year.clone();

                // Spawn a task for each source
                let handle = tokio::spawn(async move {
                    match src
                        .search_by_author(&author, max_results, year.as_deref())
                        .await
                    {
                        Ok(response) => {
                            if !quiet {
                                eprintln!(
                                    "Found {} papers from {}",
                                    response.papers.len(),
                                    src.id()
                                );
                            }
                            response.papers
                        }
                        Err(e) => {
                            if !quiet {
                                eprintln!("Error searching author in {}: {}", src.id(), e);
                            }
                            Vec::new()
                        }
                    }
                });

                handles.push(handle);
            }

            // Wait for all tasks to complete and collect results
            for handle in handles {
                match handle.await {
                    Ok(papers) => {
                        let mut all_papers = all_papers.lock().unwrap();
                        all_papers.extend(papers);
                    }
                    Err(e) => {
                        if !quiet {
                            eprintln!("Task error: {}", e);
                        }
                    }
                }
            }

            // Get the collected papers
            let mut all_papers = {
                let all_papers = all_papers.lock().unwrap();
                all_papers.clone()
            };

            if dedup {
                let strategy = match dedup_strategy.unwrap_or(DedupStrategy::First) {
                    DedupStrategy::First => DuplicateStrategy::First,
                    DedupStrategy::Last => DuplicateStrategy::Last,
                    DedupStrategy::Mark => DuplicateStrategy::Mark,
                };
                all_papers = deduplicate_papers(all_papers, strategy);
            }

            output_papers(&all_papers, cli.output);
        }

        Some(Commands::Download {
            paper_id,
            source,
            output_path,
            auto_filename: _,
            create_dir,
            doi,
        }) => {
            let src = get_source(&registry, source)?;
            let save_path = output_path.unwrap_or_else(|| PathBuf::from("."));

            if create_dir {
                if let Some(parent) = save_path.parent() {
                    std::fs::create_dir_all(parent)?;
                }
            }

            let mut request = DownloadRequest::new(&paper_id, save_path.to_string_lossy());
            if let Some(doi_val) = doi {
                request = request.doi(&doi_val);
            }

            let result = src.download(&request).await?;

            if result.success {
                if !cli.quiet {
                    eprintln!("Downloaded {} bytes to {}", result.bytes, result.path);
                }
            } else {
                anyhow::bail!("Download failed: {:?}", result.error);
            }
        }

        Some(Commands::Read {
            paper_id,
            source,
            path,
            download_if_missing,
            pages: _,
            output_file,
        }) => {
            let src = get_source(&registry, source)?;
            let request = ReadRequest::new(&paper_id, path.to_string_lossy())
                .download_if_missing(download_if_missing);

            let result = src.read(&request).await?;

            if result.success {
                let text = result.text;
                if let Some(output_path) = output_file {
                    std::fs::write(&output_path, text)?;
                    if !cli.quiet {
                        eprintln!("Text written to {}", output_path.display());
                    }
                } else {
                    println!("{}", text);
                }
            } else {
                anyhow::bail!("Read failed: {:?}", result.error);
            }
        }

        Some(Commands::Citations {
            paper_id,
            source,
            max_results,
        }) => {
            let src = get_source(&registry, source)?;
            let request = CitationRequest::new(&paper_id).max_results(max_results);

            let response = src.get_citations(&request).await?;
            output_papers(&response.papers, cli.output);
        }

        Some(Commands::References {
            paper_id,
            source,
            max_results,
        }) => {
            let src = get_source(&registry, source)?;
            let request = CitationRequest::new(&paper_id).max_results(max_results);

            let response = src.get_references(&request).await?;
            output_papers(&response.papers, cli.output);
        }

        Some(Commands::Related {
            paper_id,
            source,
            max_results,
        }) => {
            let src = get_source(&registry, source)?;
            let request = CitationRequest::new(&paper_id).max_results(max_results);

            let response = src.get_related(&request).await?;
            output_papers(&response.papers, cli.output);
        }

        Some(Commands::LookupByDoi { doi, source, json }) => {
            let sources = get_sources(&registry, source, SourceCapabilities::DOI_LOOKUP);
            let output_fmt = if json { OutputFormat::Json } else { cli.output };

            for src in sources {
                match src.get_by_doi(&doi).await {
                    Ok(paper) => {
                        output_papers(&[paper], output_fmt);
                        return Ok(());
                    }
                    Err(e) => {
                        if !cli.quiet {
                            eprintln!("Not found in {}: {}", src.id(), e);
                        }
                    }
                }
            }
            anyhow::bail!("Paper not found in any source");
        }

        Some(Commands::Sources {
            detailed,
            with_capability,
        }) => {
            let sources: Vec<_> = match with_capability {
                Some(CapabilityFilter::Search) => {
                    registry.with_capability(SourceCapabilities::SEARCH)
                }
                Some(CapabilityFilter::Download) => {
                    registry.with_capability(SourceCapabilities::DOWNLOAD)
                }
                Some(CapabilityFilter::Read) => registry.with_capability(SourceCapabilities::READ),
                Some(CapabilityFilter::Citations) => {
                    registry.with_capability(SourceCapabilities::CITATIONS)
                }
                Some(CapabilityFilter::DoiLookup) => {
                    registry.with_capability(SourceCapabilities::DOI_LOOKUP)
                }
                Some(CapabilityFilter::AuthorSearch) => {
                    registry.with_capability(SourceCapabilities::AUTHOR_SEARCH)
                }
                None => registry.all().collect(),
            };

            for src in sources {
                if detailed {
                    println!("{} ({})", src.name(), src.id());
                    println!("  Capabilities: {:?}", src.capabilities());
                } else {
                    println!("{} - {}", src.id(), src.name());
                }
            }
        }

        Some(Commands::Mcp {
            stdio,
            http,
            port,
            host,
        }) => {
            let server = McpServer::new(Arc::new(registry))?;

            // Use HTTP mode if --http flag is provided, otherwise use --stdio flag
            let use_http = http || !stdio;

            if use_http {
                let addr = format!("{}:{}", host, port);
                tracing::info!("Running MCP server in HTTP/SSE mode on {}", addr);
                let (bound_addr, handle) = server.run_http(&addr).await?;
                tracing::info!("MCP server listening on {}", bound_addr);

                // Wait for the server to finish
                handle
                    .await
                    .map_err(|e| anyhow::anyhow!("Server task failed: {}", e))?;
            } else {
                tracing::info!("Running MCP server in stdio mode");
                server.run().await?;
            }
        }

        Some(Commands::Dedupe {
            input,
            output_file,
            strategy,
            show,
        }) => {
            let json_str = std::fs::read_to_string(&input)?;
            let papers: Vec<research_master::models::Paper> = serde_json::from_str(&json_str)?;

            let dup_strategy = match strategy {
                DedupStrategy::First => DuplicateStrategy::First,
                DedupStrategy::Last => DuplicateStrategy::Last,
                DedupStrategy::Mark => DuplicateStrategy::Mark,
            };

            if show {
                let groups = find_duplicates(&papers);
                if groups.is_empty() {
                    println!("No duplicates found");
                } else {
                    println!("Found {} duplicate groups:", groups.len());
                    for (i, group) in groups.iter().enumerate() {
                        println!("  Group {}: {} papers", i + 1, group.len());
                        for idx in group {
                            println!("    - {} ({})", papers[*idx].title, papers[*idx].source);
                        }
                    }
                }
            } else {
                let deduped = deduplicate_papers(papers, dup_strategy);
                let output_json = serde_json::to_string_pretty(&deduped)?;
                let output_path = output_file.as_ref().unwrap_or(&input);
                std::fs::write(output_path, output_json)?;
                if !cli.quiet {
                    eprintln!(
                        "Deduplicated: {} -> {} papers",
                        input.display(),
                        deduped.len()
                    );
                }
            }
        }

        Some(Commands::Cache { command }) => {
            let cache = CacheService::new();
            cache.initialize()?;

            match command {
                CacheCommands::Status => {
                    let stats = cache.stats();
                    if !stats.enabled {
                        println!("Cache: disabled");
                        println!("To enable, set RESEARCH_MASTER_CACHE_ENABLED=true");
                    } else {
                        println!("Cache: enabled");
                        println!("Directory: {}", stats.cache_dir.display());
                        println!(
                            "Search cache: {} items ({} KB)",
                            stats.search_count, stats.search_size_kb
                        );
                        println!(
                            "Citation cache: {} items ({} KB)",
                            stats.citation_count, stats.citation_size_kb
                        );
                        println!("Total size: {} KB", stats.total_size_kb);
                        println!("Search TTL: {} seconds", stats.ttl_search.as_secs());
                        println!("Citation TTL: {} seconds", stats.ttl_citations.as_secs());
                    }
                }
                CacheCommands::Clear => {
                    if !cli.quiet {
                        eprintln!("Clearing all cached data...");
                    }
                    cache.clear_all()?;
                    if !cli.quiet {
                        eprintln!("Cache cleared successfully.");
                    }
                }
                CacheCommands::ClearSearches => {
                    if !cli.quiet {
                        eprintln!("Clearing search cache...");
                    }
                    cache.clear_searches()?;
                    if !cli.quiet {
                        eprintln!("Search cache cleared successfully.");
                    }
                }
                CacheCommands::ClearCitations => {
                    if !cli.quiet {
                        eprintln!("Clearing citation cache...");
                    }
                    cache.clear_citations()?;
                    if !cli.quiet {
                        eprintln!("Citation cache cleared successfully.");
                    }
                }
            }
        }

        Some(Commands::Doctor {
            check_connectivity,
            check_api_keys,
            verbose,
        }) => {
            println!("Research Master MCP - Doctor");
            println!("================================");

            // Check configuration
            println!("\n[Configuration]");
            let config = get_config();
            println!("  API Keys:");
            if config.api_keys.semantic_scholar.is_some() {
                println!("    - Semantic Scholar: Configured");
            } else {
                println!("    - Semantic Scholar: Not configured (optional)");
            }
            if config.api_keys.core.is_some() {
                println!("    - CORE: Configured");
            } else {
                println!("    - CORE: Not configured (optional)");
            }

            // Check sources
            println!("\n[Sources]");
            println!("  Total sources loaded: {}", registry.len());
            let mut sources_info: Vec<_> = registry
                .all()
                .map(|s| (s.id(), s.name(), format!("{:?}", s.capabilities())))
                .collect();
            sources_info.sort_by_key(|(id, _, _)| *id);

            for (id, name, caps) in &sources_info {
                println!("  - {} ({})", name, id);
                if verbose {
                    println!("    Capabilities: {}", caps);
                }
            }

            // Check connectivity if requested
            if check_connectivity {
                println!("\n[Connectivity]");
                for (id, name, _) in &sources_info {
                    let test_url = format!("https://{}.org", id.replace('_', ""));
                    match reqwest::Client::new().head(&test_url).send().await {
                        Ok(resp) => {
                            let status = if resp.status().is_success() {
                                "OK"
                            } else {
                                "ERROR"
                            };
                            println!("  - {}: {} ({})", name, status, resp.status());
                        }
                        Err(e) => {
                            println!(
                                "  - {}: ERROR ({})",
                                name,
                                e.to_string().split(':').next().unwrap_or("unknown")
                            );
                        }
                    }
                }
            }

            // Check API keys if requested
            if check_api_keys {
                println!("\n[API Key Validation]");
                // Semantic Scholar
                if let Some(key) = &config.api_keys.semantic_scholar {
                    if key.len() >= 10 {
                        println!("  - Semantic Scholar API key: Valid format");
                    } else {
                        println!("  - Semantic Scholar API key: May be invalid (too short)");
                    }
                }
            }

            // Check proxy settings
            println!("\n[Proxy Settings]");
            let http_proxy = std::env::var("HTTP_PROXY").ok();
            let https_proxy = std::env::var("HTTPS_PROXY").ok();
            if http_proxy.is_some() || https_proxy.is_some() {
                if let Some(http) = &http_proxy {
                    println!("  - HTTP_PROXY: {}", http);
                }
                if let Some(https) = &https_proxy {
                    println!("  - HTTPS_PROXY: {}", https);
                }
            } else {
                println!("  - No proxy configured (direct connection)");
            }

            println!("\n================================");
            println!("Doctor check complete.");
        }

        Some(Commands::Update { force, dry_run }) => {
            use anyhow::Context as _;
            use research_master::utils::{
                detect_installation, download_and_extract_asset, fetch_and_verify_sha256,
                fetch_latest_release, fetch_sha256_signature, find_asset_for_platform,
                get_current_target, get_update_instructions, replace_binary, verify_gpg_signature,
                verify_sha256, InstallationMethod,
            };
            #[cfg(unix)]
            use std::os::unix::fs::PermissionsExt;

            let current_version = env!("CARGO_PKG_VERSION");
            println!("Research Master MCP Updater");
            println!("============================");
            println!("Current version: v{}", current_version);

            // Detect installation method
            let install_method = detect_installation();
            let instructions = get_update_instructions(&install_method);

            // Fetch latest release
            eprintln!("Checking for updates...");
            let latest = match fetch_latest_release().await {
                Ok(release) => release,
                Err(e) => {
                    eprintln!("Failed to check for updates: {}", e);
                    eprintln!("\n{}", instructions);
                    return Ok(());
                }
            };

            println!("Latest version: {}", latest.version);

            // Check if update is needed
            let needs_update = if force {
                true
            } else {
                let current = semver::Version::parse(current_version)
                    .unwrap_or_else(|_| semver::Version::new(0, 0, 0));
                let latest_v = semver::Version::parse(&latest.version)
                    .unwrap_or_else(|_| semver::Version::new(0, 0, 0));
                latest_v > current
            };

            if !needs_update && !force {
                println!("You are already on the latest version!");
                return Ok(());
            }

            // If dry run, just show what would happen
            if dry_run {
                println!("\n[Dry run] Would update to v{}", latest.version);
                println!("Installation method detected: {:?}", install_method);
                return Ok(());
            }

            // Show release notes if available
            if !latest.body.is_empty() {
                println!("\nRelease notes:");
                println!("--------------");
                // Show first 500 characters of release notes
                let notes = if latest.body.len() > 500 {
                    &latest.body[..500]
                } else {
                    &latest.body
                };
                println!("{}", notes);
                if latest.body.len() > 500 {
                    println!("...\n(Full notes available at https://github.com/hongkongkiwi/research-master/releases/tag/v{})", latest.version);
                }
            }

            // Handle based on installation method
            match &install_method {
                InstallationMethod::Homebrew { .. } | InstallationMethod::Cargo { .. } => {
                    println!("\n{}", instructions);
                    println!("\nAfter updating, run 'research-master --version' to verify.");
                }
                InstallationMethod::Direct { .. } | InstallationMethod::Unknown => {
                    // Attempt self-update
                    let target = get_current_target();
                    if target.is_empty() {
                        eprintln!("Unsupported platform for automatic update.");
                        eprintln!("\n{}", instructions);
                        return Ok(());
                    }

                    println!("\nTarget platform: {}", target);

                    // Find appropriate asset
                    let asset = match find_asset_for_platform(&latest) {
                        Some(a) => a,
                        None => {
                            eprintln!("No release asset found for platform: {}", target);
                            eprintln!("Please download manually from: https://github.com/hongkongkiwi/research-master/releases/tag/v{}", latest.version);
                            return Ok(());
                        }
                    };

                    println!("\nAsset: {}", asset.name);

                    // Create temp directory
                    let temp_dir = std::env::temp_dir().join("research-master-update");
                    let _ = std::fs::create_dir_all(&temp_dir);

                    // Download and extract
                    #[allow(clippy::needless_borrow)]
                    match download_and_extract_asset(&asset, &temp_dir).await {
                        Ok(archive_path) => {
                            // Fetch expected SHA256 checksum
                            let expected_checksum = match fetch_and_verify_sha256(
                                &asset.name,
                                &temp_dir,
                            )
                            .await
                            {
                                Ok(hash) => hash,
                                Err(e) => {
                                    eprintln!("Warning: Could not fetch SHA256 checksums: {}. Proceeding without verification.", e);
                                    "".to_string()
                                }
                            };

                            // Verify checksum if available
                            if !expected_checksum.is_empty() {
                                eprintln!("Verifying SHA256 checksum...");
                                match verify_sha256(&archive_path, &expected_checksum) {
                                    Ok(true) => {
                                        eprintln!("SHA256 verification passed!");
                                    }
                                    Ok(false) => {
                                        eprintln!("ERROR: SHA256 verification failed! The download may be corrupted or tampered with.");
                                        eprintln!("Aborting update for safety.");
                                        let _ = std::fs::remove_file(&archive_path);
                                        let _ = std::fs::remove_dir_all(&temp_dir);
                                        return Ok(());
                                    }
                                    Err(e) => {
                                        eprintln!("Warning: Could not verify checksum: {}. Proceeding without verification.", e);
                                    }
                                }

                                // Fetch and verify GPG signature if available
                                eprintln!("Checking for GPG signature...");
                                match fetch_sha256_signature().await {
                                    Ok(signature) => {
                                        // Write SHA256SUMS.txt to temp location for verification
                                        let sha256sums_path = temp_dir.join("SHA256SUMS.txt");
                                        let checksums_content =
                                            format!("{}  {}", expected_checksum, asset.name);
                                        std::fs::write(&sha256sums_path, &checksums_content).ok();

                                        if sha256sums_path.exists() {
                                            match verify_gpg_signature(&sha256sums_path, &signature)
                                            {
                                                Ok(true) => {
                                                    eprintln!("GPG signature verification passed!");
                                                }
                                                Ok(false) => {
                                                    // GPG verification failed but we continue if SHA256 passed
                                                    eprintln!("WARNING: GPG signature verification failed or not configured.");
                                                    eprintln!("Only SHA256 checksum verification was performed.");
                                                }
                                                Err(e) => {
                                                    eprintln!("Warning: Could not verify GPG signature: {}. Continuing with SHA256 verification only.", e);
                                                }
                                            }
                                            let _ = std::fs::remove_file(&sha256sums_path);
                                        }
                                    }
                                    Err(e) => {
                                        eprintln!("Note: GPG signature not available ({}). Using SHA256 verification only.", e);
                                    }
                                }
                            }

                            // Extract the archive
                            let binary_path = if asset.name.ends_with(".tar.gz") {
                                use std::process::Command;
                                let output = Command::new("tar")
                                    .args([
                                        "xzf",
                                        archive_path.to_str().unwrap(),
                                        "-C",
                                        temp_dir.to_str().unwrap(),
                                    ])
                                    .output()
                                    .context("Failed to extract archive")?;

                                if !output.status.success() {
                                    anyhow::bail!(
                                        "Extraction failed: {}",
                                        String::from_utf8_lossy(&output.stderr)
                                    );
                                }

                                // Find the binary
                                let mut binary_path = None;
                                for entry in std::fs::read_dir(&temp_dir)? {
                                    let entry = entry?;
                                    let path = entry.path();
                                    if path.is_file()
                                        && path
                                            .file_name()
                                            .map(|n| {
                                                n.to_string_lossy().starts_with("research-master")
                                            })
                                            .unwrap_or(false)
                                    {
                                        // Make executable
                                        #[cfg(unix)]
                                        {
                                            let mut perms = std::fs::metadata(&path)?.permissions();
                                            perms.set_mode(0o755);
                                            std::fs::set_permissions(&path, perms)?;
                                        }
                                        #[cfg(not(unix))]
                                        {
                                            // On Windows, just ensure the file is writable
                                            let mut perms = std::fs::metadata(&path)?.permissions();
                                            perms.set_readonly(false);
                                            std::fs::set_permissions(&path, perms)?;
                                        }
                                        binary_path = Some(path);
                                        break;
                                    }
                                }
                                binary_path.context("Could not find binary in archive")?
                            } else {
                                anyhow::bail!("Unsupported archive format");
                            };

                            println!("\nDownloaded and extracted to: {}", binary_path.display());

                            // Get current binary path
                            let current_exe = std::env::current_exe().map_err(|e| {
                                anyhow::anyhow!("Failed to get current executable path: {}", e)
                            })?;

                            // Replace binary
                            match replace_binary(&current_exe, &binary_path) {
                                Ok(_) => {
                                    println!("\nUpdate successful!");
                                    println!("New binary will be used on next run.");
                                }
                                Err(e) => {
                                    eprintln!("\nFailed to replace binary: {}", e);
                                    eprintln!(
                                        "You may need to manually replace the binary at: {}",
                                        current_exe.display()
                                    );
                                }
                            }

                            // Cleanup
                            let _ = std::fs::remove_file(&archive_path);
                            let _ = std::fs::remove_file(&binary_path);
                        }
                        Err(e) => {
                            eprintln!("\nFailed to download/update: {}", e);
                        }
                    }

                    // Cleanup temp dir
                    let _ = std::fs::remove_dir_all(&temp_dir);
                }
            }
        }

        Some(Commands::Config { action }) => {
            match action {
                ConfigAction::Init => {
                    println!("Initializing configuration...");
                    println!("Config file location: ~/.config/research-master/config.toml");
                    println!("Run 'research-master config show' to view current config.");
                    println!("Run 'research-master config edit' to edit config.");
                }
                ConfigAction::Show => {
                    let config_path = find_config_file();
                    if let Some(path) = config_path {
                        match std::fs::read_to_string(&path) {
                            Ok(content) => {
                                println!("Configuration at {}:\n", path.display());
                                println!("{}", content);
                            }
                            Err(_) => {
                                println!("Config file exists but could not be read.");
                            }
                        }
                    } else {
                        println!("No config file found.");
                        println!("Run 'research-master config init' to create one.");
                    }
                }
                ConfigAction::Edit => {
                    let config_path = find_config_file().unwrap_or_else(|| {
                        let path = dirs::config_dir()
                            .unwrap_or_else(|| std::path::PathBuf::from("."))
                            .join("research-master")
                            .join("config.toml");
                        println!("Creating new config at: {}", path.display());
                        let _ = std::fs::create_dir_all(path.parent().unwrap());
                        path
                    });

                    if !config_path.exists() {
                        println!("Creating new config file: {}", config_path.display());
                        let default_config = r#"# Research Master MCP Configuration
# See https://github.com/hongkongkiwi/research-master for documentation

[general]
# Default output format: auto, table, json, plain
output = "auto"

[downloads]
# Default download directory
default_path = "./downloads"
# Organize downloads by source
organize_by_source = true
# Maximum concurrent downloads
concurrency = 5

[cache]
# Enable caching (requires RESEARCH_MASTER_CACHE_ENABLED=true)
enabled = false
# Cache directory
directory = "~/.cache/research-master"

[api_keys]
# Add your API keys here (uncomment and replace with your keys)
# semantic_scholar = "your-api-key"
# core = "your-api-key"
# openalex = "your-email@example.com"
"#;
                        let _ = std::fs::write(&config_path, default_config);
                    }

                    // Open in editor
                    let editor = if cfg!(target_os = "windows") {
                        std::env::var("EDITOR").unwrap_or_else(|_| "notepad".to_string())
                    } else {
                        std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string())
                    };
                    println!("Opening {} in {}...", config_path.display(), editor);
                    let status = std::process::Command::new(&editor)
                        .arg(&config_path)
                        .status();
                    match status {
                        Ok(s) if s.success() => {
                            println!("Config updated successfully.");
                        }
                        Ok(_) => {
                            println!("Editor closed without saving changes.");
                        }
                        Err(e) => {
                            eprintln!("Failed to open editor: {}", e);
                            println!(
                                "You can edit the config manually at: {}",
                                config_path.display()
                            );
                        }
                    }
                }
            }
        }

        Some(Commands::Export {
            input,
            format,
            output,
            source: _,
            query: _,
            max_results: _,
        }) => {
            println!(
                "Export command - format: {:?}, input: {:?}, output: {:?}",
                format, input, output
            );
            println!(
                "Example: research-master export --input papers.json --format bibtex -O output.bib"
            );
            println!(
                "This feature exports search results or input files to BibTeX, CSV, JSON, or RIS format."
            );
        }

        Some(Commands::BulkDownload {
            input,
            output_dir,
            source: _,
            organize_by_source,
            concurrency,
        }) => {
            println!("Bulk download from: {}", input.display());
            println!("Output directory: {}", output_dir.display());
            println!("Organize by source: {}", organize_by_source);
            println!("Concurrency: {}", concurrency);

            // Check if input file exists
            if !input.exists() {
                eprintln!("Error: Input file not found: {}", input.display());
            } else {
                println!("Reading paper IDs from {}...", input.display());
                // This would be implemented to read and download papers
                println!("Feature ready for implementation.");
            }
        }

        Some(Commands::ApiKeys { action, source }) => match action {
            ApiKeyAction::Set => {
                if let Some(src) = source {
                    println!("Set API key for: {}", src);
                    println!("Run 'research-master doctor --check-api-keys' to verify keys.");
                } else {
                    println!("Usage: research-master api-keys set --source <source-name>");
                }
            }
            ApiKeyAction::List => {
                println!("Configured API keys:");
                println!(
                    "  SEMANTIC_SCHOLAR_API_KEY: ***{}",
                    std::env::var("SEMANTIC_SCHOLAR_API_KEY")
                        .map(|s| s.len().to_string())
                        .unwrap_or_else(|_| "not set".to_string())
                );
                println!(
                    "  CORE_API_KEY: ***{}",
                    std::env::var("CORE_API_KEY")
                        .map(|s| s.len().to_string())
                        .unwrap_or_else(|_| "not set".to_string())
                );
                println!(
                    "  OPENALEX_EMAIL: {}",
                    std::env::var("OPENALEX_EMAIL").unwrap_or_else(|_| "not set".to_string())
                );
                println!(
                    "\nRun 'research-master doctor --check-api-keys' to verify configuration."
                );
            }
            ApiKeyAction::Remove => {
                if let Some(src) = source {
                    println!("Remove API key for: {}", src);
                    println!("Unset the corresponding environment variable to disable.");
                } else {
                    println!("Usage: research-master api-keys remove --source <source-name>");
                }
            }
        },

        Some(Commands::Completions { shell }) => {
            use clap::CommandFactory;

            let mut cmd = Cli::command();
            let bin_name = cmd.get_name().to_string();

            match shell {
                Shell::Bash => {
                    clap_complete::generate(Bash, &mut cmd, &bin_name, &mut std::io::stdout());
                }
                Shell::Elvish => {
                    clap_complete::generate(Elvish, &mut cmd, &bin_name, &mut std::io::stdout());
                }
                Shell::Fish => {
                    clap_complete::generate(Fish, &mut cmd, &bin_name, &mut std::io::stdout());
                }
                Shell::PowerShell => {
                    clap_complete::generate(
                        PowerShell,
                        &mut cmd,
                        &bin_name,
                        &mut std::io::stdout(),
                    );
                }
                Shell::Zsh => {
                    clap_complete::generate(Zsh, &mut cmd, &bin_name, &mut std::io::stdout());
                }
            }
            println!();
            println!("To use these completions:");
            println!();
            match shell {
                Shell::Bash => {
                    println!("  # Add to ~/.bashrc or ~/.bash_profile:");
                    println!("  source <({} completions bash)", bin_name);
                }
                Shell::Zsh => {
                    println!("  # Add to ~/.zshrc:");
                    println!("  autoload -U compinit");
                    println!("  compinit");
                    println!(
                        "  {} completions zsh > ~/.zsh/completion/_research-master",
                        bin_name
                    );
                }
                Shell::Fish => {
                    println!("  # Fish handles completions automatically when placed in:");
                    println!("  mkdir -p ~/.config/fish/completions/");
                    println!(
                        "  {} completions fish > ~/.config/fish/completions/research-master.fish",
                        bin_name
                    );
                }
                Shell::PowerShell => {
                    println!("  # Add to your PowerShell profile:");
                    println!(
                        "  {} completions powershell | Out-String | Invoke-Expression",
                        bin_name
                    );
                }
                Shell::Elvish => {
                    println!("  # Add to ~/.elvish/rc.elv:");
                    println!("  use {} completions", bin_name);
                }
            }
        }

        None => {
            // No command provided - show help
            println!("No command provided. Use --help for usage information.");
            println!("Common commands:");
            println!("  search <query>   - Search for papers");
            println!("  author <name>    - Search by author");
            println!("  download <id>    - Download a paper");
            println!("  sources          - List available sources");
            println!("  serve            - Run MCP server");
        }
    }

    Ok(())
}

fn get_source(
    registry: &SourceRegistry,
    source: Source,
) -> Result<&std::sync::Arc<dyn research_master::sources::Source>> {
    let source_id = match source {
        Source::All => anyhow::bail!("Please specify a specific source"),
        s => source_to_id(s),
    };
    registry
        .get_required(source_id)
        .map_err(|e| anyhow::anyhow!(e))
}

fn get_sources(
    registry: &SourceRegistry,
    source: Source,
    capability: SourceCapabilities,
) -> Vec<&std::sync::Arc<dyn research_master::sources::Source>> {
    match source {
        Source::All => registry.with_capability(capability),
        s => {
            let id = source_to_id(s);
            registry.get(id).into_iter().collect()
        }
    }
}

fn source_to_id(source: Source) -> &'static str {
    match source {
        Source::Arxiv => "arxiv",
        Source::Pubmed => "pubmed",
        Source::Biorxiv => "biorxiv",
        Source::Semantic => "semantic",
        Source::OpenAlex => "openalex",
        Source::CrossRef => "crossref",
        Source::Iacr => "iacr",
        Source::Pmc => "pmc",
        Source::Hal => "hal",
        Source::Dblp => "dblp",
        Source::Ssrn => "ssrn",
        Source::Dimensions => "dimensions",
        Source::IeeeXplore => "ieee_xplore",
        Source::EuropePmc => "europe_pmc",
        Source::Core => "core",
        Source::Zenodo => "zenodo",
        Source::Unpaywall => "unpaywall",
        Source::Mdpi => "mdpi",
        Source::Jstor => "jstor",
        Source::Scispace => "scispace",
        Source::Acm => "acm",
        Source::ConnectedPapers => "connected_papers",
        Source::Doaj => "doaj",
        Source::WorldWideScience => "worldwidescience",
        Source::Osf => "osf",
        Source::Base => "base",
        Source::Springer => "springer",
        Source::GoogleScholar => "google_scholar",
        Source::All => unreachable!(),
    }
}

fn output_papers(papers: &[research_master::models::Paper], format: OutputFormat) {
    let actual_format = if format == OutputFormat::Auto {
        if std::io::stdout().is_terminal() {
            OutputFormat::Table
        } else {
            OutputFormat::Json
        }
    } else {
        format
    };

    match actual_format {
        OutputFormat::Json => {
            println!("{}", serde_json::to_string_pretty(papers).unwrap());
        }
        OutputFormat::Plain => {
            for paper in papers {
                println!("{} - {} ({})", paper.title, paper.authors, paper.source);
                println!("  URL: {}", paper.url);
                if let Some(ref doi) = paper.doi {
                    println!("  DOI: {}", doi);
                }
                if let Some(ref pdf_url) = paper.pdf_url {
                    println!("  PDF: {}", pdf_url);
                }
                println!();
            }
        }
        OutputFormat::Table => {
            use comfy_table::{Attribute, Cell, Table};
            let mut table = Table::new();
            table.load_preset(comfy_table::presets::UTF8_FULL);
            table.set_header(vec!["Title", "Authors", "Source", "Year"]);

            for paper in papers {
                let year = paper
                    .published_date
                    .as_ref()
                    .map(|d| d.chars().take(4).collect::<String>())
                    .unwrap_or_default();

                let title = if paper.title.len() > 50 {
                    format!("{}...", &paper.title[..47])
                } else {
                    paper.title.clone()
                };

                let authors = if paper.authors.len() > 30 {
                    format!("{}...", &paper.authors[..27])
                } else {
                    paper.authors.clone()
                };

                table.add_row(vec![
                    Cell::new(title).add_attribute(Attribute::Bold),
                    Cell::new(authors),
                    Cell::new(paper.source.to_string()),
                    Cell::new(year),
                ]);
            }
            println!("{table}");
        }
        OutputFormat::Auto => unreachable!(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cli_version() {
        let version = env!("CARGO_PKG_VERSION");
        assert!(!version.is_empty());
        // Version should be semantic versioning format
        let parts: Vec<&str> = version.split('.').collect();
        assert!(parts.len() >= 2);
        assert!(parts[0].parse::<u32>().is_ok());
    }

    #[test]
    fn test_output_format_values() {
        assert_eq!(OutputFormat::Auto as i32, 0);
        assert_eq!(OutputFormat::Table as i32, 1);
        assert_eq!(OutputFormat::Json as i32, 2);
        assert_eq!(OutputFormat::Plain as i32, 3);
    }

    #[test]
    fn test_cli_default_values() {
        let cli = Cli::parse_from(["research-master"]);
        assert_eq!(cli.verbose, 0);
        assert!(!cli.quiet);
        assert_eq!(cli.output, OutputFormat::Auto);
        assert_eq!(cli.timeout, 30);
        assert!(!cli.no_cache);
        assert!(cli.command.is_none());
    }

    #[test]
    fn test_cli_verbose_flag() {
        let cli = Cli::parse_from(["research-master", "-v"]);
        assert_eq!(cli.verbose, 1);

        let cli = Cli::parse_from(["research-master", "-vv"]);
        assert_eq!(cli.verbose, 2);

        let cli = Cli::parse_from(["research-master", "--verbose"]);
        assert_eq!(cli.verbose, 1);
    }

    #[test]
    fn test_cli_quiet_flag() {
        let cli = Cli::parse_from(["research-master", "-q"]);
        assert!(cli.quiet);

        let cli = Cli::parse_from(["research-master", "--quiet"]);
        assert!(cli.quiet);
    }

    #[test]
    fn test_cli_output_format() {
        let cli = Cli::parse_from(["research-master", "-o", "json"]);
        assert_eq!(cli.output, OutputFormat::Json);

        let cli = Cli::parse_from(["research-master", "--output", "table"]);
        assert_eq!(cli.output, OutputFormat::Table);
    }

    #[test]
    fn test_cli_timeout() {
        let cli = Cli::parse_from(["research-master", "--timeout", "60"]);
        assert_eq!(cli.timeout, 60);
    }

    #[test]
    fn test_cli_config_flag() {
        let cli = Cli::parse_from(["research-master", "--config", "/path/to/config.toml"]);
        assert_eq!(cli.config, Some(PathBuf::from("/path/to/config.toml")));
    }

    #[test]
    fn test_cli_no_cache_flag() {
        let cli = Cli::parse_from(["research-master", "--no-cache"]);
        assert!(cli.no_cache);
    }

    #[test]
    fn test_cli_search_command() {
        let cli = Cli::parse_from(["research-master", "search", "machine learning"]);
        match &cli.command {
            Some(Commands::Search {
                query, max_results, ..
            }) => {
                assert_eq!(query, "machine learning");
                assert_eq!(*max_results, 10);
            }
            _ => panic!("Expected Search command"),
        }
    }

    #[test]
    fn test_cli_search_with_options() {
        let cli = Cli::parse_from([
            "research-master",
            "search",
            "neural networks",
            "--max-results",
            "50",
            "--year",
            "2023",
            "--source",
            "arxiv",
            "--dedup",
        ]);
        match &cli.command {
            Some(Commands::Search {
                query,
                max_results,
                year,
                ..
            }) => {
                assert_eq!(query, "neural networks");
                assert_eq!(*max_results, 50);
                assert_eq!(year.clone(), Some("2023".to_string()));
            }
            _ => panic!("Expected Search command"),
        }
    }

    #[test]
    fn test_cli_download_command() {
        let cli = Cli::parse_from([
            "research-master",
            "download",
            "2301.12345",
            "--source",
            "arxiv",
        ]);
        match &cli.command {
            Some(Commands::Download {
                paper_id,
                source,
                output_path: _,
                auto_filename: _,
                create_dir: _,
                doi: _,
            }) => {
                assert_eq!(paper_id, "2301.12345");
                assert_eq!(*source, Source::Arxiv);
            }
            _ => panic!("Expected Download command"),
        }
    }

    #[test]
    fn test_cli_doi_command() {
        let cli = Cli::parse_from(["research-master", "doi", "10.1234/test"]);
        match &cli.command {
            Some(Commands::LookupByDoi { doi, .. }) => {
                assert_eq!(doi, "10.1234/test");
            }
            _ => panic!("Expected LookupByDoi command"),
        }
    }

    #[test]
    fn test_cli_sources_command() {
        let cli = Cli::parse_from(["research-master", "sources"]);
        match &cli.command {
            Some(Commands::Sources { detailed, .. }) => {
                assert!(!*detailed);
            }
            _ => panic!("Expected Sources command"),
        }
    }

    #[test]
    fn test_cli_sources_detailed() {
        let cli = Cli::parse_from(["research-master", "sources", "--detailed"]);
        match &cli.command {
            Some(Commands::Sources { detailed, .. }) => {
                assert!(*detailed);
            }
            _ => panic!("Expected Sources command"),
        }
    }

    #[test]
    fn test_cli_serve_command() {
        let cli = Cli::parse_from(["research-master", "serve"]);
        match &cli.command {
            Some(Commands::Mcp {
                stdio, port, host, ..
            }) => {
                assert!(*stdio);
                assert_eq!(*port, 3000);
                assert_eq!(host, "127.0.0.1");
            }
            _ => panic!("Expected Serve command"),
        }
    }

    #[test]
    fn test_cli_serve_http_mode() {
        // Just verify the command parses - stdio defaults to true so http doesn't override it
        let cli = Cli::parse_from(["research-master", "serve", "--http"]);
        assert!(matches!(cli.command, Some(Commands::Mcp { .. })));
    }

    // Author command tests
    #[test]
    fn test_cli_author_command() {
        let cli = Cli::parse_from(["research-master", "author", "Geoffrey Hinton"]);
        match &cli.command {
            Some(Commands::Author { author, .. }) => {
                assert_eq!(author, "Geoffrey Hinton");
            }
            _ => panic!("Expected Author command"),
        }
    }

    #[test]
    fn test_cli_author_with_source() {
        let cli = Cli::parse_from([
            "research-master",
            "author",
            "Geoffrey Hinton",
            "--source",
            "semantic",
        ]);
        match &cli.command {
            Some(Commands::Author { author, source, .. }) => {
                assert_eq!(author, "Geoffrey Hinton");
                assert_eq!(*source, Source::Semantic);
            }
            _ => panic!("Expected Author command"),
        }
    }

    // Read command tests
    #[test]
    fn test_cli_read_command() {
        let cli = Cli::parse_from([
            "research-master",
            "read",
            "2301.12345",
            "--source",
            "arxiv",
            "--path",
            "/path/to/paper.pdf",
        ]);
        match &cli.command {
            Some(Commands::Read { paper_id, .. }) => {
                assert_eq!(paper_id, "2301.12345");
            }
            _ => panic!("Expected Read command"),
        }
    }

    #[test]
    fn test_cli_read_with_options() {
        let cli = Cli::parse_from([
            "research-master",
            "read",
            "2301.12345",
            "--source",
            "arxiv",
            "--path",
            "/path/to/paper.pdf",
            "--output-file",
            "output.txt",
            "--pages",
            "5",
        ]);
        match &cli.command {
            Some(Commands::Read {
                paper_id,
                source,
                pages,
                output_file,
                path: _,
                ..
            }) => {
                assert_eq!(paper_id, "2301.12345");
                assert_eq!(*source, Source::Arxiv);
                assert_eq!(*pages, Some(5));
                assert_eq!(
                    output_file.clone().map(|p| p.to_string_lossy().to_string()),
                    Some("output.txt".to_string())
                );
            }
            _ => panic!("Expected Read command"),
        }
    }

    // Citations command tests
    #[test]
    fn test_cli_citations_command() {
        let cli = Cli::parse_from([
            "research-master",
            "citations",
            "2301.12345",
            "--source",
            "arxiv",
        ]);
        match &cli.command {
            Some(Commands::Citations { paper_id, .. }) => {
                assert_eq!(paper_id, "2301.12345");
            }
            _ => panic!("Expected Citations command"),
        }
    }

    #[test]
    fn test_cli_citations_with_options() {
        let cli = Cli::parse_from([
            "research-master",
            "citations",
            "2301.12345",
            "--source",
            "semantic",
            "--max-results",
            "50",
        ]);
        match &cli.command {
            Some(Commands::Citations {
                paper_id,
                source,
                max_results,
            }) => {
                assert_eq!(paper_id, "2301.12345");
                assert_eq!(*source, Source::Semantic);
                assert_eq!(*max_results, 50);
            }
            _ => panic!("Expected Citations command"),
        }
    }

    // References command tests
    #[test]
    fn test_cli_references_command() {
        let cli = Cli::parse_from([
            "research-master",
            "references",
            "1706.03762",
            "--source",
            "semantic",
        ]);
        match &cli.command {
            Some(Commands::References { paper_id, .. }) => {
                assert_eq!(paper_id, "1706.03762");
            }
            _ => panic!("Expected References command"),
        }
    }

    #[test]
    fn test_cli_references_alias() {
        let cli = Cli::parse_from([
            "research-master",
            "ref",
            "1706.03762",
            "--source",
            "semantic",
        ]);
        assert!(matches!(cli.command, Some(Commands::References { .. })));
    }

    // Related command tests
    #[test]
    fn test_cli_related_command() {
        let cli = Cli::parse_from([
            "research-master",
            "related",
            "1706.03762",
            "--source",
            "connected_papers",
        ]);
        match &cli.command {
            Some(Commands::Related { paper_id, .. }) => {
                assert_eq!(paper_id, "1706.03762");
            }
            _ => panic!("Expected Related command"),
        }
    }

    #[test]
    fn test_cli_related_alias() {
        let cli = Cli::parse_from([
            "research-master",
            "rel",
            "1706.03762",
            "--source",
            "connected_papers",
        ]);
        assert!(matches!(cli.command, Some(Commands::Related { .. })));
    }

    // Lookup command tests
    #[test]
    fn test_cli_lookup_command() {
        let cli = Cli::parse_from(["research-master", "doi", "10.1234/test"]);
        match &cli.command {
            Some(Commands::LookupByDoi { doi, .. }) => {
                assert_eq!(doi, "10.1234/test");
            }
            _ => panic!("Expected LookupByDoi command"),
        }
    }

    #[test]
    fn test_cli_lookup_with_source() {
        let cli = Cli::parse_from([
            "research-master",
            "doi",
            "10.1234/test",
            "--source",
            "crossref",
        ]);
        match &cli.command {
            Some(Commands::LookupByDoi { doi, source, .. }) => {
                assert_eq!(doi, "10.1234/test");
                assert_eq!(*source, Source::CrossRef);
            }
            _ => panic!("Expected LookupByDoi command"),
        }
    }

    // Cache command tests
    #[test]
    fn test_cli_cache_status() {
        let cli = Cli::parse_from(["research-master", "cache", "status"]);
        assert!(matches!(
            cli.command,
            Some(Commands::Cache {
                command: CacheCommands::Status
            })
        ));
    }

    #[test]
    fn test_cli_cache_clear() {
        let cli = Cli::parse_from(["research-master", "cache", "clear"]);
        assert!(matches!(
            cli.command,
            Some(Commands::Cache {
                command: CacheCommands::Clear
            })
        ));
    }

    #[test]
    fn test_cli_cache_clear_searches() {
        let cli = Cli::parse_from(["research-master", "cache", "clear-searches"]);
        assert!(matches!(
            cli.command,
            Some(Commands::Cache {
                command: CacheCommands::ClearSearches
            })
        ));
    }

    #[test]
    fn test_cli_cache_clear_citations() {
        let cli = Cli::parse_from(["research-master", "cache", "clear-citations"]);
        assert!(matches!(
            cli.command,
            Some(Commands::Cache {
                command: CacheCommands::ClearCitations
            })
        ));
    }

    // Doctor command tests
    #[test]
    fn test_cli_doctor_command() {
        let cli = Cli::parse_from(["research-master", "doctor"]);
        match &cli.command {
            Some(Commands::Doctor {
                check_connectivity,
                check_api_keys,
                verbose,
            }) => {
                assert!(!*check_connectivity);
                assert!(!*check_api_keys);
                assert!(!*verbose);
            }
            _ => panic!("Expected Doctor command"),
        }
    }

    #[test]
    fn test_cli_doctor_with_options() {
        let cli = Cli::parse_from([
            "research-master",
            "doctor",
            "--check-connectivity",
            "--check-api-keys",
            "--verbose",
        ]);
        match &cli.command {
            Some(Commands::Doctor {
                check_connectivity,
                check_api_keys,
                verbose,
            }) => {
                assert!(*check_connectivity);
                assert!(*check_api_keys);
                assert!(*verbose);
            }
            _ => panic!("Expected Doctor command"),
        }
    }

    #[test]
    fn test_cli_doctor_alias() {
        let cli = Cli::parse_from(["research-master", "diag"]);
        assert!(matches!(cli.command, Some(Commands::Doctor { .. })));
    }

    // Update command tests
    #[test]
    fn test_cli_update_command() {
        let cli = Cli::parse_from(["research-master", "update"]);
        match &cli.command {
            Some(Commands::Update { force, dry_run }) => {
                assert!(!*force);
                assert!(!*dry_run);
            }
            _ => panic!("Expected Update command"),
        }
    }

    #[test]
    fn test_cli_update_with_options() {
        let cli = Cli::parse_from(["research-master", "update", "--force", "--dry-run"]);
        match &cli.command {
            Some(Commands::Update { force, dry_run }) => {
                assert!(*force);
                assert!(*dry_run);
            }
            _ => panic!("Expected Update command"),
        }
    }

    // Completions command tests
    #[test]
    fn test_cli_completions_bash() {
        let cli = Cli::parse_from(["research-master", "completions", "bash"]);
        match &cli.command {
            Some(Commands::Completions { shell }) => {
                assert!(matches!(shell, Shell::Bash));
            }
            _ => panic!("Expected Completions command"),
        }
    }

    #[test]
    fn test_cli_completions_zsh() {
        let cli = Cli::parse_from(["research-master", "completions", "zsh"]);
        match &cli.command {
            Some(Commands::Completions { shell }) => {
                assert!(matches!(shell, Shell::Zsh));
            }
            _ => panic!("Expected Completions command"),
        }
    }

    #[test]
    fn test_cli_completions_fish() {
        let cli = Cli::parse_from(["research-master", "completions", "fish"]);
        match &cli.command {
            Some(Commands::Completions { shell }) => {
                assert!(matches!(shell, Shell::Fish));
            }
            _ => panic!("Expected Completions command"),
        }
    }

    #[test]
    fn test_cli_completions_powershell() {
        let cli = Cli::parse_from(["research-master", "completions", "power-shell"]);
        match &cli.command {
            Some(Commands::Completions { shell }) => {
                assert!(matches!(shell, Shell::PowerShell));
            }
            _ => panic!("Expected Completions command"),
        }
    }

    #[test]
    fn test_cli_completions_alias() {
        let cli = Cli::parse_from(["research-master", "completion", "bash"]);
        assert!(matches!(cli.command, Some(Commands::Completions { .. })));
    }

    // Dedupe command tests
    #[test]
    fn test_cli_dedupe_command() {
        let cli = Cli::parse_from(["research-master", "dedupe", "papers.json"]);
        match &cli.command {
            Some(Commands::Dedupe { input, .. }) => {
                assert_eq!(input.to_string_lossy(), "papers.json");
            }
            _ => panic!("Expected Dedupe command"),
        }
    }

    #[test]
    fn test_cli_dedupe_with_options() {
        let cli = Cli::parse_from([
            "research-master",
            "dedupe",
            "papers.json",
            "-O",
            "deduped.json",
            "--strategy",
            "last",
            "--show",
        ]);
        match &cli.command {
            Some(Commands::Dedupe {
                input,
                output_file,
                strategy,
                show,
            }) => {
                assert_eq!(input.to_string_lossy(), "papers.json");
                assert_eq!(
                    output_file.clone().map(|p| p.to_string_lossy().to_string()),
                    Some("deduped.json".to_string())
                );
                assert_eq!(*strategy, DedupStrategy::Last);
                assert!(*show);
            }
            _ => panic!("Expected Dedupe command"),
        }
    }

    #[test]
    fn test_cli_dedupe_alias() {
        let cli = Cli::parse_from(["research-master", "dedup", "papers.json"]);
        assert!(matches!(cli.command, Some(Commands::Dedupe { .. })));
    }

    // Search with all options
    #[test]
    fn test_cli_search_all_options() {
        let cli = Cli::parse_from([
            "research-master",
            "search",
            "transformer",
            "--source",
            "arxiv",
            "--max-results",
            "25",
            "--year",
            "2020-2023",
            "--sort-by",
            "citations",
            "--order",
            "desc",
            "--category",
            "cs.CL",
            "--author",
            "Vaswani",
            "--dedup",
            "--dedup-strategy",
            "mark",
        ]);
        match &cli.command {
            Some(Commands::Search {
                query,
                source,
                max_results,
                year,
                sort_by,
                order,
                category,
                author,
                dedup,
                dedup_strategy,
                fetch_details,
            }) => {
                assert_eq!(query, "transformer");
                assert_eq!(*source, Source::Arxiv);
                assert_eq!(*max_results, 25);
                assert_eq!(year.clone(), Some("2020-2023".to_string()));
                assert_eq!(*sort_by, Some(SortField::Citations));
                assert_eq!(*order, Some(Order::Desc));
                assert_eq!(category.clone(), Some("cs.CL".to_string()));
                assert_eq!(author.clone(), Some("Vaswani".to_string()));
                assert!(*dedup);
                assert_eq!(*dedup_strategy, Some(DedupStrategy::Mark));
                assert!(*fetch_details); // Default is true
            }
            _ => panic!("Expected Search command"),
        }
    }

    // Source enum variant tests
    #[test]
    fn test_source_enum_all_variants() {
        let variants = [
            Source::Arxiv,
            Source::Pubmed,
            Source::Biorxiv,
            Source::Semantic,
            Source::OpenAlex,
            Source::CrossRef,
            Source::Iacr,
            Source::Pmc,
            Source::Hal,
            Source::Dblp,
            Source::Ssrn,
            Source::Dimensions,
            Source::IeeeXplore,
            Source::EuropePmc,
            Source::Core,
            Source::Zenodo,
            Source::Unpaywall,
            Source::Mdpi,
            Source::Jstor,
            Source::Scispace,
            Source::Acm,
            Source::ConnectedPapers,
            Source::Doaj,
            Source::WorldWideScience,
            Source::Osf,
            Source::Base,
            Source::Springer,
            Source::GoogleScholar,
            Source::All,
        ];
        assert_eq!(variants.len(), 29);
    }

    #[test]
    fn test_source_to_id_all_variants() {
        let tests = [
            (Source::Arxiv, "arxiv"),
            (Source::Pubmed, "pubmed"),
            (Source::Biorxiv, "biorxiv"),
            (Source::Semantic, "semantic"),
            (Source::OpenAlex, "openalex"),
            (Source::CrossRef, "crossref"),
            (Source::Iacr, "iacr"),
            (Source::Pmc, "pmc"),
            (Source::Hal, "hal"),
            (Source::Dblp, "dblp"),
            (Source::Ssrn, "ssrn"),
            (Source::Dimensions, "dimensions"),
            (Source::IeeeXplore, "ieee_xplore"),
            (Source::EuropePmc, "europe_pmc"),
            (Source::Core, "core"),
            (Source::Zenodo, "zenodo"),
            (Source::Unpaywall, "unpaywall"),
            (Source::Mdpi, "mdpi"),
            (Source::Jstor, "jstor"),
            (Source::Scispace, "scispace"),
            (Source::Acm, "acm"),
            (Source::ConnectedPapers, "connected_papers"),
            (Source::Doaj, "doaj"),
            (Source::WorldWideScience, "worldwidescience"),
            (Source::Osf, "osf"),
            (Source::Base, "base"),
            (Source::Springer, "springer"),
            (Source::GoogleScholar, "google_scholar"),
        ];
        for (source, expected_id) in tests {
            assert_eq!(source_to_id(source), expected_id, "Failed for {:?}", source);
        }
    }

    // Sort field tests
    #[test]
    fn test_sort_field_enum() {
        assert_eq!(SortField::Relevance as i32, 0);
        assert_eq!(SortField::Date as i32, 1);
        assert_eq!(SortField::Citations as i32, 2);
        assert_eq!(SortField::Title as i32, 3);
        assert_eq!(SortField::Author as i32, 4);
    }

    #[test]
    fn test_order_enum() {
        assert_eq!(Order::Asc as i32, 0);
        assert_eq!(Order::Desc as i32, 1);
    }

    #[test]
    fn test_dedup_strategy_enum() {
        assert_eq!(DedupStrategy::First as i32, 0);
        assert_eq!(DedupStrategy::Last as i32, 1);
        assert_eq!(DedupStrategy::Mark as i32, 2);
    }

    // Capability filter tests
    #[test]
    fn test_capability_filter_enum() {
        assert_eq!(CapabilityFilter::Search as i32, 0);
        assert_eq!(CapabilityFilter::Download as i32, 1);
        assert_eq!(CapabilityFilter::Read as i32, 2);
        assert_eq!(CapabilityFilter::Citations as i32, 3);
        assert_eq!(CapabilityFilter::DoiLookup as i32, 4);
        assert_eq!(CapabilityFilter::AuthorSearch as i32, 5);
    }

    // Download with all options
    #[test]
    fn test_cli_download_all_options() {
        let cli = Cli::parse_from([
            "research-master",
            "download",
            "2301.12345",
            "--source",
            "arxiv",
            "--output-path",
            "/path/to/file.pdf",
            "--auto-filename",
            "--create-dir",
            "--doi",
            "10.1234/test",
        ]);
        match &cli.command {
            Some(Commands::Download {
                paper_id,
                source,
                output_path,
                auto_filename,
                create_dir,
                doi,
            }) => {
                assert_eq!(paper_id, "2301.12345");
                assert_eq!(*source, Source::Arxiv);
                assert_eq!(
                    output_path.clone().map(|p| p.to_string_lossy().to_string()),
                    Some("/path/to/file.pdf".to_string())
                );
                assert!(*auto_filename);
                assert!(*create_dir);
                assert_eq!(
                    doi.clone().map(|d| d.to_string()),
                    Some("10.1234/test".to_string())
                );
            }
            _ => panic!("Expected Download command"),
        }
    }

    // Sources with capability filter
    #[test]
    fn test_cli_sources_with_capability() {
        let cli = Cli::parse_from([
            "research-master",
            "sources",
            "--with-capability",
            "download",
        ]);
        match &cli.command {
            Some(Commands::Sources {
                with_capability, ..
            }) => {
                assert_eq!(*with_capability, Some(CapabilityFilter::Download));
            }
            _ => panic!("Expected Sources command"),
        }
    }

    // Author with all options
    #[test]
    fn test_cli_author_all_options() {
        let cli = Cli::parse_from([
            "research-master",
            "author",
            "Geoffrey Hinton",
            "--source",
            "all",
            "--max-results",
            "20",
            "--year",
            "2010-",
            "--dedup",
            "--dedup-strategy",
            "first",
        ]);
        match &cli.command {
            Some(Commands::Author {
                author,
                source,
                max_results,
                year,
                dedup,
                dedup_strategy,
            }) => {
                assert_eq!(author, "Geoffrey Hinton");
                assert_eq!(*source, Source::All);
                assert_eq!(*max_results, 20);
                assert_eq!(year.clone(), Some("2010-".to_string()));
                assert!(*dedup);
                assert_eq!(*dedup_strategy, Some(DedupStrategy::First));
            }
            _ => panic!("Expected Author command"),
        }
    }
}