kandil_code 2.1.1

Intelligent development platform (CLI + TUI + Multi-Agent System) with cross-platform AI model benchmarking, system diagnostics, and advanced development tools
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
use crate::adapters::{edge, linux, macos, mobile, windows};
use crate::benchmark::{BenchmarkOptions, CrossPlatformBenchmark};
use crate::core::adapters::ai::factory::AIProviderFactory;
use crate::core::hardware::{detect_hardware, PlatformKind};
use crate::core::prompting::{PromptIntent, PromptRouter};
use crate::enhanced_ui;
use crate::pwa;
use crate::security::mobile as mobile_security;
use crate::security::platform::PlatformHardener;
use crate::utils::config::{Config, SecureKey};
use crate::utils::plugins::PluginManager;
use crate::utils::project_manager::ProjectManager;
use crate::utils::refactoring::{RefactorEngine, RefactorParams};
use crate::utils::templates::TemplateEngine;
use crate::utils::test_generation::TestGenerator;
use anyhow::Result;
use clap::{Parser, Subcommand};
use futures_util::stream::StreamExt;
use std::{path::PathBuf, sync::Arc};
use tokio::{fs, io::AsyncWriteExt, task};

#[derive(Parser)]
#[command(name = "kandil")]
#[command(about = "Intelligent development platform", long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,

    #[arg(short, long, global = true, help = "Verbose output")]
    pub verbose: bool,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Initialize a new Kandil project
    Init,
    /// Chat with the AI assistant
    Chat {
        #[arg(value_parser)]
        message: Option<String>,
    },
    /// Create a new project from template
    Create {
        /// Template name (flutter, python, javascript, rust)
        #[arg(value_parser)]
        template: String,
        /// Project name
        #[arg(value_parser)]
        name: String,
    },
    /// Launch the TUI studio
    Tui,
    /// Project management commands
    Projects {
        #[command(subcommand)]
        sub: ProjectSub,
    },
    /// Agent commands for requirements, design, etc.
    Agent {
        #[command(subcommand)]
        sub: AgentSub,
    },
    /// Refactoring commands
    Refactor {
        #[command(subcommand)]
        sub: RefactorSub,
    },
    /// Test generation commands
    Test {
        #[command(subcommand)]
        sub: TestSub,
    },
    /// Model switching commands
    SwitchModel { provider: String, model: String },
    /// Plugin management commands
    Plugin {
        #[command(subcommand)]
        sub: PluginSub,
    },
    /// Configuration management commands
    Config {
        #[command(subcommand)]
        sub: ConfigSub,
    },
    /// Local model management commands
    LocalModel {
        #[command(subcommand)]
        sub: LocalModelSub,
    },
    /// Authentication commands
    Auth {
        #[command(subcommand)]
        sub: AuthSub,
    },
    /// System diagnostics and health checks
    Doctor {
        #[arg(long)]
        verbose: bool,
        #[arg(long, default_value = "json")]
        format: String,
    },
    /// Windows-specific local runtime helpers
    Windows {
        #[command(subcommand)]
        sub: WindowsSub,
    },
    /// macOS-specific runtime helpers
    Macos {
        #[command(subcommand)]
        sub: MacosSub,
    },
    /// Linux-specific runtime helpers
    Linux {
        #[command(subcommand)]
        sub: LinuxSub,
    },
    /// Mobile & edge runtime helpers
    Mobile {
        #[command(subcommand)]
        sub: MobileSub,
    },
    /// Export PWA assets for offline installs
    Pwa {
        #[arg(long, default_value = "dist/pwa")]
        output: PathBuf,
    },
    /// Launch the Axum-based web companion dashboard
    Web {
        /// Address to bind, e.g. 127.0.0.1:7878
        #[arg(short, long, default_value = "127.0.0.1:7878")]
        address: String,
    },
}

#[derive(Subcommand)]
pub enum AgentSub {
    /// Generate requirements document
    Requirements {
        /// Description of the project to analyze
        description: String,
    },
    /// Generate design document
    Design {
        /// Description of the requirements to design for
        requirements: String,
    },
    /// Generate code from design
    Code {
        /// Path to design document
        design_path: String,
        /// Target language
        language: String,
    },
    /// Generate or execute tests
    Test {
        #[command(subcommand)]
        sub: TestSubCommand,
    },
    /// Generate documentation
    Documentation {
        #[command(subcommand)]
        sub: DocumentationSubCommand,
    },
    /// Manage releases
    Release {
        #[command(subcommand)]
        sub: ReleaseSubCommand,
    },
    /// Run quality assurance checks
    Qa {
        #[command(subcommand)]
        sub: QaSubCommand,
    },
    /// Manage system maintenance
    Maintenance {
        #[command(subcommand)]
        sub: MaintenanceSubCommand,
    },
    /// Professional role simulations
    Simulate {
        #[command(subcommand)]
        sub: SimulateSubCommand,
    },
    /// Advanced agents for review, security, deployment
    Advanced {
        #[command(subcommand)]
        sub: AdvancedSubCommand,
    },
    /// Tech role simulations (Architect, Developer, QA)
    TechRole {
        #[command(subcommand)]
        sub: TechRoleSubCommand,
    },
    /// DevOps, Scrum, and advanced feature simulations
    AdvancedFeatures {
        #[command(subcommand)]
        sub: AdvancedFeaturesSubCommand,
    },
}

#[derive(Subcommand)]
pub enum MaintenanceSubCommand {
    /// Run a health check for a system
    HealthCheck {
        /// Name of the system to check
        system_name: String,
    },
}

#[derive(Subcommand)]
pub enum QaSubCommand {
    /// Run the full QA suite for a project
    FullSuite {
        /// Path to the project to run QA on
        project_path: String,
    },
}

#[derive(Subcommand)]
pub enum ReleaseSubCommand {
    /// Run the full release process
    FullProcess {
        /// Version to release
        version: String,
    },
}

#[derive(Subcommand)]
pub enum DocumentationSubCommand {
    /// Generate documentation for a project
    Generate {
        /// Path to the project to document
        path: String,
    },
}

#[derive(Subcommand)]
pub enum AdvancedFeaturesSubCommand {
    /// DevOps activities (IaC, CI/CD, monitoring)
    DevOps {
        #[command(subcommand)]
        sub: DevOpsSubCommand,
    },
    /// Scrum ceremony simulations
    Scrum {
        #[command(subcommand)]
        sub: ScrumSubCommand,
    },
    /// Internationalization assistance
    I18n {
        #[command(subcommand)]
        sub: I18nSubCommand,
    },
    /// Accessibility compliance checking
    A11y {
        #[command(subcommand)]
        sub: A11ySubCommand,
    },
    /// Real-time collaboration features
    Collab {
        #[command(subcommand)]
        sub: CollabSubCommand,
    },
    /// IDE extension prototype
    Ide {
        #[command(subcommand)]
        sub: IdeSubCommand,
    },
}

#[derive(Subcommand)]
pub enum DevOpsSubCommand {
    /// Generate Terraform infrastructure code
    Terraform {
        /// Infrastructure specification
        spec: String,
    },
    /// Run incident response drill
    Drill {
        /// Scenario to drill
        scenario: String,
    },
    /// Generate CI/CD pipeline
    Pipeline {
        /// Project type
        project_type: String,
    },
}

#[derive(Subcommand)]
pub enum ScrumSubCommand {
    /// Plan a sprint
    Plan {
        /// Sprint goal
        goal: String,
        /// Duration in days
        duration: u32,
        /// Team size
        team_size: u32,
    },
    /// Run retrospective for a sprint
    Retro {
        /// Sprint number
        sprint: u32,
    },
    /// Conduct a Scrum ceremony
    Ceremony {
        /// Type of ceremony (sprint_planning, daily_scrum, etc.)
        ceremony_type: String,
        /// Comma-separated list of participants
        participants: String,
    },
}

#[derive(Subcommand)]
pub enum I18nSubCommand {
    /// Translate text to a target language
    Translate {
        /// Text to translate
        text: String,
        /// Target language code (en, es, fr, etc.)
        target: String,
        /// Source language code
        source: String,
    },
    /// Audit translations in a directory
    Audit {
        /// Path to resource directory
        path: String,
    },
    /// Review a specific translation
    Review {
        /// Original text
        original: String,
        /// Translated text
        translation: String,
        /// Target language code
        target: String,
    },
}

#[derive(Subcommand)]
pub enum A11ySubCommand {
    /// Audit content for accessibility (WCAG AA level)
    Audit {
        /// Content to audit
        content: String,
    },
    /// Generate accessibility guidelines for a component
    Guidelines {
        /// Component type
        component: String,
    },
    /// Remediate accessibility issues in HTML
    Fix {
        /// HTML content to remediate
        html: String,
    },
}

#[derive(Subcommand)]
pub enum CollabSubCommand {
    /// Create a collaboration session
    Session {
        /// Session name
        name: String,
        /// Creator ID
        creator_id: String,
        /// Creator name
        creator_name: String,
    },
    /// Add a participant to session
    AddParticipant {
        /// Session ID
        session_id: String,
        /// Participant ID
        user_id: String,
        /// Participant name
        name: String,
        /// Role in session
        role: String,
    },
    /// Add a document to session
    AddDoc {
        /// Session ID
        session_id: String,
        /// Document ID
        doc_id: String,
        /// Document name
        name: String,
        /// Initial content
        content: String,
        /// Language
        language: String,
    },
}

#[derive(Subcommand)]
pub enum IdeSubCommand {
    /// Get code suggestions for selected code
    Suggestions {
        /// File path
        file: String,
        /// Language of the code
        language: String,
        /// Selected code snippet
        code: String,
    },
    /// Generate documentation for code
    Docs {
        /// Code to document
        code: String,
        /// Language of the code
        language: String,
    },
    /// Get refactoring options
    Refactor {
        /// Code to refactor
        code: String,
        /// Language of the code
        language: String,
    },
    /// Run inline code review
    Review {
        /// Code to review
        code: String,
        /// Language of the code
        language: String,
    },
}

#[derive(Subcommand)]
pub enum TechRoleSubCommand {
    /// Architect role simulation
    Architect {
        #[command(subcommand)]
        sub: ArchitectSubCommand,
    },
    /// Developer role simulation
    Developer {
        #[command(subcommand)]
        sub: DeveloperSubCommand,
    },

    /// Cross-role collaboration
    Collaborate {
        #[command(subcommand)]
        sub: CollaborateSubCommand,
    },
}

#[derive(Subcommand)]
pub enum ArchitectSubCommand {
    /// Review architecture design
    Review {
        /// UML or architecture diagram to review
        diagram: String,
    },
    /// Make an architecture decision
    Decide {
        /// Context for the decision
        context: String,
        /// The decision to make
        decision: String,
    },
    /// Generate Architecture Decision Record
    Adr {
        /// Decision ID to generate ADR for
        decision_id: String,
    },
}

#[derive(Subcommand)]
pub enum DeveloperSubCommand {
    /// Implement a feature
    Implement {
        /// Feature specification
        spec: String,
        /// File to implement in
        file: String,
    },
    /// Start pair programming session
    Pair {
        /// Name of the pair programming partner
        partner: String,
        /// Task to work on
        task: String,
        /// File to work on
        file: String,
    },
    /// Find bugs in code
    Bugs {
        /// Code to analyze for bugs
        code: String,
        /// File path of the code
        file: String,
    },
}

#[derive(Subcommand)]
pub enum CollaborateSubCommand {
    /// Start a cross-role collaboration session
    Session {
        /// Session title
        title: String,
        /// Comma-separated list of roles (architect,developer,qa)
        roles: String,
        /// Session agenda
        agenda: Vec<String>,
    },
    /// Create a cross-role decision
    Decision {
        /// Decision title
        title: String,
        /// Decision description
        description: String,
        /// Comma-separated list of involved roles
        roles: String,
    },
}

#[derive(Subcommand)]
pub enum AdvancedSubCommand {
    /// Code review and quality analysis
    Review {
        /// Path to source file
        file: String,
    },
    /// Security and ethics scanning
    Security {
        /// Path to source file
        file: String,
        /// Description of the system
        description: String,
    },
    /// Deployment planning and execution
    Deploy {
        #[command(subcommand)]
        sub: DeploySubCommand,
    },
    /// System self-improvement analysis
    SelfImprove {
        /// Path to codebase to analyze
        path: String,
    },
}

#[derive(Subcommand)]
pub enum DeploySubCommand {
    /// Create a deployment plan
    Plan {
        /// Environment to deploy to
        environment: String,
        /// Application name
        app: String,
    },
    /// Execute a deployment
    Execute {
        /// Path to deployment plan
        plan: String,
    },
}

#[derive(Subcommand)]
pub enum TestSubCommand {
    /// Generate tests for a source file
    Generate {
        /// Path to source file
        source: String,
        /// Target language
        language: String,
    },
    /// Execute tests
    Execute {
        /// Path to test file
        test: String,
        /// Test framework to use
        framework: String,
    },
    /// Analyze test coverage
    Coverage {
        /// Path to source file
        source: String,
        /// Path to test file
        test: String,
    },
}

#[derive(Subcommand)]
pub enum SimulateSubCommand {
    /// Simulate Project Manager activities
    Pm {
        #[command(subcommand)]
        sub: PmSubCommand,
    },
    /// Simulate Business Analyst activities
    Ba {
        #[command(subcommand)]
        sub: BaSubCommand,
    },
}

#[derive(Subcommand)]
pub enum PmSubCommand {
    /// Plan a sprint
    PlanSprint {
        /// Project name
        project: String,
        /// Sprint duration in weeks
        duration: u32,
    },
    /// Run sprint retrospective
    Retrospective {
        /// Sprint number
        sprint: u32,
    },
}

#[derive(Subcommand)]
pub enum BaSubCommand {
    /// Validate requirements
    Validate {
        /// Requirements document path
        requirements: String,
    },
    /// Create a user story
    UserStory {
        /// Feature description
        feature: String,
    },
}

#[derive(Subcommand)]
pub enum ProjectSub {
    /// List all projects
    List,
    /// Switch to a project
    Switch {
        /// Project ID to switch to
        id: String,
    },
    /// Sync project with cloud
    Sync {
        /// Project ID to sync (current if not specified)
        id: Option<String>,
    },
    /// Show project information
    Info {
        /// Project ID to show info for (current if not specified)
        id: Option<String>,
    },
}

#[derive(Subcommand)]
pub enum RefactorSub {
    /// Preview a refactoring operation
    Preview {
        /// File to refactor
        file: String,
        /// Type of refactoring (rename_variable, extract_function, etc.)
        refactor_type: String,
        /// Additional parameters for the refactoring
        #[arg(short, long)]
        params: Vec<String>,
    },
    /// Apply all pending refactor operations
    Apply,
    /// Cancel pending refactor operations
    Cancel,
}

#[derive(Subcommand)]
pub enum TestSub {
    /// Generate unit tests for a file
    Generate {
        /// Source file to generate tests for
        file: String,
        /// Test framework to use (rust, pytest, jest, flutter)
        #[arg(short, long, default_value = "")]
        framework: String,
    },
    /// Generate integration tests
    Integration {
        /// Feature description for integration tests
        feature: String,
    },
    /// Analyze test coverage
    Coverage {
        /// Source file
        source: String,
        /// Test file
        test: String,
    },
}

#[derive(Subcommand)]
pub enum PluginSub {
    /// Install a plugin from URL or file
    Install { source: String },
    /// List installed plugins
    List,
    /// Execute a plugin
    Run { name: String, args: Vec<String> },
}

#[derive(Subcommand)]
pub enum ConfigSub {
    /// Set API key for a provider
    SetKey { provider: String, key: String },
    /// List configured API keys
    ListKeys,
    /// Show cost statistics
    Costs {
        /// Provider to show costs for (all if not specified)
        provider: Option<String>,
    },
    /// Validate production configuration
    Validate,
}

#[derive(Subcommand)]
pub enum LocalModelSub {
    /// List installed local models
    List {
        /// Show only models compatible with your hardware
        #[arg(long)]
        compatible: bool,
    },
    /// Install a local model
    Install {
        /// Model name (e.g., qwen2.5-coder-7b-q4)
        #[arg(value_parser)]
        model: String,
        /// Force install even if hardware is insufficient
        #[arg(long)]
        force: bool,
    },
    /// Remove a local model
    Remove {
        #[arg(value_parser)]
        model: String,
    },
    /// Verify model integrity
    Verify {
        #[arg(value_parser)]
        model: String,
    },
    /// Benchmark installed model
    Benchmark {
        #[arg(value_parser)]
        model: Option<String>,
        /// Output format
        #[arg(long, default_value = "table")]
        format: String,
        /// Force a specific runtime (ollama, lmstudio, gpt4all, foundry, default)
        #[arg(long)]
        runtime: Option<String>,
        /// Benchmark every detected runtime
        #[arg(long)]
        all_runtimes: bool,
    },
    /// Use a local model and persist selection
    Use {
        #[arg(value_parser)]
        model: String,
    },
    /// Show local model system status
    Status,
}

struct BenchmarkCliOptions {
    model: Option<String>,
    format: String,
    runtime: Option<String>,
    all_runtimes: bool,
}

#[derive(Subcommand)]
pub enum AuthSub {
    Login { provider: String },
}

#[derive(Subcommand)]
pub enum WindowsSub {
    /// Show the status of local adapters (Ollama WSL2, LM Studio, GPT4All, Foundry)
    Status,
    /// Run GPU passthrough diagnostics for WSL2
    CheckGpu,
    /// Run comprehensive system diagnostics
    Doctor,
    /// Print WSL2 + Ollama setup instructions
    SetupWsl2,
}

#[derive(Subcommand)]
pub enum MacosSub {
    /// Display Core ML status and ANE availability
    Status,
    /// Run comprehensive system diagnostics
    Doctor,
    /// Show setup instructions for Core ML runtimes
    SetupCoreml,
}

#[derive(Subcommand)]
pub enum LinuxSub {
    /// Display local runtime status (Ollama socket, CUDA availability)
    Status,
    /// Run hardening checks for the current system
    Doctor,
    /// Show recommended setup instructions for Linux hosts
    Setup,
}

#[derive(Subcommand)]
pub enum MobileSub {
    /// Create an encrypted-ready bundle for iOS devices
    IosSync,
    /// Prepare an Android-ready bundle for Google AI Core / Termux
    AndroidSync,
    /// Export an ONNX-ready manifest for edge devices
    EdgeSnapshot,
}

pub async fn run(cli: Cli) -> Result<()> {
    match cli.command {
        Some(Commands::Init) => init_project().await?,
        Some(Commands::Chat { message }) => chat(message.unwrap_or_default()).await?,
        Some(Commands::Create { template, name }) => create_project(&template, &name).await?,
        Some(Commands::Tui) => launch_tui().await?,
        Some(Commands::Projects { sub }) => handle_projects(sub).await?,
        Some(Commands::Agent { sub }) => handle_agent(sub).await?,
        Some(Commands::Refactor { sub }) => handle_refactor(sub).await?,
        Some(Commands::Test { sub }) => handle_test(sub).await?,
        Some(Commands::SwitchModel { provider, model }) => switch_model(provider, model).await?,
        Some(Commands::Plugin { sub }) => handle_plugin(sub).await?,
        Some(Commands::Config { sub }) => handle_config(sub).await?,
        Some(Commands::LocalModel { sub }) => handle_local_model(sub).await?,
        Some(Commands::Auth { sub }) => handle_auth(sub).await?,
        Some(Commands::Windows { sub }) => handle_windows(sub).await?,
        Some(Commands::Macos { sub }) => handle_macos(sub).await?,
        Some(Commands::Linux { sub }) => handle_linux(sub).await?,
        Some(Commands::Mobile { sub }) => handle_mobile(sub).await?,
        Some(Commands::Pwa { output }) => handle_pwa(output).await?,
        Some(Commands::Web { address }) => {
            eprintln!("Web module is temporarily unavailable in this build");
            return Ok(());
        },
        Some(Commands::Doctor { verbose, format }) => handle_doctor(verbose, &format).await?,
        None => {
            println!("Kandil Code - Intelligent Development Platform");
            println!("Use --help for commands");
        }
    }
    Ok(())
}

async fn init_project() -> Result<()> {
    println!("Initializing new Kandil project...");

    let project_manager = ProjectManager::new()?;
    let current_dir = std::env::current_dir()?.to_string_lossy().to_string();

    // Try to get project name from directory or use a default
    let project_name = std::path::Path::new(&current_dir)
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("unnamed_project");

    let project =
        project_manager.create_project(project_name, &current_dir, "ollama", "llama3:70b")?;

    println!("Created project: {} with ID: {}", project.name, project.id);
    Ok(())
}

async fn chat(message: String) -> Result<()> {
    if message.trim().is_empty() {
        return enhanced_ui::repl::run_repl().await;
    }

    let config = Config::load()?;
    let factory = AIProviderFactory::new(config.clone());
    
    // Use prompt router to intelligently route the message
    let router = PromptRouter::new();
    let routed = router.route_message(
        &message,
        &config.ai_provider,
        &config.ai_model,
    );
    
    // Create AI instance based on routed prompt
    let ai = Arc::new(factory.create_ai(&routed.provider, &routed.model)?);
    let tracked_ai = crate::core::adapters::TrackedAI::new(ai.clone(), factory.get_cost_tracker());

    // Show routing info if it differs from default or if verbose
    if routed.provider != config.ai_provider || routed.model != config.ai_model {
        println!("🎯 Routed to {} ({}) for {:?} intent", 
            routed.provider, routed.model, routed.intent);
        println!("   {}", routed.explanation);
    }

    let response = tracked_ai.chat(&message).await?;
    println!("{}", response);

    // Save to project memory if project manager is available
    if let Ok(project_manager) = ProjectManager::new() {
        if let Ok(current_project) = project_manager.ensure_active_project(None) {
            // For this example, we'll use a simple session ID
            let session_id = uuid::Uuid::new_v4().to_string();

            // Save user message
            let _ = project_manager.save_project_memory(
                &current_project.id,
                &session_id,
                "user",
                &message,
                None, // Token count not available without proper parsing
            );

            // Save AI response
            let _ = project_manager.save_project_memory(
                &current_project.id,
                &session_id,
                "ai",
                &response,
                None, // Token count not available without proper parsing
            );
        }
    }

    Ok(())
}

async fn create_project(template: &str, name: &str) -> Result<()> {
    let engine = TemplateEngine::new();
    engine.create_project(template, name, name)?;

    // Create a project entry in the database
    let project_manager = ProjectManager::new()?;
    let project = project_manager.create_project(
        name,
        &std::env::current_dir()?.join(name).to_string_lossy(),
        "ollama",
        "llama3:70b",
    )?;

    println!("Created project '{}' using template '{}'", name, template);
    println!("Project ID: {}", project.id);
    Ok(())
}

async fn handle_agent(sub: AgentSub) -> Result<()> {
    let config = Config::load()?;
    let factory = AIProviderFactory::new(config.clone());
    let router = PromptRouter::new();
    
    // Route based on agent type
    let (intent, task_description) = match &sub {
        AgentSub::Requirements { description } => (PromptIntent::Planning, description.clone()),
        AgentSub::Design { requirements } => (PromptIntent::Architecture, requirements.clone()),
        AgentSub::Code { design_path, .. } => (PromptIntent::Coding, format!("Generate code from {}", design_path)),
        AgentSub::Test { .. } => (PromptIntent::Testing, "Generate tests".to_string()),
        AgentSub::Documentation { .. } => (PromptIntent::Analysis, "Generate documentation".to_string()),
        _ => (PromptIntent::Conversation, "Agent task".to_string()),
    };
    
    let routed = router.route_for_intent(intent, &config.ai_provider, &config.ai_model);
    let ai = Arc::new(factory.create_ai(&routed.provider, &routed.model)?);
    
    if routed.provider != config.ai_provider || routed.model != config.ai_model {
        println!("🎯 Agent using {} ({}) for {:?} intent", 
            routed.provider, routed.model, routed.intent);
    }

    match sub {
        AgentSub::Requirements { description } => {
            let requirements_agent = crate::core::agents::RequirementsAgent::new(ai);
            let _doc = requirements_agent
                .generate_requirements_document(&description)
                .await?;
            println!("Generated requirements document for: {}", description);
            // In a real implementation, we would display the structured document
            println!("Requirements document structure created (content generation would happen in full implementation)");
        }
        AgentSub::Design { requirements } => {
            let design_agent = crate::core::agents::DesignAgent::new(ai);
            let _doc = design_agent.generate_design_document(&requirements).await?;
            println!(
                "Generated design document based on requirements: {}",
                requirements
            );
            // In a real implementation, we would display the structured document
            println!("Design document structure created (content generation would happen in full implementation)");
        }
        AgentSub::Code {
            design_path,
            language,
        } => {
            // Read the design document
            let design_content = std::fs::read_to_string(&design_path)
                .unwrap_or_else(|_| "Design document content would be read from file".to_string());

            let code_agent = crate::core::agents::CodeAgent::new(ai)?;
            let output = code_agent.generate_code(&design_content, &language).await?;
            println!(
                "Generated {} code with {} files",
                language,
                output.files.len()
            );
            // In a real implementation, we would save the generated files
            println!("Code generation completed (files would be saved in full implementation)");
        }
        AgentSub::Test { sub: test_cmd } => {
            let test_agent = crate::core::agents::TestAgent::new(ai);
            match test_cmd {
                TestSubCommand::Generate { source, language } => {
                    let tests = test_agent.generate_tests(&source, &language).await?;
                    println!("Generated tests for: {}", source);
                    println!("{}", tests);
                }
                TestSubCommand::Execute { test, framework } => {
                    let results = test_agent.execute_tests(&test, &framework).await?;
                    println!("Test execution results:");
                    println!(
                        "  Passed: {}, Failed: {}, Skipped: {}",
                        results.passed, results.failed, results.skipped
                    );
                    println!("  Duration: {}ms", results.duration_ms);
                }
                TestSubCommand::Coverage { source, test } => {
                    let analysis = test_agent.analyze_test_coverage(&source, &test).await?;
                    println!("Test coverage analysis for {} and {}:", source, test);
                    println!("{}", analysis);
                }
            }
        }
        AgentSub::Documentation { sub: doc_cmd } => {
            let doc_agent = crate::core::agents::documentation::DocumentationGenerator::new(ai);
            match doc_cmd {
                DocumentationSubCommand::Generate { path } => {
                    let report = doc_agent.generate_documentation_for_project(&path).await?;
                    println!("Generated documentation for project at {}:", path);
                    println!("{}", report);
                }
            }
        }
        AgentSub::Release { sub: release_cmd } => {
            let mut release_manager =
                crate::core::agents::release_manager::ReleaseManager::new(ai, "0.1.0".to_string()); // Placeholder version
            match release_cmd {
                ReleaseSubCommand::FullProcess { version } => {
                    release_manager.version = version; // Update version from CLI arg
                    release_manager.run_full_release_process().await?;
                    println!(
                        "Full release process completed for version: {}",
                        release_manager.version
                    );
                }
            }
        }
        AgentSub::Qa { sub: qa_cmd } => {
            let mut qa_system =
                crate::core::agents::quality_assurance::QualityAssuranceSystem::new(ai);
            match qa_cmd {
                QaSubCommand::FullSuite { project_path } => {
                    let _report = qa_system.run_full_qa_suite(&project_path).await?;
                    println!("Full QA suite completed for project at {}:", project_path);
                    println!("{}", qa_system.generate_qa_report_md());
                }
            }
        }
        AgentSub::Maintenance {
            sub: maintenance_cmd,
        } => {
            let mut maintenance_manager =
                crate::core::agents::maintenance::MaintenanceManager::new(ai);
            match maintenance_cmd {
                MaintenanceSubCommand::HealthCheck { system_name } => {
                    maintenance_manager.run_health_checks(&system_name).await?;
                    println!("Health check completed for system: {}", system_name);
                }
            }
        }
        AgentSub::Simulate { sub: sim_cmd } => match sim_cmd {
            SimulateSubCommand::Pm { sub: pm_cmd } => {
                let pm_sim = crate::core::agents::ProjectManagerSimulation::new(ai);
                match pm_cmd {
                    PmSubCommand::PlanSprint { project, duration } => {
                        let plan = pm_sim.plan_sprint(&project, duration).await?;
                        println!("Sprint plan for project '{}':", project);
                        println!(
                            "  Sprint: {}, Duration: {} weeks",
                            plan.sprint_number, duration
                        );
                        println!("  Goals: {} items", plan.goals.len());
                        println!("  Timeline: {} to {}", plan.start_date, plan.end_date);
                    }
                    PmSubCommand::Retrospective { sprint } => {
                        let results = pm_sim.run_retrospective(sprint).await?;
                        println!("Retrospective results for Sprint {}:", sprint);
                        println!("{}", results);
                    }
                }
            }
            SimulateSubCommand::Ba { sub: ba_cmd } => {
                let ba_sim = crate::core::agents::BusinessAnalystSimulation::new(ai);
                match ba_cmd {
                    BaSubCommand::Validate { requirements } => {
                        let validation = ba_sim.validate_requirements(&requirements).await?;
                        println!("Requirements validation for: {}", requirements);
                        println!("{}", validation);
                    }
                    BaSubCommand::UserStory { feature } => {
                        let story = ba_sim.create_user_story(&feature).await?;
                        println!("Created user story for feature: {}", feature);
                        println!("  ID: {}, Title: {}", story.id, story.title);
                        println!("  Priority: {:?}", story.priority);
                        println!("  Story Points: {}", story.story_points);
                    }
                }
            }
        },
        AgentSub::Advanced { sub: advanced_cmd } => {
            match advanced_cmd {
                AdvancedSubCommand::Review { file } => {
                    let review_agent = crate::core::agents::ReviewAgent::new(ai);
                    let report = review_agent.code_review(&file).await?;
                    println!("Code review for: {}", file);
                    println!("  Score: {}/100", report.score);
                    println!("  Issues found: {}", report.issues.len());
                    println!("  Summary: {}", report.summary);
                }
                AdvancedSubCommand::Security { file, description } => {
                    let security_agent = crate::core::agents::EthicsSecurityAgent::new(ai);
                    let report = security_agent
                        .security_scan(&std::fs::read_to_string(&file)?, &file)
                        .await?;
                    println!("Security scan for: {}", file);
                    println!("  Risk Score: {}/100", report.risk_score);
                    println!("  Vulnerabilities: {}", report.vulnerabilities.len());

                    // Also run ethics check
                    let ethics_report = security_agent
                        .ethics_check(&std::fs::read_to_string(&file)?, &description)
                        .await?;
                    println!(
                        "Ethics check completed with score: {}/100",
                        ethics_report.ethics_score
                    );
                }
                AdvancedSubCommand::Deploy { sub: deploy_cmd } => {
                    let deploy_agent = crate::core::agents::DeploymentAgent::new(ai)?;
                    match deploy_cmd {
                        DeploySubCommand::Plan { environment, app } => {
                            let plan = deploy_agent
                                .create_deployment_plan(&environment, &app)
                                .await?;
                            println!("Deployment plan for {} to {}:", app, environment);
                            println!("  Steps: {}", plan.steps.len());
                            println!("  Estimated duration: {}", plan.estimated_duration);
                            println!(
                                "  Rollback plan: {}",
                                if plan.rollback_plan.steps.is_empty() {
                                    "No"
                                } else {
                                    "Yes"
                                }
                            );
                        }
                        DeploySubCommand::Execute { plan } => {
                            // In a real implementation, this would load and execute the plan
                            println!("Executing deployment plan from: {}", plan);
                            println!("Deployment execution would happen in full implementation");
                        }
                    }
                }
                AdvancedSubCommand::SelfImprove { path } => {
                    let meta_agent = crate::core::agents::MetaAgent::new(ai);
                    let analysis = meta_agent.analyze_system(&path).await?;
                    println!("System analysis for: {}", path);
                    println!(
                        "  Performance issues: {}",
                        analysis.performance_bottlenecks.len()
                    );
                    println!(
                        "  Code quality issues: {}",
                        analysis.code_quality_issues.len()
                    );
                    println!("  Security concerns: {}", analysis.security_concerns.len());

                    let improvement_plans = meta_agent.generate_improvement_plan(&analysis).await?;
                    println!("  Suggested improvements: {}", improvement_plans.len());

                    // Also run the self-evolution capability
                    let evolution_result = meta_agent.evolve_agent_capabilities().await?;
                    println!("  Self-improvement analysis: {}", evolution_result);
                }
            }
        }
        AgentSub::TechRole { sub: tech_role_cmd } => {
            match tech_role_cmd {
                TechRoleSubCommand::Architect { sub: arch_cmd } => {
                    let mut arch_agent = crate::core::agents::ArchitectSimulation::new(ai)?;
                    match arch_cmd {
                        ArchitectSubCommand::Review { diagram } => {
                            let review = arch_agent.review_design(&diagram).await?;
                            println!("Architecture review completed:");
                            println!("  Score: {}/100", review.score);
                            println!("  Recommendations: {}", review.recommendations.len());
                            println!("  Issues found: {}", review.identified_issues.len());
                        }
                        ArchitectSubCommand::Decide { context, decision } => {
                            let arch_decision = arch_agent
                                .make_architecture_decision(&context, &decision)
                                .await?;
                            println!("Architecture decision made:");
                            println!("  ID: {}, Title: {}", arch_decision.id, arch_decision.title);
                            println!("  Status: {:?}", arch_decision.status);
                        }
                        ArchitectSubCommand::Adr { decision_id } => {
                            let adr = arch_agent.generate_adr(&decision_id).await?;
                            println!("Architecture Decision Record for {}: ", decision_id);
                            println!("{}", adr);
                        }
                    }
                }
                TechRoleSubCommand::Developer { sub: dev_cmd } => {
                    let mut dev_agent = crate::core::agents::DeveloperSimulation::new(
                        ai,
                        "Current Project".to_string(),
                    );
                    match dev_cmd {
                        DeveloperSubCommand::Implement { spec, file } => {
                            let implementation = dev_agent.implement_feature(&spec, &file).await?;
                            println!("Feature implementation completed for: {}", file);
                            println!("{}", implementation);
                        }
                        DeveloperSubCommand::Pair {
                            partner,
                            task,
                            file,
                        } => {
                            let session_id = dev_agent
                                .start_pair_programming(&partner, &task, &file)
                                .await?;
                            println!("Started pair programming session: {}", session_id);
                        }
                        DeveloperSubCommand::Bugs { code, file } => {
                            let bugs = dev_agent.find_bugs(&code, &file).await?;
                            println!("Bugs found in {}: {}", file, bugs.len());
                            for bug in bugs {
                                println!("  - {} ({}): {}", bug.id, bug.severity, bug.description);
                            }
                        }
                    }
                }

                TechRoleSubCommand::Collaborate { sub: collab_cmd } => {
                    let mut collab_manager = crate::core::agents::CollaborationManager::new();
                    match collab_cmd {
                        CollaborateSubCommand::Session {
                            title,
                            roles,
                            agenda,
                        } => {
                            // Parse roles
                            let role_list: Vec<crate::core::agents::collaboration::Role> = roles
                                .split(',')
                                .map(|r| match r.trim() {
                                    "architect" => {
                                        crate::core::agents::collaboration::Role::Architect
                                    }
                                    "developer" => {
                                        crate::core::agents::collaboration::Role::Developer
                                    }
                                    "qa" => crate::core::agents::collaboration::Role::QA,
                                    _ => crate::core::agents::collaboration::Role::Developer, // default
                                })
                                .collect();

                            let session_id = collab_manager
                                .start_collaboration_session(&title, role_list, agenda)
                                .await;
                            println!("Collaboration session started: {}", session_id);
                        }
                        CollaborateSubCommand::Decision {
                            title,
                            description,
                            roles,
                        } => {
                            // Parse roles
                            let role_list: Vec<crate::core::agents::collaboration::Role> = roles
                                .split(',')
                                .map(|r| match r.trim() {
                                    "architect" => {
                                        crate::core::agents::collaboration::Role::Architect
                                    }
                                    "developer" => {
                                        crate::core::agents::collaboration::Role::Developer
                                    }
                                    "qa" => crate::core::agents::collaboration::Role::QA,
                                    _ => crate::core::agents::collaboration::Role::Developer, // default
                                })
                                .collect();

                            // For this simulation, we'll create a basic cross-role decision
                            // without full agent interaction
                            println!("Creating cross-role decision: {}", title);
                            println!("Roles involved: {:?}", role_list);
                            println!("Description: {}", description);
                        }
                    }
                }
            }
        }
        AgentSub::AdvancedFeatures {
            sub: advanced_features_cmd,
        } => {
            match advanced_features_cmd {
                AdvancedFeaturesSubCommand::DevOps { sub: devops_cmd } => {
                    let devops_agent = crate::core::agents::DevOpsSimulation::new(ai);
                    match devops_cmd {
                        DevOpsSubCommand::Terraform { spec } => {
                            let tf_path = devops_agent.generate_terraform(&spec).await?;
                            println!("Generated Terraform configuration: {:?}", tf_path);
                        }
                        DevOpsSubCommand::Drill { scenario } => {
                            let report = devops_agent.run_drill(&scenario).await?;
                            println!("Incident response drill completed:");
                            println!("  Scenario: {}", report.scenario);
                            println!("  Duration: {} seconds", report.duration_seconds);
                            println!("  Effectiveness: {}/100", report.effectiveness_score);
                        }
                        DevOpsSubCommand::Pipeline { project_type } => {
                            let pipeline =
                                devops_agent.generate_ci_cd_pipeline(&project_type).await?;
                            println!("Generated CI/CD pipeline for {} projects", project_type);
                            println!("{}", pipeline);
                        }
                    }
                }
                AdvancedFeaturesSubCommand::Scrum { sub: scrum_cmd } => {
                    let mut scrum_agent = crate::core::agents::ScrumSimulation::new(ai);
                    match scrum_cmd {
                        ScrumSubCommand::Plan {
                            goal,
                            duration,
                            team_size,
                        } => {
                            let sprint = scrum_agent.plan_sprint(goal, duration, team_size).await?;
                            println!("Sprint {} planned:", sprint.number);
                            println!("  Goal: {}", sprint.goal);
                            println!("  Duration: {} days", sprint.duration_days);
                            println!("  Team size: {}", sprint.team_size);
                        }
                        ScrumSubCommand::Retro { sprint } => {
                            let retro = scrum_agent.run_retrospective(sprint).await?;
                            println!("Sprint {} retrospective:", retro.sprint_number);
                            println!("  Satisfaction: {}/10", retro.satisfaction_score);
                            println!("  Good things: {}", retro.good_things.len());
                            println!("  Improvement areas: {}", retro.improvement_areas.len());
                        }
                        ScrumSubCommand::Ceremony {
                            ceremony_type,
                            participants,
                        } => {
                            let participants_list = participants
                                .split(',')
                                .map(|s| s.trim().to_string())
                                .collect();
                            let ceremony = scrum_agent
                                .conduct_ceremony(
                                    &ceremony_type,
                                    participants_list,
                                    scrum_agent.get_current_sprint(),
                                )
                                .await?;
                            println!(
                                "Conducted {} ceremony with {} participants",
                                ceremony.name,
                                ceremony.participants.len()
                            );
                        }
                    }
                }
                AdvancedFeaturesSubCommand::I18n { sub: i18n_cmd } => {
                    let mut i18n_agent = crate::core::agents::I18nAssistant::new(ai);
                    match i18n_cmd {
                        I18nSubCommand::Translate {
                            text,
                            target,
                            source,
                        } => {
                            let translation =
                                i18n_agent.translate_text(&text, &target, &source).await?;
                            println!("Translation from {} to {}:", source, target);
                            println!("{}", translation);
                        }
                        I18nSubCommand::Audit { path } => {
                            let report = i18n_agent.audit_translations(&path).await?;
                            println!("Translation audit completed:");
                            println!("  Languages: {}", report.completeness_by_language.len());
                            println!("  Recommendations: {}", report.recommendations.len());
                        }
                        I18nSubCommand::Review {
                            original,
                            translation,
                            target,
                        } => {
                            let report = i18n_agent
                                .review_translation(&original, &translation, &target)
                                .await?;
                            println!("Translation review completed:");
                            println!("  Quality score: {}/100", report.quality_score);
                            println!("  Issues found: {}", report.issues_found.len());
                        }
                    }
                }
                AdvancedFeaturesSubCommand::A11y { sub: a11y_cmd } => {
                    let a11y_agent = crate::core::agents::A11yAssistant::new(ai);
                    match a11y_cmd {
                        A11ySubCommand::Audit { content } => {
                            // Default to WCAG AA level
                            use crate::core::agents::a11y::WcagLevel;
                            let report = a11y_agent.wcag_audit(&content, WcagLevel::AA).await?;
                            println!("Accessibility audit completed:");
                            println!("  Score: {}/100", report.accessibility_score);
                            println!("  Issues found: {}", report.issues_found.len());
                        }
                        A11ySubCommand::Guidelines { component } => {
                            let guidelines =
                                a11y_agent.generate_a11y_guidelines(&component).await?;
                            println!("Accessibility guidelines for {}:", component);
                            println!("{}", guidelines);
                        }
                        A11ySubCommand::Fix { html } => {
                            let fixed_html = a11y_agent.remediate_issues(&html).await?;
                            println!("Accessibility issues remediated in HTML:");
                            println!("{}", fixed_html);
                        }
                    }
                }
                AdvancedFeaturesSubCommand::Collab { sub: collab_cmd } => {
                    let mut collab = crate::core::agents::RealTimeCollaboration::new();
                    match collab_cmd {
                        CollabSubCommand::Session {
                            name,
                            creator_id,
                            creator_name,
                        } => {
                            let session_id =
                                collab.create_session(&name, &creator_id, &creator_name)?;
                            println!("Created collaboration session: {}", session_id);
                        }
                        CollabSubCommand::AddParticipant {
                            session_id,
                            user_id,
                            name,
                            role,
                        } => {
                            use crate::core::agents::collaboration_realtime::Role;
                            let role_enum = match role.as_str() {
                                "owner" => Role::Owner,
                                "admin" => Role::Admin,
                                "editor" => Role::Editor,
                                "viewer" => Role::Viewer,
                                _ => Role::Editor,
                            };
                            collab.add_participant(&session_id, &user_id, &name, role_enum)?;
                            println!("Added participant {} to session {}", name, session_id);
                        }
                        CollabSubCommand::AddDoc {
                            session_id,
                            doc_id,
                            name,
                            content,
                            language,
                        } => {
                            collab.add_document(
                                &session_id,
                                &doc_id,
                                &name,
                                &content,
                                &language,
                            )?;
                            println!("Added document {} to session {}", name, session_id);
                        }
                    }
                }
                AdvancedFeaturesSubCommand::Ide { sub: ide_cmd } => {
                    let ide_ext = crate::core::agents::IdeExtension::new(ai);
                    match ide_cmd {
                        IdeSubCommand::Suggestions {
                            file,
                            language,
                            code,
                        } => {
                            let ctx = crate::core::agents::ide_extension::ExtensionContext {
                                file_path: file,
                                language,
                                selected_code: code,
                                cursor_position: (1, 1),
                                workspace_root: ".".to_string(),
                            };
                            let suggestions = ide_ext.get_code_suggestions(&ctx).await?;
                            println!("Code suggestions ({} found):", suggestions.len());
                            for suggestion in suggestions {
                                println!("  - {}: {}", suggestion.title, suggestion.description);
                            }
                        }
                        IdeSubCommand::Docs { code, language } => {
                            let docs = ide_ext.generate_documentation(&code, &language).await?;
                            println!("Generated documentation:");
                            println!("{}", docs);
                        }
                        IdeSubCommand::Refactor { code, language } => {
                            let options = ide_ext.get_refactoring_options(&code, &language).await?;
                            println!("Refactoring options:");
                            for option in options {
                                println!("{}", option);
                            }
                        }
                        IdeSubCommand::Review { code, language } => {
                            let comments = ide_ext.run_inline_code_review(&code, &language).await?;
                            println!("Inline code review comments ({} found):", comments.len());
                            for comment in comments {
                                println!(
                                    "  Line {}: [{}] {}",
                                    comment.line_number, comment.severity, comment.message
                                );
                            }
                        }
                    }
                }
            }
        }
    }
    Ok(())
}

async fn handle_projects(sub: ProjectSub) -> Result<()> {
    let project_manager = ProjectManager::new()?;

    match sub {
        ProjectSub::List => {
            let projects = project_manager.list_projects()?;
            if projects.is_empty() {
                println!("No projects found");
            } else {
                println!("Projects:");
                for project in projects {
                    let last_opened = project
                        .last_opened
                        .map(|d| d.format("%Y-%m-%d %H:%M:%S").to_string())
                        .unwrap_or_else(|| "Never".to_string());
                    println!(
                        "  ID: {} | Name: {} | Path: {} | Last Opened: {}",
                        project.id, project.name, project.root_path, last_opened
                    );
                }
            }
        }
        ProjectSub::Switch { id } => {
            let project = project_manager.switch_project(&id)?;
            println!(
                "Switched to project: {} at {}",
                project.name, project.root_path
            );
        }
        ProjectSub::Sync { id } => {
            // For now, just show that sync would happen
            // In a real implementation, we would use the CloudSync module
            match id {
                Some(project_id) => println!("Syncing project: {}", project_id),
                None => println!("Syncing current project"),
            }
            println!("Note: Cloud sync functionality would be implemented with Supabase in a full implementation");
        }
        ProjectSub::Info { id } => {
            let project = if let Some(project_id) = id {
                project_manager.get_project(&project_id)?
            } else {
                // Get current project (for now, just get the most recently used)
                let projects = project_manager.list_projects()?;
                projects.first().cloned()
            };

            match project {
                Some(p) => {
                    println!("Project Details:");
                    println!("  ID: {}", p.id);
                    println!("  Name: {}", p.name);
                    println!("  Path: {}", p.root_path);
                    println!("  AI Provider: {}", p.ai_provider);
                    println!("  AI Model: {}", p.ai_model);
                    println!("  Memory Enabled: {}", p.memory_enabled);
                    println!("  Created: {}", p.created_at.format("%Y-%m-%d %H:%M:%S"));
                    println!(
                        "  Last Opened: {}",
                        p.last_opened
                            .map(|d| d.format("%Y-%m-%d %H:%M:%S").to_string())
                            .unwrap_or_else(|| "Never".to_string())
                    );
                }
                None => println!("Project not found"),
            }
        }
    }
    Ok(())
}

#[cfg(feature = "tui")]
async fn launch_tui() -> Result<()> {
    let project_manager = ProjectManager::new()?;
    let _current_project = project_manager.ensure_active_project(None)?;
    let mut app = crate::tui::StudioApp::new()?;
    app.run().await?;
    Ok(())
}

#[cfg(not(feature = "tui"))]
async fn launch_tui() -> Result<()> {
    Err(anyhow::anyhow!("TUI feature is not enabled in this build"))
}

async fn handle_refactor(sub: RefactorSub) -> Result<()> {
    let mut engine = RefactorEngine::new();
    match sub {
        RefactorSub::Preview {
            file,
            refactor_type,
            params,
        } => {
            // Parse params - in a real implementation, this would be more sophisticated
            let mut refactor_params = RefactorParams {
                old_name: None,
                new_name: None,
                start_line: None,
                end_line: None,
                function_name: None,
                visibility: None,
            };

            // Simple parameter parsing for demo purposes
            for param in params {
                if let Some((key, value)) = param.split_once('=') {
                    match key {
                        "old_name" => refactor_params.old_name = Some(value.to_string()),
                        "new_name" => refactor_params.new_name = Some(value.to_string()),
                        _ => {}
                    }
                }
            }

            let result = engine.preview_refactor(&file, &refactor_type, &refactor_params)?;
            println!("Refactoring preview for '{}':", file);
            println!("{}", result);
        }
        RefactorSub::Apply => {
            engine.apply_pending_operations()?;
            println!("Applied all pending refactor operations");
        }
        RefactorSub::Cancel => {
            engine.cancel_pending_operations();
            println!("Cancelled all pending refactor operations");
        }
    }
    Ok(())
}

async fn handle_test(sub: TestSub) -> Result<()> {
    let config = Config::load()?;
    let factory = AIProviderFactory::new(config.clone());
    let ai = Arc::new(factory.create_ai(&config.ai_provider, &config.ai_model)?);
    let tracked_ai = crate::core::adapters::TrackedAI::new(ai.clone(), factory.get_cost_tracker());
    let generator = TestGenerator::new(ai.clone()); // Using the underlying AI for now

    match sub {
        TestSub::Generate { file, framework } => {
            let tests = generator.generate_tests_for_file(&file, &framework).await?;
            println!("Generated tests for '{}':", file);
            println!("{}", tests);
        }
        TestSub::Integration { feature } => {
            let tests = generator.generate_integration_tests(&feature).await?;
            println!("Generated integration tests for feature:");
            println!("{}", tests);
        }
        TestSub::Coverage { source, test } => {
            let analysis = generator.analyze_test_coverage(&source, &test).await?;
            println!("Test coverage analysis for '{}' and '{}':", source, test);
            println!("{}", analysis);
        }
    }
    Ok(())
}

async fn switch_model(provider: String, model: String) -> Result<()> {
    // In a real implementation, this would update the configuration
    // For now, we'll just verify the provider is valid
    match provider.as_str() {
        "ollama" | "claude" | "qwen" | "openai" => {
            println!("Switched to provider: {}, model: {}", provider, model);
        }
        _ => return Err(anyhow::anyhow!("Invalid provider: {}", provider)),
    }
    Ok(())
}

async fn handle_plugin(sub: PluginSub) -> Result<()> {
    let manager = PluginManager::new();
    match sub {
        PluginSub::Install { source } => {
            manager.install_plugin(&source)?;
            println!("Plugin installed from: {}", source);
        }
        PluginSub::List => {
            let plugins = manager.list_plugins()?;
            if plugins.is_empty() {
                println!("No plugins installed");
            } else {
                println!("Installed plugins:");
                for plugin in plugins {
                    println!("  - {}", plugin);
                }
            }
        }
        PluginSub::Run { name, args } => {
            // In a real implementation, we'd look up the actual plugin path
            println!("Running plugin: {} with args: {:?}", name, args);
            // This would execute the plugin with proper sandboxing
        }
    }
    Ok(())
}

async fn handle_config(sub: ConfigSub) -> Result<()> {
    match sub {
        ConfigSub::SetKey { provider, key } => {
            SecureKey::save(&provider, &key)?;
            println!("API key saved securely for provider: {}", provider);
        }
        ConfigSub::ListKeys => {
            println!("Currently implemented as a placeholder - key listing will be implemented in future versions");
            // Note: Actual key listing may not be implemented for security reasons
        }
        ConfigSub::Costs { provider } => {
            // For now, showing a placeholder - in a real implementation we would access the cost tracker
            match provider {
                Some(provider_str) => {
                    println!(
                        "Cost tracking for provider: {} is not available in this context",
                        provider_str
                    );
                    println!("Cost tracking is available when using AI features");
                }
                None => {
                    println!("Cost tracking summary is not available in this context");
                    println!("Cost tracking is available when using AI features");
                }
            }
        }
        ConfigSub::Validate => {
            let cfg = Config::load()?;
            match cfg.validate_production().await {
                Ok(()) => println!("Configuration validation: ok"),
                Err(e) => {
                    eprintln!("Configuration validation failed: {}", e);
                    return Err(e);
                }
            }
        }
    }
    Ok(())
}

async fn handle_local_model(sub: LocalModelSub) -> Result<()> {
    match sub {
        LocalModelSub::List { compatible } => {
            let hardware = detect_hardware();
            let catalog = &crate::models::catalog::MODEL_CATALOG;

            println!("Available Models:");
            for model in catalog.iter() {
                // Check compatibility if requested
                if compatible && model.ram_required_gb > hardware.total_ram_gb {
                    continue;
                }

                println!("  {}", model.name);
                println!(
                    "    Size: {}GB, RAM: {}GB, GPU: {:?}GB",
                    model.size_gb, model.ram_required_gb, model.gpu_vram_min
                );

                // Show speed in a user-friendly way
                let speed_str = match &model.speed_rating {
                    crate::models::catalog::Speed::UltraFast(tps) => {
                        format!("Ultra Fast ({} t/s)", tps)
                    }
                    crate::models::catalog::Speed::VeryFast(tps) => {
                        format!("Very Fast ({} t/s)", tps)
                    }
                    crate::models::catalog::Speed::Fast(tps) => format!("Fast ({} t/s)", tps),
                    crate::models::catalog::Speed::Medium(tps) => format!("Medium ({} t/s)", tps),
                    crate::models::catalog::Speed::Slow(tps) => format!("Slow ({} t/s)", tps),
                };

                let quality_str = format!("{:?}", model.quality_rating);
                println!("    Speed: {}, Quality: {}", speed_str, quality_str);
                println!("    {}", model.description);
                println!("    Context sizes: {:?}", model.context_sizes);
            }
        }
        LocalModelSub::Install { model, force } => {
            let model_spec = crate::models::catalog::MODEL_CATALOG
                .iter()
                .find(|m| m.name == model)
                .ok_or_else(|| anyhow::anyhow!("Unknown model: {}", model))?;

            let hardware = detect_hardware();
            if !force && model_spec.ram_required_gb > hardware.total_ram_gb {
                anyhow::bail!(
                    "Insufficient RAM. Model requires {}GB, you have {}GB. Use --force to override.",
                    model_spec.ram_required_gb,
                    hardware.total_ram_gb
                );
            }

            let path = get_model_path(&model_spec.filename).await?;
            if path.exists() {
                println!("Model already installed at {:?}", path);
                return Ok(());
            }

            // Create directory if it doesn't exist
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).await?;
            }

            download_model(model_spec, &path).await?;
            // Note: Verification would require SHA256 which isn't in the model spec

            println!("✅ Model {} installed successfully", model);
        }
        LocalModelSub::Remove { model } => {
            let model_spec = crate::models::catalog::MODEL_CATALOG
                .iter()
                .find(|m| m.name == model)
                .ok_or_else(|| anyhow::anyhow!("Unknown model: {}", model))?;

            let path = get_model_path(&model_spec.filename).await?;
            if path.exists() {
                fs::remove_file(&path).await?;
                println!("✅ Model {} removed successfully", model);
            } else {
                println!("Model {} not found at {:?}", model, path);
            }
        }
        LocalModelSub::Verify { model } => {
            let model_spec = crate::models::catalog::MODEL_CATALOG
                .iter()
                .find(|m| m.name == model)
                .ok_or_else(|| anyhow::anyhow!("Unknown model: {}", model))?;

            let path = get_model_path(&model_spec.filename).await?;
            if path.exists() {
                println!("✅ Model {} found and verified at {:?}", model, path);
            } else {
                println!("❌ Model {} not found at {:?}", model, path);
            }
        }
        LocalModelSub::Benchmark {
            model,
            format,
            runtime,
            all_runtimes,
        } => {
            let opts = BenchmarkCliOptions {
                model,
                format,
                runtime,
                all_runtimes,
            };
            benchmark_model(opts).await?;
        }
        LocalModelSub::Use { model } => {
            // This would update the user's config file to set this as default
            println!("Setting {} as the default model", model);

            // In a real implementation, this would update the config file
            // For now, just print a message
            println!("Note: This would normally update the default model in your config file.");
        }
        LocalModelSub::Status => {
            let hardware = detect_hardware();
            let catalog = &crate::models::catalog::MODEL_CATALOG;

            println!("Hardware Profile:");
            println!(
                "  RAM: {}GB total, {}GB available",
                hardware.total_ram_gb, hardware.available_ram_gb
            );
            println!(
                "  CPU: {} physical cores, {} logical cores",
                hardware.cpu_physical_cores, hardware.cpu_logical_cores
            );
            println!("  Disk free: {}GB", hardware.free_disk_gb);
            if let Some(gpu) = &hardware.gpu {
                println!(
                    "  GPU: {} {} with {}GB memory",
                    gpu.brand, gpu.model, gpu.memory_gb
                );
            } else {
                println!("  GPU: None detected");
            }
            println!("  Platform: {:?}\n", hardware.platform);

            let models_dir = models_root().await?;

            let mut installed_count = 0usize;
            for model in catalog.iter() {
                let path = models_dir.join(&model.filename);
                if fs::metadata(&path).await.is_ok() {
                    installed_count += 1;
                }
            }
            println!("Installed models: {}", installed_count);

            match hardware.platform {
                PlatformKind::Windows | PlatformKind::WindowsWsl => {
                    let status = windows::WindowsAdapterStatus::gather().await;
                    println!(
                        "WSL IP: {}",
                        status.wsl_ip.unwrap_or_else(|| "not detected".to_string())
                    );
                    println!("  Ollama (WSL2): {}", bool_icon(status.ollama_reachable));
                    println!("  LM Studio API: {}", bool_icon(status.lmstudio_reachable));
                    println!("  GPT4All API: {}", bool_icon(status.gpt4all_reachable));
                    println!(
                        "  Foundry Local API: {}",
                        bool_icon(status.foundry_reachable)
                    );
                    let gpu_report = windows::check_wsl_gpu();
                    println!("  GPU passthrough: {}", gpu_report.message);
                }
                PlatformKind::MacOs => {
                    let status = macos::CoremlRuntimeStatus::detect();
                    println!(
                        "Core ML tools installed: {}",
                        bool_icon(status.coremltools_available)
                    );
                    println!(
                        "Apple Neural Engine available: {}",
                        bool_icon(status.ane_available)
                    );
                    for note in status.notes {
                        println!("  - {}", note);
                    }
                }
                PlatformKind::Linux => {
                    let status = linux::LinuxRuntimeStatus::detect();
                    println!("Ollama socket present: {}", bool_icon(status.ollama_socket));
                    println!("CUDA detected: {}", bool_icon(status.cuda_available));
                    for note in status.notes {
                        println!("  - {}", note);
                    }
                }
                PlatformKind::Unknown => {
                    println!("Platform-specific runtime diagnostics unavailable.");
                }
            }
        }
    }
    Ok(())
}

async fn models_root() -> Result<std::path::PathBuf> {
    let path = dirs::data_dir()
        .unwrap_or_else(|| std::env::current_dir().unwrap())
        .join("kandil")
        .join("models");
    fs::create_dir_all(&path).await?;
    Ok(path)
}

fn bool_icon(value: bool) -> &'static str {
    if value {
        ""
    } else {
        "⚠️"
    }
}

async fn get_model_path(filename: &str) -> Result<std::path::PathBuf> {
    Ok(models_root().await?.join(filename))
}

async fn download_model(
    model: &crate::models::catalog::ModelSpec,
    path: &std::path::PathBuf,
) -> Result<()> {
    // Construct the Hugging Face download URL
    let url = format!(
        "https://huggingface.co/{}/resolve/main/{}",
        model.huggingface_repo, model.filename
    );

    let client = reqwest::Client::new();
    let response = client.get(&url).send().await?;
    let total_size = response.content_length().unwrap_or(0);

    let pb = indicatif::ProgressBar::new(total_size);
    pb.set_style(indicatif::ProgressStyle::default_bar()
        .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")
        .unwrap());

    let mut file = tokio::io::BufWriter::new(fs::File::create(path).await?);
    let mut stream = response.bytes_stream();

    while let Some(chunk) = stream.next().await {
        let chunk = chunk?;
        pb.inc(chunk.len() as u64);
        tokio::io::copy(&mut &chunk[..], &mut file).await?;
    }

    pb.finish_with_message("Download complete");
    file.flush().await?;
    Ok(())
}

async fn benchmark_model(opts: BenchmarkCliOptions) -> Result<()> {
    let cfg = Config::load()?;
    let model = opts.model.clone().unwrap_or_else(|| cfg.ai_model.clone());

    let suite = CrossPlatformBenchmark::new();

    let report = suite
        .run(BenchmarkOptions {
            model: model.clone(),
            default_provider: cfg.ai_provider.clone(),
            runtime: opts.runtime.clone(),
            include_all_runtimes: opts.all_runtimes,
            prompts: None,
        })
        .await?;

    match opts.format.to_lowercase().as_str() {
        "json" => {
            println!("{}", serde_json::to_string_pretty(&report)?);
        }
        _ => {
            println!("📊 Cross-Platform Benchmark");
            println!("Model: {}", report.model);
            println!(
                "Hardware: {}GB RAM ({}GB available) • Disk free: {}GB • Platform: {:?}",
                report.hardware.total_ram_gb,
                report.hardware.available_ram_gb,
                report.hardware.free_disk_gb,
                report.hardware.platform
            );
            if let Some(gpu) = &report.hardware.gpu {
                println!("GPU: {} {} ({}GB)", gpu.brand, gpu.model, gpu.memory_gb);
            } else {
                println!("GPU: not detected");
            }
            println!("Prompts: {}", report.prompts.len());
            println!("Timestamp: {}", report.timestamp);

            for runtime in &report.results {
                println!("\nRuntime: {}", runtime.runtime);
                println!("  Provider: {}", runtime.provider);
                println!("  Avg latency: {} ms", runtime.average_latency_ms);
                println!(
                    "  Avg throughput: {} tokens/s",
                    runtime.average_tokens_per_sec
                );
                println!("  Memory peak: {} MB", runtime.memory_peak_mb);
                if let Some(impact) = runtime.battery_impact {
                    println!("  Battery impact: {:.1}%/min", impact);
                }
                println!("  Samples:");
                for sample in &runtime.samples {
                    println!(
                        "{}{} ms, {} tokens",
                        sample.prompt, sample.latency_ms, sample.output_tokens
                    );
                }
            }

            if !report.warnings.is_empty() {
                println!("\nWarnings:");
                for warning in &report.warnings {
                    println!("  - {}", warning);
                }
            }
        }
    }

    Ok(())
}

async fn handle_auth(sub: AuthSub) -> Result<()> {
    match sub {
        AuthSub::Login { provider } => {
            println!("Enter API key for {}:", provider);
            let mut buf = String::new();
            use std::io::Read;
            let mut stdin = std::io::stdin();
            stdin.read_to_string(&mut buf)?;
            let key = buf.trim().to_string();
            if key.is_empty() {
                return Err(anyhow::anyhow!("Empty API key"));
            }
            SecureKey::save(&provider, &key)?;
            println!("API key saved for {}", provider);
        }
    }
    Ok(())
}

async fn handle_pwa(output: PathBuf) -> Result<()> {
    pwa::write_assets(&output)?;
    println!("📦 PWA assets exported to {}", output.display());
    Ok(())
}

async fn handle_windows(sub: WindowsSub) -> Result<()> {
    match sub {
        WindowsSub::Status => {
            let status = windows::WindowsAdapterStatus::gather().await;
            println!(
                "WSL IP: {}",
                status.wsl_ip.unwrap_or_else(|| "not detected".to_string())
            );
            println!(
                "Ollama (WSL2) reachable: {}",
                if status.ollama_reachable {
                    ""
                } else {
                    "⚠️"
                }
            );
            println!(
                "LM Studio API reachable: {}",
                if status.lmstudio_reachable {
                    ""
                } else {
                    "⚠️"
                }
            );
            println!(
                "GPT4All API reachable: {}",
                if status.gpt4all_reachable {
                    ""
                } else {
                    "⚠️"
                }
            );
            println!(
                "Foundry Local reachable: {}",
                if status.foundry_reachable {
                    ""
                } else {
                    "⚠️"
                }
            );
        }
        WindowsSub::CheckGpu => {
            let report = windows::check_wsl_gpu();
            println!("{}", report.message);
        }
        WindowsSub::Doctor => {
            // Run comprehensive diagnostics
            use crate::benchmark::CrossPlatformBenchmark;
            let benchmark = CrossPlatformBenchmark::new();
            let report = benchmark.run_diagnostics().await;

            println!("🏥 Windows System Diagnostic Report:");
            println!("Platform: {:?}", report.hardware.platform);
            println!("Hardware: {}GB RAM, {} CPU cores",
                     report.hardware.total_ram_gb, report.hardware.cpu_logical_cores);

            if let Some(gpu) = &report.hardware.gpu {
                println!("GPU: {} {} with {}GB memory",
                         gpu.brand, gpu.model, gpu.memory_gb);
            }

            let reachable_count = report.connectivity.endpoints.iter().filter(|e| e.reachable).count();
            println!("Connectivity: {}/{} endpoints reachable",
                     reachable_count, report.connectivity.endpoints.len());

            for endpoint in &report.connectivity.endpoints {
                let status = if endpoint.reachable { "" } else { "" };
                println!("  {} {}: {}", status, endpoint.name, endpoint.endpoint);
            }
        }
        WindowsSub::SetupWsl2 => {
            println!("{}", windows::setup_wsl2_instructions());
        }
    }
    Ok(())
}

async fn handle_macos(sub: MacosSub) -> Result<()> {
    match sub {
        MacosSub::Status => {
            let status = macos::CoremlRuntimeStatus::detect();
            println!(
                "Core ML tools installed: {}",
                bool_icon(status.coremltools_available)
            );
            println!(
                "Apple Neural Engine available: {}",
                bool_icon(status.ane_available)
            );
            for note in status.notes {
                println!("  - {}", note);
            }
        }
        MacosSub::Doctor => {
            // Run comprehensive diagnostics
            use crate::benchmark::CrossPlatformBenchmark;
            let benchmark = CrossPlatformBenchmark::new();
            let report = benchmark.run_diagnostics().await;

            println!("🏥 macOS System Diagnostic Report:");
            println!("Platform: {:?}", report.hardware.platform);
            println!("Hardware: {}GB RAM, {} CPU cores",
                     report.hardware.total_ram_gb, report.hardware.cpu_logical_cores);

            if let Some(gpu) = &report.hardware.gpu {
                println!("GPU: {} {} with {}GB memory",
                         gpu.brand, gpu.model, gpu.memory_gb);
            }

            let reachable_count = report.connectivity.endpoints.iter().filter(|e| e.reachable).count();
            println!("Connectivity: {}/{} endpoints reachable",
                     reachable_count, report.connectivity.endpoints.len());

            for endpoint in &report.connectivity.endpoints {
                let status = if endpoint.reachable { "" } else { "" };
                println!("  {} {}: {}", status, endpoint.name, endpoint.endpoint);
            }
        }
        MacosSub::SetupCoreml => {
            println!("{}", macos::setup_instructions());
        }
    }
    Ok(())
}

async fn handle_linux(sub: LinuxSub) -> Result<()> {
    match sub {
        LinuxSub::Status => {
            let status = linux::LinuxRuntimeStatus::detect();
            println!("Ollama socket present: {}", bool_icon(status.ollama_socket));
            println!("CUDA detected: {}", bool_icon(status.cuda_available));
            for note in status.notes {
                println!("  - {}", note);
            }
        }
        LinuxSub::Doctor => {
            let hardware = detect_hardware();
            PlatformHardener::new(&hardware).apply()?;
            println!("Ran Linux platform hardening checks. Review warnings above, if any.");

            // Also run comprehensive diagnostics
            use crate::benchmark::CrossPlatformBenchmark;
            let benchmark = CrossPlatformBenchmark::new();
            let report = benchmark.run_diagnostics().await;

            println!("\n🏥 Additional System Diagnostic Report:");
            println!("Platform: {:?}", report.hardware.platform);
            println!("Connectivity endpoints reachable: {}/{}",
                     report.connectivity.endpoints.iter().filter(|e| e.reachable).count(),
                     report.connectivity.endpoints.len());
            println!("Memory usage: {}MB baseline, {}MB peak",
                     report.performance.memory_baseline_mb,
                     report.performance.memory_peak_mb);
        }
        LinuxSub::Setup => {
            println!("{}", linux::setup_instructions());
        }
    }
    Ok(())
}

async fn handle_doctor(verbose: bool, format: &str) -> Result<()> {
    use crate::benchmark::CrossPlatformBenchmark;

    println!("🏥 Running system diagnostics...");

    let benchmark = CrossPlatformBenchmark::new();
    let report = benchmark.run_diagnostics().await;

    match format.to_lowercase().as_str() {
        "json" => {
            println!("{}", serde_json::to_string_pretty(&report)?);
        }
        "table" | "text" => {
            println!("\n🏥 Kandil Code System Diagnostic Report");
            println!("=====================================");
            println!("Timestamp: {}", report.timestamp);

            println!("\n🖥️  Hardware Profile:");
            println!("  Platform: {:?}", report.hardware.platform);
            println!("  CPU: {} physical cores, {} logical cores",
                     report.hardware.cpu_physical_cores, report.hardware.cpu_logical_cores);
            println!("  RAM: {}GB total, {}GB available",
                     report.hardware.total_ram_gb, report.hardware.available_ram_gb);
            println!("  Disk: {}GB free", report.hardware.free_disk_gb);

            if let Some(gpu) = &report.hardware.gpu {
                println!("  GPU: {} {} with {}GB memory",
                         gpu.brand, gpu.model, gpu.memory_gb);
            } else {
                println!("  GPU: None detected");
            }

            println!("\n🔌 Connectivity Status:");
            for endpoint in &report.connectivity.endpoints {
                let status = if endpoint.reachable { "" } else { "" };
                println!("  {} {}: {} ({})", status, endpoint.name, endpoint.endpoint, endpoint.reachable);
            }

            println!("\n⚡ Performance Metrics:");
            println!("  CPU Test Duration: {}ms", report.performance.cpu_test_duration_ms);
            println!("  Memory Baseline: {}MB", report.performance.memory_baseline_mb);
            println!("  Memory Peak: {}MB", report.performance.memory_peak_mb);

            println!("\n🔒 Security Status:");
            println!("  API Keys Secure: {}", if report.security.api_key_secure { "" } else { "⚠️ " });
            println!("  Network Secure: {}", if report.security.network_secure { "" } else { "⚠️ " });

            if verbose {
                println!("\n📋 Detailed Report:");
                println!("  Hardware: {:?}", report.hardware);
                println!("  Connectivity: {:?}", report.connectivity);
                println!("  Performance: {:?}", report.performance);
                println!("  Security: {:?}", report.security);
            }
        }
        _ => {
            println!("{}", serde_json::to_string_pretty(&report)?);
        }
    }

    Ok(())
}

async fn handle_mobile(sub: MobileSub) -> Result<()> {
    let models_dir = models_root().await?;
    match sub {
        MobileSub::IosSync => {
            let dir = models_dir.clone();
            let export = task::spawn_blocking(move || mobile::export_ios_bundle(&dir)).await??;
            mobile_security::enforce_ios_bundle_security(&export)?;
            println!("📦 iOS bundle created at {}", export.display());
            println!(
                "Copy this directory into iCloud Drive and import it from the Kandil iOS app."
            );
        }
        MobileSub::AndroidSync => {
            let dir = models_dir.clone();
            let export =
                task::spawn_blocking(move || mobile::export_android_bundle(&dir)).await??;
            mobile_security::enforce_android_bundle_security(&export)?;
            println!("🤖 Android bundle created at {}", export.display());
            println!("Transfer it to /sdcard/kandil/models and use Termux or AI Core to import.");
        }
        MobileSub::EdgeSnapshot => {
            let dir = models_dir.clone();
            let export = task::spawn_blocking(move || edge::export_edge_snapshot(&dir)).await??;
            mobile_security::enforce_edge_bundle_security(&export)?;
            println!("🛠️ Edge manifest written to {}", export.display());
            println!(
                "Use the manifest to drive ONNX exports for Raspberry Pi / Jetson deployments."
            );
        }
    }
    Ok(())
}