seqtui 0.1.0

Fast TUI toolkit for viewing, translating, and manipulating biological sequences.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
//! Data model for the alignment viewer.
//!
//! This module contains all data structures for representing:
//! - Sequences and alignments
//! - Viewport state
//! - Application state
//! - Loading state for async operations
//!
//! The design allows for future extensions like filtering, codon views,
//! and translation between nucleotides and amino acids.

use std::ops::Range;
use std::path::PathBuf;

/// Type of biological sequence with nucleotide ratio.
/// The ratio indicates the proportion of nucleotide characters (ACGTUN) found.
/// - ratio > 0.8: displayed with nucleotide colors
/// - ratio < 0.5: likely amino acid, error for NT-requiring operations
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SequenceType {
    /// Ratio of nucleotide characters (0.0 to 1.0)
    pub nt_ratio: f64,
}

impl Default for SequenceType {
    fn default() -> Self {
        Self { nt_ratio: 1.0 }
    }
}

impl SequenceType {
    /// Creates a new SequenceType with the given NT ratio.
    pub fn new(nt_ratio: f64) -> Self {
        Self { nt_ratio }
    }

    /// Returns true if this appears to be nucleotide (>80% NT chars).
    /// Used for display coloring.
    #[inline]
    pub fn is_nucleotide(&self) -> bool {
        self.nt_ratio > 0.8
    }

    /// Returns true if this appears to be amino acid (≤80% NT chars).
    #[inline]
    pub fn is_amino_acid(&self) -> bool {
        !self.is_nucleotide()
    }

    /// Returns true if this is likely NOT nucleotide (<50% NT chars).
    /// Used for error checking in NT-requiring operations (translation, VCF).
    #[inline]
    pub fn is_likely_not_nucleotide(&self) -> bool {
        self.nt_ratio < 0.5
    }

    // Legacy constants for compatibility
    /// Nucleotide sequence type (100% NT ratio)
    pub const NUCLEOTIDE: SequenceType = SequenceType { nt_ratio: 1.0 };
    /// Amino acid sequence type (0% NT ratio)
    pub const AMINO_ACID: SequenceType = SequenceType { nt_ratio: 0.0 };
}

/// Loading state for async operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoadingState {
    /// No loading in progress, alignment is ready
    Ready,
    /// Loading file from disk
    LoadingFile {
        /// File path being loaded
        path: PathBuf,
        /// Progress message (e.g., "Parsing NEXUS file...")
        message: String,
        /// Number of sequences loaded so far (if available)
        sequences_loaded: Option<usize>,
    },
    /// Translating sequences
    Translating {
        /// Progress message
        message: String,
        /// Number of sequences translated so far
        sequences_done: usize,
        /// Total number of sequences
        total: usize,
    },
}

impl Default for LoadingState {
    fn default() -> Self {
        LoadingState::Ready
    }
}

impl LoadingState {
    /// Returns true if currently loading/processing.
    pub fn is_loading(&self) -> bool {
        !matches!(self, LoadingState::Ready)
    }

    /// Returns the display message for the loading state.
    pub fn message(&self) -> Option<&str> {
        match self {
            LoadingState::Ready => None,
            LoadingState::LoadingFile { message, .. } => Some(message),
            LoadingState::Translating { message, .. } => Some(message),
        }
    }

    /// Returns progress as a fraction (0.0 to 1.0) if available.
    pub fn progress(&self) -> Option<f64> {
        match self {
            LoadingState::Ready => None,
            LoadingState::LoadingFile { .. } => None, // Indeterminate
            LoadingState::Translating { sequences_done, total, .. } => {
                if *total > 0 {
                    Some(*sequences_done as f64 / *total as f64)
                } else {
                    None
                }
            }
        }
    }
}

/// A file entry in the file browser.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileEntry {
    /// File or directory name
    pub name: String,
    /// Full path
    pub path: PathBuf,
    /// Whether this is a directory
    pub is_dir: bool,
}

/// State for the file browser popup.
#[derive(Debug, Clone)]
pub struct FileBrowserState {
    /// Current directory being browsed
    pub current_dir: PathBuf,
    /// List of entries in the current directory
    pub entries: Vec<FileEntry>,
    /// Currently selected index (0-based)
    pub selected: usize,
    /// Scroll offset for long lists
    pub scroll_offset: usize,
    /// Error message that triggered the browser (shown in title)
    pub error_message: String,
    /// Whether to show all files (not just known sequence extensions)
    pub show_all_files: bool,
}

impl FileBrowserState {
    /// Creates a new file browser starting at the given directory.
    pub fn new(start_dir: PathBuf, error_message: String) -> Self {
        let mut browser = Self {
            current_dir: start_dir,
            entries: Vec::new(),
            selected: 0,
            scroll_offset: 0,
            error_message,
            show_all_files: false,
        };
        browser.refresh_entries();
        browser
    }

    /// Refreshes the list of entries from the current directory.
    pub fn refresh_entries(&mut self) {
        self.entries.clear();
        self.selected = 0;
        self.scroll_offset = 0;

        // Add parent directory entry if not at root
        if let Some(parent) = self.current_dir.parent() {
            self.entries.push(FileEntry {
                name: "..".to_string(),
                path: parent.to_path_buf(),
                is_dir: true,
            });
        }

        // Read directory contents
        if let Ok(read_dir) = std::fs::read_dir(&self.current_dir) {
            let mut dirs: Vec<FileEntry> = Vec::new();
            let mut files: Vec<FileEntry> = Vec::new();

            for entry in read_dir.flatten() {
                let path = entry.path();
                let name = entry.file_name().to_string_lossy().to_string();
                
                // Skip hidden files (starting with .)
                if name.starts_with('.') {
                    continue;
                }

                let is_dir = path.is_dir();
                let entry = FileEntry { name, path, is_dir };

                if is_dir {
                    dirs.push(entry);
                } else if self.show_all_files {
                    files.push(entry);
                } else {
                    // Only show sequence files
                    let ext = entry.path.extension()
                        .and_then(|e| e.to_str())
                        .map(|e| e.to_lowercase());
                    
                    if matches!(
                        ext.as_deref(),
                        Some(
                            "fasta" | "fa" | "fna" | "faa" | "fas" |
                            "phy" | "phylip" | "aln" | "ali" |
                            "nex" | "nexus" | "nxs"
                        )
                    ) {
                        files.push(entry);
                    }
                }
            }

            // Sort directories and files separately, then combine
            dirs.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
            files.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));

            self.entries.extend(dirs);
            self.entries.extend(files);
        }
    }

    /// Moves selection up.
    pub fn select_prev(&mut self) {
        if self.selected > 0 {
            self.selected -= 1;
            // Adjust scroll if needed
            if self.selected < self.scroll_offset {
                self.scroll_offset = self.selected;
            }
        }
    }

    /// Moves selection down.
    pub fn select_next(&mut self) {
        if self.selected + 1 < self.entries.len() {
            self.selected += 1;
        }
    }

    /// Adjusts scroll offset for the given visible height.
    pub fn adjust_scroll(&mut self, visible_height: usize) {
        if self.selected >= self.scroll_offset + visible_height {
            self.scroll_offset = self.selected - visible_height + 1;
        }
    }

    /// Returns the currently selected entry, if any.
    pub fn selected_entry(&self) -> Option<&FileEntry> {
        self.entries.get(self.selected)
    }

    /// Enters the selected directory.
    pub fn enter_selected(&mut self) -> Option<PathBuf> {
        if let Some(entry) = self.selected_entry() {
            if entry.is_dir {
                self.current_dir = entry.path.clone();
                self.refresh_entries();
                None
            } else {
                // Return the selected file path
                Some(entry.path.clone())
            }
        } else {
            None
        }
    }

    /// Toggles whether to show all files or only known sequence extensions.
    pub fn toggle_show_all_files(&mut self) {
        self.show_all_files = !self.show_all_files;
        self.refresh_entries();
    }
}

/// Represents a single sequence with its identifier and data.
/// 
/// Sequence data is stored as `Vec<u8>` (ASCII bytes) rather than `String`
/// for efficiency - biological sequences only use ASCII characters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Sequence {
    /// The sequence identifier (from FASTA header, without '>')
    pub id: String,
    /// The sequence data as ASCII bytes (nucleotides or amino acids)
    data: Vec<u8>,
}

impl Sequence {
    /// Creates a new sequence from a string.
    pub fn new(id: impl Into<String>, data: impl AsRef<str>) -> Self {
        Self {
            id: id.into(),
            data: data.as_ref().as_bytes().to_vec(),
        }
    }

    /// Creates a new sequence from raw bytes (zero-copy when possible).
    pub fn from_bytes(id: impl Into<String>, data: Vec<u8>) -> Self {
        Self {
            id: id.into(),
            data,
        }
    }

    /// Returns the length of the sequence.
    #[inline]
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns true if the sequence is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Gets a character at a specific position (O(1) direct byte access).
    #[inline]
    pub fn char_at(&self, pos: usize) -> Option<char> {
        self.data.get(pos).map(|&b| b as char)
    }

    /// Gets a byte at a specific position.
    #[inline]
    pub fn byte_at(&self, pos: usize) -> Option<u8> {
        self.data.get(pos).copied()
    }

    /// Gets the raw bytes of the sequence.
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }

    /// Takes ownership of the sequence data (for thread transfer).
    /// This avoids copying when the sequence won't be used again.
    #[inline]
    pub fn into_bytes(self) -> Vec<u8> {
        self.data
    }

    /// Clones the sequence data (when ownership transfer isn't possible).
    #[inline]
    pub fn clone_bytes(&self) -> Vec<u8> {
        self.data.clone()
    }

    /// Gets a slice of the sequence data as a string.
    /// This is safe because biological sequences are always ASCII.
    pub fn slice(&self, range: Range<usize>) -> &str {
        let start = range.start.min(self.data.len());
        let end = range.end.min(self.data.len());
        // SAFETY: Biological sequences are always valid ASCII/UTF-8
        unsafe { std::str::from_utf8_unchecked(&self.data[start..end]) }
    }

    /// Gets the entire sequence as a string slice.
    #[inline]
    pub fn as_str(&self) -> &str {
        // SAFETY: Biological sequences are always valid ASCII/UTF-8
        unsafe { std::str::from_utf8_unchecked(&self.data) }
    }
}

/// Represents an alignment of multiple sequences.
#[derive(Debug, Clone)]
pub struct Alignment {
    /// All sequences in the alignment
    pub sequences: Vec<Sequence>,
    /// The expected length of all sequences (if aligned)
    alignment_length: Option<usize>,
    /// Whether all sequences have the same length
    pub is_valid_alignment: bool,
    /// Warning message if sequences have different lengths
    pub warning: Option<String>,
    /// Detected sequence type (nucleotide or amino acid)
    pub sequence_type: SequenceType,
}

impl Alignment {
    /// Creates a new alignment from a vector of sequences.
    pub fn new(sequences: Vec<Sequence>) -> Self {
        let (is_valid, alignment_length, warning) = Self::validate_alignment(&sequences);
        let sequence_type = Self::detect_sequence_type(&sequences);
        Self {
            sequences,
            alignment_length,
            is_valid_alignment: is_valid,
            warning,
            sequence_type,
        }
    }

    /// Detects sequence type by sampling characters across sequences.
    /// 
    /// Samples up to 2500 characters total, taking at most 500 characters per sequence.
    /// This ensures we sample across at least 5 sequences (if available) rather than
    /// just the first sequence, providing a more reliable estimate.
    /// 
    /// Returns a SequenceType with the computed NT ratio.
    fn detect_sequence_type(sequences: &[Sequence]) -> SequenceType {
        if sequences.is_empty() {
            return SequenceType::NUCLEOTIDE;
        }

        let mut nucleotide_chars = 0usize;
        let mut total_chars = 0usize;
        const MAX_CHARS_PER_SEQ: usize = 500;
        const MAX_TOTAL_CHARS: usize = 2500;

        for seq in sequences {
            let mut seq_chars = 0usize;
            for &b in seq.as_bytes() {
                let upper = b.to_ascii_uppercase();
                // Skip gaps, missing data, and spaces
                if matches!(upper, b'-' | b'.' | b' ' | b'?' | b'N') {
                    continue;
                }
                total_chars += 1;
                seq_chars += 1;
                // Nucleotide characters (ACGTU)
                if matches!(upper, b'A' | b'C' | b'G' | b'T' | b'U') {
                    nucleotide_chars += 1;
                }
                // Stop sampling this sequence after MAX_CHARS_PER_SEQ
                if seq_chars >= MAX_CHARS_PER_SEQ {
                    break;
                }
            }
            // Stop sampling after MAX_TOTAL_CHARS total
            if total_chars >= MAX_TOTAL_CHARS {
                break;
            }
        }

        if total_chars == 0 {
            return SequenceType::NUCLEOTIDE;
        }

        SequenceType::new(nucleotide_chars as f64 / total_chars as f64)
    }

    /// Validates that all sequences have the same length.
    fn validate_alignment(sequences: &[Sequence]) -> (bool, Option<usize>, Option<String>) {
        if sequences.is_empty() {
            return (true, None, None);
        }

        let first_len = sequences[0].len();
        let all_same = sequences.iter().all(|s| s.len() == first_len);

        if all_same {
            (true, Some(first_len), None)
        } else {
            let min_len = sequences.iter().map(|s| s.len()).min().unwrap_or(0);
            let max_len = sequences.iter().map(|s| s.len()).max().unwrap_or(0);
            let warning = format!(
                "Warning: Sequences have different lengths (min: {}, max: {}). Not a valid alignment.",
                min_len, max_len
            );
            (false, Some(max_len), Some(warning))
        }
    }

    /// Returns the number of sequences.
    pub fn sequence_count(&self) -> usize {
        self.sequences.len()
    }

    /// Returns the alignment length (max sequence length).
    pub fn alignment_length(&self) -> usize {
        self.alignment_length.unwrap_or(0)
    }

    /// Returns the maximum identifier length (for display purposes).
    pub fn max_id_length(&self) -> usize {
        self.sequences.iter().map(|s| s.id.len()).max().unwrap_or(0)
    }

    /// Gets a sequence by index.
    pub fn get(&self, index: usize) -> Option<&Sequence> {
        self.sequences.get(index)
    }

    /// Returns true if the alignment is empty.
    pub fn is_empty(&self) -> bool {
        self.sequences.is_empty()
    }
}

/// The viewport defines what portion of the alignment is currently visible.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Viewport {
    /// Index of the first visible sequence (row)
    pub first_row: usize,
    /// Index of the first visible column
    pub first_col: usize,
    /// Number of visible rows
    pub visible_rows: usize,
    /// Number of visible columns
    pub visible_cols: usize,
}

impl Viewport {
    /// Creates a new viewport.
    pub fn new(visible_rows: usize, visible_cols: usize) -> Self {
        Self {
            first_row: 0,
            first_col: 0,
            visible_rows,
            visible_cols,
        }
    }

    /// Updates the viewport dimensions.
    pub fn resize(&mut self, visible_rows: usize, visible_cols: usize) {
        self.visible_rows = visible_rows;
        self.visible_cols = visible_cols;
    }

    /// Returns the range of visible rows.
    pub fn row_range(&self) -> Range<usize> {
        self.first_row..self.first_row + self.visible_rows
    }

    /// Returns the range of visible columns.
    pub fn col_range(&self) -> Range<usize> {
        self.first_col..self.first_col + self.visible_cols
    }

    /// Checks if a column is visible.
    pub fn is_col_visible(&self, col: usize) -> bool {
        col >= self.first_col && col < self.first_col + self.visible_cols
    }

    /// Checks if a row is visible.
    pub fn is_row_visible(&self, row: usize) -> bool {
        row >= self.first_row && row < self.first_row + self.visible_rows
    }
}

/// The current cursor position in the alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Cursor {
    /// Current row (sequence index)
    pub row: usize,
    /// Current column (position in sequence)
    pub col: usize,
}

impl Cursor {
    /// Creates a new cursor at origin.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a cursor at a specific position.
    pub fn at(row: usize, col: usize) -> Self {
        Self { row, col }
    }
}

/// Help tab sections
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum HelpTab {
    #[default]
    Basics,
    Navigation,
    VimNav,
    Search,
    Translation,
}

impl HelpTab {
    /// Returns the next tab (wrapping around)
    pub fn next(self) -> Self {
        match self {
            HelpTab::Basics => HelpTab::Navigation,
            HelpTab::Navigation => HelpTab::VimNav,
            HelpTab::VimNav => HelpTab::Search,
            HelpTab::Search => HelpTab::Translation,
            HelpTab::Translation => HelpTab::Basics,
        }
    }

    /// Returns the previous tab (wrapping around)
    pub fn prev(self) -> Self {
        match self {
            HelpTab::Basics => HelpTab::Translation,
            HelpTab::Navigation => HelpTab::Basics,
            HelpTab::VimNav => HelpTab::Navigation,
            HelpTab::Search => HelpTab::VimNav,
            HelpTab::Translation => HelpTab::Search,
        }
    }

    /// Returns the tab name for display
    pub fn name(self) -> &'static str {
        match self {
            HelpTab::Basics => "Basics",
            HelpTab::Navigation => "Arrow Nav",
            HelpTab::VimNav => "Vim Nav",
            HelpTab::Search => "Search",
            HelpTab::Translation => "Translation",
        }
    }

    /// Returns all tabs in order
    pub fn all() -> &'static [HelpTab] {
        &[HelpTab::Basics, HelpTab::Navigation, HelpTab::VimNav, HelpTab::Search, HelpTab::Translation]
    }
}

/// Application mode for handling different input states.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum AppMode {
    /// Normal navigation mode
    #[default]
    Normal,
    /// Command input mode (after pressing ':')
    Command(String),
    /// Search forward mode (after pressing '/')
    Search(String),
    /// Search backward mode (after pressing '?')
    SearchBackward(String),
    /// Translation settings mode (selecting genetic code and frame)
    TranslationSettings,
}

/// View mode for the alignment (nucleotide or translated amino acid).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ViewMode {
    /// Original nucleotide view
    #[default]
    Nucleotide,
    /// Translated amino acid view
    AminoAcid,
}

/// Translation settings for NT to AA conversion.
#[derive(Debug, Clone)]
pub struct TranslationSettings {
    /// Selected genetic code ID (1-33)
    pub genetic_code_id: u8,
    /// Reading frame (0, 1, or 2 for +1, +2, +3)
    pub frame: usize,
    /// Currently selected genetic code index in the list (for UI)
    pub selected_code_index: usize,
    /// Currently selected frame in the UI (0, 1, 2)
    pub selected_frame: usize,
    /// Scroll offset for the genetic code list
    pub scroll_offset: usize,
    /// Whether user has translated at least once (to show settings on first use)
    pub has_translated: bool,
}

impl Default for TranslationSettings {
    fn default() -> Self {
        Self {
            genetic_code_id: 1, // Standard code
            frame: 0,          // +1 frame
            selected_code_index: 0,
            selected_frame: 0,
            scroll_offset: 0,
            has_translated: false,
        }
    }
}

/// The complete application state.
#[derive(Debug)]
pub struct AppState {
    /// File name (basename without extension) for display
    pub file_name: String,
    /// The original loaded alignment (always nucleotide if translated)
    pub alignment: Alignment,
    /// The translated alignment (if viewing as AA) - lazily computed and cached
    translated_alignment: Option<Alignment>,
    /// Genetic code ID used for the cached translation (to detect if recomputation needed)
    cached_translation_code_id: Option<u8>,
    /// Reading frame used for the cached translation (to detect if recomputation needed)
    cached_translation_frame: Option<usize>,
    /// Current view mode
    pub view_mode: ViewMode,
    /// Translation settings
    pub translation_settings: TranslationSettings,
    /// Current viewport
    pub viewport: Viewport,
    /// Current cursor position
    pub cursor: Cursor,
    /// Current application mode
    pub mode: AppMode,
    /// Whether the application should quit
    pub should_quit: bool,
    /// Status message to display
    pub status_message: Option<String>,
    /// Last search pattern
    pub last_search: Option<String>,
    /// Last search direction (true = backward, false = forward)
    pub last_search_backward: bool,
    /// Whether to show the help overlay
    pub show_help: bool,
    /// Current help tab
    pub help_tab: HelpTab,
    /// Pending 'g' key for g-commands
    pub pending_g: bool,
    /// Pending 'z' key for z-commands
    pub pending_z: bool,
    /// Number buffer for <number>| command
    pub number_buffer: String,
    /// Loading state for async operations
    pub loading_state: LoadingState,
    /// Spinner animation frame (0-3)
    pub spinner_frame: usize,
    /// Error popup to display (shown as centered popup, dismissable with any key)
    pub error_popup: Option<String>,
    /// File browser state (shown when file not found)
    pub file_browser: Option<FileBrowserState>,
    /// Whether to use fancy UI glyphs (non-Windows only)
    pub fancy_ui: bool,
}

impl AppState {
        /// Goes to a specific row (1-indexed for user, like Vim :line command).
        /// Clamps column to alignment length. Returns true if successful, false if out of range.
        fn goto_row_1indexed(&mut self, row: usize) -> bool {
            let num_seqs = self.active_alignment().sequence_count();
            if row > 0 && row <= num_seqs {
                self.cursor.row = row - 1; // 1-indexed for user
                // Keep cursor column, but clamp to alignment length
                let aln_len = self.active_alignment().alignment_length();
                self.cursor.col = self.cursor.col.min(aln_len.saturating_sub(1));
                self.ensure_cursor_visible();
                self.status_message = None; // Clear any previous error
                true
            } else {
                self.status_message = Some(format!("Invalid sequence number: {}", row));
                false
            }
        }
    /// Creates a new application state with the given alignment.
    pub fn new(alignment: Alignment, file_name: String) -> Self {
        let warning = alignment.warning.clone();
        Self {
            file_name,
            alignment,
            translated_alignment: None,
            cached_translation_code_id: None,
            cached_translation_frame: None,
            view_mode: ViewMode::Nucleotide,
            translation_settings: TranslationSettings::default(),
            viewport: Viewport::new(0, 0),
            cursor: Cursor::new(),
            mode: AppMode::Normal,
            should_quit: false,
            status_message: warning,
            last_search: None,
            last_search_backward: false,
            show_help: false,
            help_tab: HelpTab::default(),
            pending_g: false,
            pending_z: false,
            number_buffer: String::new(),
            loading_state: LoadingState::Ready,
            spinner_frame: 0,
            error_popup: None,
            file_browser: None,
            fancy_ui: false,
        }
    }

    /// Creates an empty application state for async loading.
    pub fn new_loading(file_name: String, path: PathBuf) -> Self {
        Self {
            file_name: file_name.clone(),
            alignment: Alignment::new(vec![]),
            translated_alignment: None,
            cached_translation_code_id: None,
            cached_translation_frame: None,
            view_mode: ViewMode::Nucleotide,
            translation_settings: TranslationSettings::default(),
            viewport: Viewport::new(0, 0),
            cursor: Cursor::new(),
            mode: AppMode::Normal,
            should_quit: false,
            status_message: None,
            last_search: None,
            last_search_backward: false,
            show_help: false,
            help_tab: HelpTab::default(),
            pending_g: false,
            pending_z: false,
            number_buffer: String::new(),
            loading_state: LoadingState::LoadingFile {
                path,
                message: format!("Loading {}...", file_name),
                sequences_loaded: None,
            },
            spinner_frame: 0,
            error_popup: None,
            file_browser: None,
            fancy_ui: false,
        }
    }

    /// Updates the alignment after async loading completes.
    pub fn set_alignment(&mut self, alignment: Alignment) {
        let warning = alignment.warning.clone();
        self.alignment = alignment;
        self.loading_state = LoadingState::Ready;
        if let Some(w) = warning {
            self.status_message = Some(w);
        }
    }

    /// Sets an error state after loading fails.
    /// If the error is "file not found", opens the file browser.
    pub fn set_loading_error(&mut self, error: String, file_path: Option<PathBuf>) {
        self.loading_state = LoadingState::Ready;
        
        // Check if this is a "file not found" error
        let is_not_found = error.contains("No such file") || error.contains("not found");
        
        if is_not_found {
            // Open file browser starting from the file's directory or current directory
            let start_dir = file_path
                .as_ref()
                .and_then(|p| p.parent())
                .filter(|p| !p.as_os_str().is_empty()) // Filter out empty parent paths
                .map(|p| p.to_path_buf())
                .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
            
            let file_name = file_path
                .as_ref()
                .and_then(|p| p.file_name())
                .and_then(|n| n.to_str())
                .unwrap_or("file");
            
            let error_msg = format!("File not found: {}", file_name);
            self.file_browser = Some(FileBrowserState::new(start_dir, error_msg));
        } else {
            // Show error popup for other errors
            self.error_popup = Some(error);
        }
    }

    /// Shows an error popup with the given message.
    pub fn show_error_popup(&mut self, message: String) {
        self.error_popup = Some(message);
    }

    /// Dismisses the error popup.
    pub fn dismiss_error_popup(&mut self) {
        self.error_popup = None;
    }

    /// Closes the file browser.
    pub fn close_file_browser(&mut self) {
        self.file_browser = None;
    }

    /// Moves file browser selection up.
    pub fn file_browser_up(&mut self) {
        if let Some(browser) = &mut self.file_browser {
            browser.select_prev();
        }
    }

    /// Moves file browser selection down.
    pub fn file_browser_down(&mut self) {
        if let Some(browser) = &mut self.file_browser {
            browser.select_next();
        }
    }

    /// Selects the current file browser entry.
    /// Returns Some(path) if a file was selected, None if navigating into directory.
    pub fn file_browser_select(&mut self) -> Option<PathBuf> {
        if let Some(browser) = &mut self.file_browser {
            browser.enter_selected()
        } else {
            None
        }
    }

    /// Goes to parent directory in file browser.
    pub fn file_browser_parent(&mut self) {
        if let Some(browser) = &mut self.file_browser {
            if let Some(parent) = browser.current_dir.parent() {
                browser.current_dir = parent.to_path_buf();
                browser.refresh_entries();
            }
        }
    }

    /// Toggles whether the file browser shows all files or only sequence files.
    pub fn file_browser_toggle_show_all(&mut self) {
        if let Some(browser) = &mut self.file_browser {
            browser.toggle_show_all_files();
        }
    }

    /// Quits the file browser. If an alignment is loaded, just close the browser.
    /// If no alignment is loaded, quit the application.
    pub fn file_browser_quit(&mut self) {
        self.file_browser = None;
        // Only quit if no alignment is loaded
        if self.alignment.sequence_count() == 0 {
            self.should_quit = true;
        }
    }

    /// Sets the translated alignment after async translation completes.
    pub fn set_translated_alignment(&mut self, alignment: Alignment) {
        self.translated_alignment = Some(alignment);
        // Cache the settings used for this translation
        self.cached_translation_code_id = Some(self.translation_settings.genetic_code_id);
        self.cached_translation_frame = Some(self.translation_settings.frame);
        self.loading_state = LoadingState::Ready;
        self.view_mode = ViewMode::AminoAcid;
        
        // Convert NT cursor position to AA position
        let frame = self.translation_settings.frame;
        let aa_col = if self.cursor.col >= frame {
            (self.cursor.col - frame) / 3
        } else {
            0
        };
        
        let aa_len = self.translated_alignment.as_ref()
            .map(|a| a.alignment_length())
            .unwrap_or(0);
        self.cursor.col = aa_col.min(aa_len.saturating_sub(1));
        
        self.ensure_cursor_visible();
        self.status_message = Some(format!(
            "Translated using code {} (frame +{})",
            self.translation_settings.genetic_code_id,
            self.translation_settings.frame + 1
        ));
    }

    /// Returns true if we should start background translation.
    /// Called when user triggers translation command.
    /// Returns false if we already have a cached translation with matching settings.
    pub fn should_start_translation(&self) -> bool {
        self.alignment.sequence_type.is_nucleotide()
            && !self.loading_state.is_loading()
            && !self.has_valid_cached_translation()
    }
    
    /// Returns true if we have a cached translated alignment that matches
    /// the current translation settings (code_id and frame).
    pub fn has_valid_cached_translation(&self) -> bool {
        if self.translated_alignment.is_none() {
            return false;
        }
        let code_matches = self.cached_translation_code_id == Some(self.translation_settings.genetic_code_id);
        let frame_matches = self.cached_translation_frame == Some(self.translation_settings.frame);
        code_matches && frame_matches
    }
    
    /// Switches to the cached amino acid view without recomputation.
    /// Call this when has_valid_cached_translation() returns true.
    pub fn switch_to_cached_aa_view(&mut self) {
        if self.translated_alignment.is_none() {
            return;
        }
        self.view_mode = ViewMode::AminoAcid;
        
        // Convert NT cursor position to AA position
        let frame = self.translation_settings.frame;
        let aa_col = if self.cursor.col >= frame {
            (self.cursor.col - frame) / 3
        } else {
            0
        };
        
        let aa_len = self.translated_alignment.as_ref()
            .map(|a| a.alignment_length())
            .unwrap_or(0);
        self.cursor.col = aa_col.min(aa_len.saturating_sub(1));
        
        self.ensure_cursor_visible();
        self.status_message = Some(format!(
            "Using cached translation (code {}, frame +{})",
            self.cached_translation_code_id.unwrap_or(1),
            self.cached_translation_frame.unwrap_or(0) + 1
        ));
    }

    /// Advances the spinner animation frame.
    pub fn tick_spinner(&mut self) {
        self.spinner_frame = (self.spinner_frame + 1) % 4;
    }

    /// Returns the current spinner character.
    pub fn spinner_char(&self) -> char {
        const SPINNER: [char; 4] = ['|', '/', '-', '\\'];
        SPINNER[self.spinner_frame]
    }

    /// Returns the currently active alignment (original or translated).
    pub fn active_alignment(&self) -> &Alignment {
        match self.view_mode {
            ViewMode::Nucleotide => &self.alignment,
            ViewMode::AminoAcid => self.translated_alignment.as_ref().unwrap_or(&self.alignment),
        }
    }

    /// Returns whether a translated alignment exists.
    pub fn has_translated_alignment(&self) -> bool {
        self.translated_alignment.is_some()
    }

    /// Updates the viewport size based on terminal dimensions.
    pub fn update_viewport_size(&mut self, rows: usize, cols: usize) {
        self.viewport.resize(rows, cols);
        self.ensure_cursor_visible();
    }

    /// Moves the cursor up by one row.
    pub fn move_up(&mut self) {
        if self.cursor.row > 0 {
            self.cursor.row -= 1;
            self.ensure_cursor_visible();
        }
    }

    /// Moves the cursor down by one row.
    pub fn move_down(&mut self) {
        if self.cursor.row + 1 < self.active_alignment().sequence_count() {
            self.cursor.row += 1;
            self.ensure_cursor_visible();
        }
    }

    /// Moves the cursor left by one column.
    pub fn move_left(&mut self) {
        if self.cursor.col > 0 {
            self.cursor.col -= 1;
            self.ensure_cursor_visible();
        }
    }

    /// Moves the cursor right by one column.
    pub fn move_right(&mut self) {
        if self.cursor.col + 1 < self.active_alignment().alignment_length() {
            self.cursor.col += 1;
            self.ensure_cursor_visible();
        }
    }

    /// Moves the cursor up by half a page.
    pub fn half_page_up(&mut self) {
        let half_page = self.viewport.visible_rows / 2;
        self.cursor.row = self.cursor.row.saturating_sub(half_page);
        self.ensure_cursor_visible();
    }

    /// Moves the cursor down by half a page.
    pub fn half_page_down(&mut self) {
        let half_page = self.viewport.visible_rows / 2;
        let max_row = self.active_alignment().sequence_count().saturating_sub(1);
        self.cursor.row = (self.cursor.row + half_page).min(max_row);
        self.ensure_cursor_visible();
    }

    /// Moves the cursor left by half a screen width.
    pub fn half_page_left(&mut self) {
        self.clear_pending();
        let half_page = self.viewport.visible_cols / 2;
        self.cursor.col = self.cursor.col.saturating_sub(half_page);
        self.ensure_cursor_visible();
    }

    /// Moves the cursor right by half a screen width.
    pub fn half_page_right(&mut self) {
        self.clear_pending();
        let half_page = self.viewport.visible_cols / 2;
        let max_col = self.active_alignment().alignment_length().saturating_sub(1);
        self.cursor.col = (self.cursor.col + half_page).min(max_col);
        self.ensure_cursor_visible();
    }

    /// Moves the cursor up by a full page.
    pub fn page_up(&mut self) {
        let page = self.viewport.visible_rows.saturating_sub(1).max(1);
        self.cursor.row = self.cursor.row.saturating_sub(page);
        self.ensure_cursor_visible();
    }

    /// Moves the cursor down by a full page.
    pub fn page_down(&mut self) {
        let page = self.viewport.visible_rows.saturating_sub(1).max(1);
        let max_row = self.active_alignment().sequence_count().saturating_sub(1);
        self.cursor.row = (self.cursor.row + page).min(max_row);
        self.ensure_cursor_visible();
    }

    /// Returns true if a character is a word delimiter (gap or stop codon).
    #[inline]
    fn is_word_delimiter(c: u8) -> bool {
        c == b'-' || c == b'*'
    }

    /// Moves to the start of the next word (w).
    /// Words are delimited by gaps (-) and stops (*).
    pub fn word_forward(&mut self) {
        let alignment = self.active_alignment();
        let aln_len = alignment.alignment_length();
        if aln_len == 0 || self.cursor.row >= alignment.sequence_count() {
            return;
        }

        let seq = alignment.sequences[self.cursor.row].as_bytes();
        let mut col = self.cursor.col;

        // If on a non-delimiter, skip to end of current word
        if col < seq.len() && !Self::is_word_delimiter(seq[col]) {
            while col < seq.len() && !Self::is_word_delimiter(seq[col]) {
                col += 1;
            }
        }

        // Skip delimiters to find start of next word
        while col < seq.len() && Self::is_word_delimiter(seq[col]) {
            col += 1;
        }

        // Clamp to valid range
        self.cursor.col = col.min(aln_len.saturating_sub(1));
        self.ensure_cursor_visible();
    }

    /// Moves to the start of the previous word (b).
    /// Words are delimited by gaps (-) and stops (*).
    pub fn word_backward(&mut self) {
        let alignment = self.active_alignment();
        if alignment.alignment_length() == 0 || self.cursor.row >= alignment.sequence_count() {
            return;
        }

        let seq = alignment.sequences[self.cursor.row].as_bytes();
        let mut col = self.cursor.col;

        // If at start, nothing to do
        if col == 0 {
            return;
        }

        // Move back one position first
        col -= 1;

        // Skip delimiters backward
        while col > 0 && Self::is_word_delimiter(seq[col]) {
            col -= 1;
        }

        // Now we're on a word character (or at position 0)
        // Find the start of this word
        while col > 0 && !Self::is_word_delimiter(seq[col - 1]) {
            col -= 1;
        }

        self.cursor.col = col;
        self.ensure_cursor_visible();
    }

    /// Moves to the end of the current/next word (e).
    /// Words are delimited by gaps (-) and stops (*).
    pub fn word_end(&mut self) {
        let alignment = self.active_alignment();
        let aln_len = alignment.alignment_length();
        if aln_len == 0 || self.cursor.row >= alignment.sequence_count() {
            return;
        }

        let seq = alignment.sequences[self.cursor.row].as_bytes();
        let mut col = self.cursor.col;

        // Move forward at least one position
        if col + 1 < seq.len() {
            col += 1;
        }

        // Skip delimiters
        while col < seq.len() && Self::is_word_delimiter(seq[col]) {
            col += 1;
        }

        // Find end of this word
        while col + 1 < seq.len() && !Self::is_word_delimiter(seq[col + 1]) {
            col += 1;
        }

        // Clamp to valid range
        self.cursor.col = col.min(aln_len.saturating_sub(1));
        self.ensure_cursor_visible();
    }

    /// Ensures the cursor is visible in the viewport, with centering behavior.
    fn ensure_cursor_visible_center(&mut self) {
        // Vertical scrolling - keep cursor in view
        if self.cursor.row < self.viewport.first_row {
            self.viewport.first_row = self.cursor.row;
        } else if self.cursor.row >= self.viewport.first_row + self.viewport.visible_rows {
            self.viewport.first_row = self.cursor.row.saturating_sub(self.viewport.visible_rows - 1);
        }

        // Horizontal scrolling - center when reaching edge
        if self.cursor.col < self.viewport.first_col {
            // Cursor went left of viewport - center it
            self.center_column();
        } else if self.cursor.col >= self.viewport.first_col + self.viewport.visible_cols {
            // Cursor went right of viewport - center it
            self.center_column();
        }

        // Clamp viewport to valid bounds
        self.clamp_viewport();
    }

    /// Ensures the cursor is visible in the viewport, with edge-aligned scrolling.
    fn ensure_cursor_visible(&mut self) {
        // Vertical scrolling - keep cursor in view
        if self.cursor.row < self.viewport.first_row {
            self.viewport.first_row = self.cursor.row;
        } else if self.cursor.row >= self.viewport.first_row + self.viewport.visible_rows {
            self.viewport.first_row = self.cursor.row.saturating_sub(self.viewport.visible_rows - 1);
        }

        // Horizontal scrolling - keep cursor on-screen without centering
        if self.cursor.col < self.viewport.first_col {
            self.viewport.first_col = self.cursor.col;
        } else if self.cursor.col >= self.viewport.first_col + self.viewport.visible_cols {
            let last_visible = self.viewport.visible_cols.saturating_sub(1);
            self.viewport.first_col = self.cursor.col.saturating_sub(last_visible);
        }

        // Clamp viewport to valid bounds
        self.clamp_viewport();
    }

    /// Centers the current column in the viewport.
    fn center_column(&mut self) {
        if self.viewport.visible_cols > 0 {
            let half = self.viewport.visible_cols / 2;
            self.viewport.first_col = self.cursor.col.saturating_sub(half);
        }
    }

    /// Clamps the viewport to valid alignment bounds.
    fn clamp_viewport(&mut self) {
        let seq_count = self.active_alignment().sequence_count();
        let aln_length = self.active_alignment().alignment_length();
        let max_row = seq_count.saturating_sub(1);
        let max_col = aln_length.saturating_sub(1);

        // Ensure we don't scroll past the end
        if self.viewport.first_row + self.viewport.visible_rows > seq_count {
            self.viewport.first_row = seq_count.saturating_sub(self.viewport.visible_rows);
        }

        if self.viewport.first_col + self.viewport.visible_cols > aln_length {
            self.viewport.first_col = aln_length.saturating_sub(self.viewport.visible_cols);
        }

        // Clamp cursor to valid bounds
        self.cursor.row = self.cursor.row.min(max_row);
        self.cursor.col = self.cursor.col.min(max_col);
    }

    /// Enters command mode.
    pub fn enter_command_mode(&mut self) {
        self.mode = AppMode::Command(String::new());
    }

    /// Handles a character input in command mode.
    pub fn command_input(&mut self, c: char) {
        if let AppMode::Command(ref mut cmd) = self.mode {
            cmd.push(c);
        }
    }

    /// Handles backspace in command mode.
    pub fn command_backspace(&mut self) {
        if let AppMode::Command(ref mut cmd) = self.mode {
            cmd.pop();
            if cmd.is_empty() {
                self.mode = AppMode::Normal;
            }
        }
    }

    /// Executes the current command.
    /// Returns true if translation should be started (handled by controller).
    pub fn execute_command(&mut self) -> bool {
        let mut start_translation = false;
        if let AppMode::Command(ref cmd) = self.mode.clone() {
            match cmd.as_str() {
                "$" => {
                    let num_seqs = self.active_alignment().sequence_count();
                    if num_seqs > 0 {
                        self.goto_row_1indexed(num_seqs);
                    } else {
                        self.status_message = Some("No sequences loaded".to_string());
                    }
                }
                "q" | "quit" => self.should_quit = true,
                "h" | "help" => self.show_help(),
                "e" | "edit" => {
                    // Open file browser to select a new file
                    let start_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
                    self.file_browser = Some(FileBrowserState::new(start_dir, "Open file".to_string()));
                    self.mode = AppMode::Normal;
                    return false;
                }
                "asAA" | "asaa" => {
                    if !self.alignment.sequence_type.is_nucleotide() {
                        self.status_message = Some("Cannot translate: not a nucleotide sequence".to_string());
                    } else if self.has_valid_cached_translation() {
                        // We have a valid cached translation - switch to it directly
                        self.switch_to_cached_aa_view();
                    } else if !self.translation_settings.has_translated {
                        // First time: show settings so user knows the options
                        self.enter_translation_settings();
                        return false; // Don't reset to Normal
                    } else {
                        // Subsequent times: request translation (controller will start background thread)
                        start_translation = true;
                    }
                }
                "asNT" | "asnt" => self.switch_to_nucleotide_view(),
                "setcode" => {
                    self.enter_translation_settings();
                    return false; // Don't reset to Normal - enter_translation_settings sets the mode
                }
                "w" => {
                    // :w alone without filename
                    self.status_message = Some("Usage: :w filename.fasta".to_string());
                }
                _ => {
                    // Handle :w filename - save to FASTA
                    if cmd.starts_with("w ") {
                        let filename = cmd[2..].trim();
                        if filename.is_empty() {
                            self.status_message = Some("Usage: :w filename.fasta".to_string());
                        } else {
                            match self.write_fasta(filename) {
                                Ok(count) => {
                                    self.status_message = Some(format!(
                                        "Saved {} sequences to {}", count, filename
                                    ));
                                }
                                Err(e) => {
                                    self.status_message = Some(format!("Error saving: {}", e));
                                }
                            }
                        }
                    }
                    // Handle :number for row/sequence navigation (like Vim's :line)
                    else if let Ok(row) = cmd.parse::<usize>() {
                        let num_seqs = self.active_alignment().sequence_count();
                        if num_seqs > 0 {
                            self.goto_row_1indexed(row);
                        } else {
                            self.status_message = Some("No sequences loaded".to_string());
                        }
                    } else {
                        self.status_message = Some(format!("Unknown command: {}", cmd));
                    }
                }
            }
        }
        self.mode = AppMode::Normal;
        start_translation
    }

    /// Enters translation settings mode.
    pub fn enter_translation_settings(&mut self) {
        // Only allow translation if the original sequence is nucleotide
        if !self.alignment.sequence_type.is_nucleotide() {
            self.status_message = Some("Cannot translate: not a nucleotide sequence".to_string());
            return;
        }
        self.mode = AppMode::TranslationSettings;
    }

    /// Writes the current view (NT or AA) to a FASTA file.
    /// Sequences are written on a single line (convenient for bash processing).
    /// Returns the number of sequences written.
    pub fn write_fasta(&self, filename: &str) -> std::io::Result<usize> {
        use std::io::Write;
        
        let alignment = self.active_alignment();
        let mut file = std::fs::File::create(filename)?;
        
        for seq in &alignment.sequences {
            // Write header
            writeln!(file, ">{}", seq.id)?;
            // Write sequence on single line
            writeln!(file, "{}", seq.as_str())?;
        }
        
        Ok(alignment.sequence_count())
    }

    /// Switches to nucleotide view.
    pub fn switch_to_nucleotide_view(&mut self) {
        if self.view_mode == ViewMode::Nucleotide {
            self.status_message = Some("Already in nucleotide view".to_string());
            return;
        }
        
        // Convert AA position to NT position: first nucleotide of the codon
        // AA position n corresponds to NT positions (n*3 + frame) to (n*3 + frame + 2)
        let frame = self.translation_settings.frame as usize;
        let nt_col = self.cursor.col * 3 + frame;
        self.cursor.col = nt_col.min(self.alignment.alignment_length().saturating_sub(1));
        
        self.view_mode = ViewMode::Nucleotide;
        
        // Keep the translated alignment cached for fast switching back
        // It will be invalidated if genetic code or frame changes
        
        self.ensure_cursor_visible();
        self.status_message = Some("Switched to nucleotide view".to_string());
    }

    /// Switches to amino acid view (performs translation).
    pub fn switch_to_amino_acid_view(&mut self) {
        use crate::genetic_code::GeneticCodes;
        
        // Perform translation
        let codes = GeneticCodes::new();
        let code = codes.get(self.translation_settings.genetic_code_id)
            .unwrap_or_else(|| codes.default_code());
        let frame = self.translation_settings.frame as usize;
        
        // Translate all sequences (single-threaded - fast enough now)
        let translated_seqs: Vec<crate::model::Sequence> = self.alignment.sequences
            .iter()
            .map(|seq| {
                let aa_data = code.translate_sequence(seq.as_bytes(), frame);
                Sequence::from_bytes(seq.id.clone(), aa_data)
            })
            .collect();
        
        let mut translated = Alignment::new(translated_seqs);
        // Force sequence type to AminoAcid
        translated.sequence_type = SequenceType::AMINO_ACID;
        self.translated_alignment = Some(translated);
        
        // Convert NT cursor position to AA position
        // NT position n in frame f corresponds to AA position (n - f) / 3
        let aa_col = if self.cursor.col >= frame {
            (self.cursor.col - frame) / 3
        } else {
            0
        };
        
        let aa_len = self.translated_alignment.as_ref()
            .map(|a| a.alignment_length())
            .unwrap_or(0);
        self.cursor.col = aa_col.min(aa_len.saturating_sub(1));
        
        self.view_mode = ViewMode::AminoAcid;
        self.ensure_cursor_visible();
        self.status_message = Some(format!(
            "Translated using code {} (frame +{})",
            self.translation_settings.genetic_code_id,
            self.translation_settings.frame + 1
        ));
    }

    /// Confirms translation settings (does NOT start translation - that's handled by controller).
    /// Returns true if translation should be started.
    pub fn confirm_translation_settings(&mut self) -> bool {
        // Copy selected values to actual settings
        self.translation_settings.genetic_code_id = {
            use crate::genetic_code::GeneticCodes;
            let codes = GeneticCodes::new();
            codes.all().get(self.translation_settings.selected_code_index)
                .map(|c| c.id)
                .unwrap_or(1)
        };
        self.translation_settings.frame = self.translation_settings.selected_frame;
        self.translation_settings.has_translated = true;
        
        self.mode = AppMode::Normal;
        
        // Check if we can use cached translation (settings match)
        if self.has_valid_cached_translation() {
            self.switch_to_cached_aa_view();
            return false;
        }
        
        // Return true to signal controller should start background translation
        true
    }

    /// Cancels translation settings.
    pub fn cancel_translation_settings(&mut self) {
        self.mode = AppMode::Normal;
    }

    /// Moves selection up in translation settings.
    pub fn translation_settings_up(&mut self) {
        if self.translation_settings.selected_code_index > 0 {
            self.translation_settings.selected_code_index -= 1;
            // Adjust scroll if needed
            if self.translation_settings.selected_code_index < self.translation_settings.scroll_offset {
                self.translation_settings.scroll_offset = self.translation_settings.selected_code_index;
            }
        }
    }

    /// Moves selection down in translation settings.
    pub fn translation_settings_down(&mut self) {
        use crate::genetic_code::GeneticCodes;
        let codes = GeneticCodes::new();
        let max_index = codes.all().len().saturating_sub(1);
        if self.translation_settings.selected_code_index < max_index {
            self.translation_settings.selected_code_index += 1;
        }
    }

    /// Cycles frame selection left in translation settings.
    pub fn translation_settings_frame_left(&mut self) {
        if self.translation_settings.selected_frame > 0 {
            self.translation_settings.selected_frame -= 1;
        }
    }

    /// Cycles frame selection right in translation settings.
    pub fn translation_settings_frame_right(&mut self) {
        if self.translation_settings.selected_frame < 2 {
            self.translation_settings.selected_frame += 1;
        }
    }

    /// Shows help information.
    fn show_help(&mut self) {
        self.show_help = !self.show_help;
    }

    /// Toggles help off (for any key press while help is shown).
    pub fn dismiss_help(&mut self) {
        self.show_help = false;
    }

    /// Navigates to the next help tab.
    pub fn help_next_tab(&mut self) {
        self.help_tab = self.help_tab.next();
    }

    /// Navigates to the previous help tab.
    pub fn help_prev_tab(&mut self) {
        self.help_tab = self.help_tab.prev();
    }

    /// Clears any pending key state.
    fn clear_pending(&mut self) {
        self.pending_g = false;
        self.pending_z = false;
        self.number_buffer.clear();
    }

    /// Sets the pending 'g' state for g-commands.
    pub fn set_pending_g(&mut self) {
        self.pending_g = true;
        self.pending_z = false;
        self.number_buffer.clear();
    }

    /// Sets the pending 'z' state for z-commands.
    pub fn set_pending_z(&mut self) {
        self.pending_z = true;
        self.pending_g = false;
        self.number_buffer.clear();
    }

    /// Accumulates a digit for the number prefix.
    pub fn accumulate_digit(&mut self, c: char) {
        self.pending_g = false;
        self.pending_z = false;
        self.number_buffer.push(c);
    }

    /// Executes goto column with the accumulated number.
    pub fn execute_goto_column(&mut self) {
        if let Ok(col) = self.number_buffer.parse::<usize>() {
            self.goto_column(col);
        }
        self.clear_pending();
    }

    /// Goes to the first column (0).
    pub fn goto_first_column(&mut self) {
        self.clear_pending();
        self.cursor.col = 0;
        self.ensure_cursor_visible();
    }

    /// Goes to the last column (end of alignment).
    pub fn goto_last_column(&mut self) {
        self.clear_pending();
        let aln_len = self.active_alignment().alignment_length();
        if aln_len > 0 {
            self.cursor.col = aln_len - 1;
            self.ensure_cursor_visible();
        }
    }

    /// Goes to the first visible column (g0).
    pub fn goto_first_visible_column(&mut self) {
        self.clear_pending();
        self.cursor.col = self.viewport.first_col;
    }

    /// Goes to the middle of the visible area (gm).
    pub fn goto_middle_visible_column(&mut self) {
        self.clear_pending();
        let middle = self.viewport.first_col + self.viewport.visible_cols / 2;
        self.cursor.col = middle.min(self.active_alignment().alignment_length().saturating_sub(1));
    }

    /// Goes to the last visible column (g$).
    pub fn goto_last_visible_column(&mut self) {
        self.clear_pending();
        let last_visible = self.viewport.first_col + self.viewport.visible_cols.saturating_sub(1);
        self.cursor.col = last_visible.min(self.active_alignment().alignment_length().saturating_sub(1));
    }

    /// Goes to a specific column (1-indexed for user).
    pub fn goto_column(&mut self, col: usize) {
        self.clear_pending();
        let aln_len = self.active_alignment().alignment_length();
        if col > 0 && col <= aln_len {
            self.cursor.col = col - 1; // 1-indexed for user
            self.ensure_cursor_visible();
            self.status_message = None; // Clear any previous error
        } else if col == 0 {
            self.cursor.col = 0;
            self.ensure_cursor_visible();
            self.status_message = None; // Clear any previous error
        } else {
            self.status_message = Some(format!("Invalid column: {}", col));
        }
    }

    /// Handles a g-command key.
    pub fn handle_g_command(&mut self, c: char) {
        self.pending_g = false;
        match c {
            '0' => self.goto_first_visible_column(),
            'm' => self.goto_middle_visible_column(),
            '$' => self.goto_last_visible_column(),
            _ => {} // Unknown g-command, ignore
        }
    }

    /// Cancels command mode and returns to normal mode.
    pub fn cancel_command(&mut self) {
        self.mode = AppMode::Normal;
    }

    /// Enters search mode.
    pub fn enter_search_mode(&mut self, backward: bool) {
        if backward {
            self.mode = AppMode::SearchBackward(String::new());
        } else {
            self.mode = AppMode::Search(String::new());
        }
    }

    /// Handles a character input in search mode.
    pub fn search_input(&mut self, c: char) {
        match &mut self.mode {
            AppMode::Search(ref mut pattern) | AppMode::SearchBackward(ref mut pattern) => {
                pattern.push(c);
            }
            _ => {}
        }
    }

    /// Handles backspace in search mode.
    pub fn search_backspace(&mut self) {
        match &mut self.mode {
            AppMode::Search(ref mut pattern) | AppMode::SearchBackward(ref mut pattern) => {
                pattern.pop();
                if pattern.is_empty() {
                    self.mode = AppMode::Normal;
                }
            }
            _ => {}
        }
    }

    /// Executes the current search.
    pub fn execute_search(&mut self) {
        let (pattern, backward) = match &self.mode {
            AppMode::Search(p) => (p.clone(), false),
            AppMode::SearchBackward(p) => (p.clone(), true),
            _ => return,
        };

        if pattern.is_empty() {
            self.mode = AppMode::Normal;
            return;
        }

        self.last_search = Some(pattern.clone());
        self.last_search_backward = backward;
        self.mode = AppMode::Normal;

        // Perform the search
        if backward {
            self.search_backward(&pattern);
        } else {
            self.search_forward(&pattern);
        }
    }

    /// Cancels search mode and returns to normal mode.
    pub fn cancel_search(&mut self) {
        self.mode = AppMode::Normal;
    }

    /// Finds the next match (n key).
    pub fn find_next(&mut self) {
        if let Some(pattern) = self.last_search.clone() {
            if self.last_search_backward {
                self.search_backward(&pattern);
            } else {
                self.search_forward(&pattern);
            }
        } else {
            self.status_message = Some("No previous search pattern".to_string());
        }
    }

    /// Finds the previous match (N key).
    pub fn find_previous(&mut self) {
        if let Some(pattern) = self.last_search.clone() {
            // Reverse the direction
            if self.last_search_backward {
                self.search_forward(&pattern);
            } else {
                self.search_backward(&pattern);
            }
        } else {
            self.status_message = Some("No previous search pattern".to_string());
        }
    }

    /// Searches forward from current position (right then down).
    /// Searches both sequence data and sequence names.
    fn search_forward(&mut self, pattern: &str) {
        let pattern_upper = pattern.to_uppercase();
        let start_row = self.cursor.row;
        let start_col = self.cursor.col;
        let alignment = self.active_alignment();
        let num_rows = alignment.sequence_count();
        let num_cols = alignment.alignment_length();

        if num_rows == 0 || num_cols == 0 {
            self.status_message = Some(format!("Pattern not found: {}", pattern));
            return;
        }

        // Search from current position to end of current row (sequence data only)
        let alignment = self.active_alignment();
        if let Some(seq) = alignment.get(start_row) {
            let search_start = start_col + 1; // Start after current position
            if search_start < seq.len() {
                let seq_str = seq.as_str();
                if let Some(pos) = seq_str[search_start..].to_uppercase().find(&pattern_upper) {
                    self.cursor.col = search_start + pos;
                    self.ensure_cursor_visible();
                    self.status_message = Some(format!("/{}", pattern));
                    return;
                }
            }
        }

        // Search remaining rows (name first, then data)
        for row_offset in 1..=num_rows {
            let row = (start_row + row_offset) % num_rows;
            let alignment = self.active_alignment();
            if let Some(seq) = alignment.get(row) {
                // First check sequence name
                if seq.id.to_uppercase().contains(&pattern_upper) {
                    self.cursor.row = row;
                    self.cursor.col = 0; // Position at start when matching name
                    self.ensure_cursor_visible();
                    let wrapped = row < start_row || (row == start_row);
                    if wrapped {
                        self.status_message = Some(format!("/{} (name, wrapped)", pattern));
                    } else {
                        self.status_message = Some(format!("/{} (name)", pattern));
                    }
                    return;
                }
                // Then check sequence data
                if let Some(pos) = seq.as_str().to_uppercase().find(&pattern_upper) {
                    self.cursor.row = row;
                    self.cursor.col = pos;
                    self.ensure_cursor_visible();
                    if row < start_row || (row == start_row && pos <= start_col) {
                        self.status_message = Some(format!("/{} (wrapped)", pattern));
                    } else {
                        self.status_message = Some(format!("/{}", pattern));
                    }
                    return;
                }
            }
        }

        self.status_message = Some(format!("Pattern not found: {}", pattern));
    }

    /// Searches backward from current position (left then up).
    /// Searches both sequence data and sequence names.
    fn search_backward(&mut self, pattern: &str) {
        let pattern_upper = pattern.to_uppercase();
        let start_row = self.cursor.row;
        let start_col = self.cursor.col;
        let alignment = self.active_alignment();
        let num_rows = alignment.sequence_count();
        let num_cols = alignment.alignment_length();

        if num_rows == 0 || num_cols == 0 {
            self.status_message = Some(format!("Pattern not found: {}", pattern));
            return;
        }

        // Search from current position backward in current row (data only)
        let alignment = self.active_alignment();
        if let Some(seq) = alignment.get(start_row) {
            if start_col > 0 {
                let search_area = &seq.as_str()[..start_col];
                if let Some(pos) = search_area.to_uppercase().rfind(&pattern_upper) {
                    self.cursor.col = pos;
                    self.ensure_cursor_visible();
                    self.status_message = Some(format!("?{}", pattern));
                    return;
                }
            }
            // Check current row's name if we're past position 0
            if start_col > 0 && seq.id.to_uppercase().contains(&pattern_upper) {
                self.cursor.col = 0;
                self.ensure_cursor_visible();
                self.status_message = Some(format!("?{} (name)", pattern));
                return;
            }
        }

        // Search previous rows (data first from end, then name)
        for row_offset in 1..=num_rows {
            let row = (start_row + num_rows - row_offset) % num_rows;
            let alignment = self.active_alignment();
            if let Some(seq) = alignment.get(row) {
                // First check sequence data (from end)
                if let Some(pos) = seq.as_str().to_uppercase().rfind(&pattern_upper) {
                    self.cursor.row = row;
                    self.cursor.col = pos;
                    self.ensure_cursor_visible();
                    if row > start_row || (row == start_row && pos >= start_col) {
                        self.status_message = Some(format!("?{} (wrapped)", pattern));
                    } else {
                        self.status_message = Some(format!("?{}", pattern));
                    }
                    return;
                }
                // Then check sequence name
                if seq.id.to_uppercase().contains(&pattern_upper) {
                    self.cursor.row = row;
                    self.cursor.col = 0;
                    self.ensure_cursor_visible();
                    let wrapped = row > start_row || (row == start_row);
                    if wrapped {
                        self.status_message = Some(format!("?{} (name, wrapped)", pattern));
                    } else {
                        self.status_message = Some(format!("?{} (name)", pattern));
                    }
                    return;
                }
            }
        }

        self.status_message = Some(format!("Pattern not found: {}", pattern));
    }
}

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

    #[test]
    fn test_sequence_creation() {
        let seq = Sequence::new("seq1", "ACGT");
        assert_eq!(seq.id, "seq1");
        assert_eq!(seq.as_str(), "ACGT");
        assert_eq!(seq.len(), 4);
    }

    #[test]
    fn test_sequence_char_at() {
        let seq = Sequence::new("seq1", "ACGT");
        assert_eq!(seq.char_at(0), Some('A'));
        assert_eq!(seq.char_at(3), Some('T'));
        assert_eq!(seq.char_at(4), None);
    }

    #[test]
    fn test_alignment_valid() {
        let seqs = vec![
            Sequence::new("seq1", "ACGT"),
            Sequence::new("seq2", "TGCA"),
        ];
        let alignment = Alignment::new(seqs);
        assert!(alignment.is_valid_alignment);
        assert!(alignment.warning.is_none());
        assert_eq!(alignment.alignment_length(), 4);
    }

    #[test]
    fn test_alignment_invalid() {
        let seqs = vec![
            Sequence::new("seq1", "ACGT"),
            Sequence::new("seq2", "TG"),
        ];
        let alignment = Alignment::new(seqs);
        assert!(!alignment.is_valid_alignment);
        assert!(alignment.warning.is_some());
    }

    #[test]
    fn test_viewport_range() {
        let vp = Viewport::new(10, 20);
        assert_eq!(vp.row_range(), 0..10);
        assert_eq!(vp.col_range(), 0..20);
    }

    #[test]
    fn test_sequence_type_nucleotide() {
        let seqs = vec![
            Sequence::new("seq1", "ACGTACGT"),
            Sequence::new("seq2", "TGCA-TGC"),
        ];
        let alignment = Alignment::new(seqs);
        assert!(alignment.sequence_type.is_nucleotide());
        assert!(alignment.sequence_type.nt_ratio > 0.8);
    }

    #[test]
    fn test_sequence_type_amino_acid() {
        let seqs = vec![
            Sequence::new("seq1", "MKFLILLFNILCLFPVLAADNHGVGPQGAS"),
            Sequence::new("seq2", "MKWVTFISLLFLFSSAYSRGVFRRDAHKSE"),
        ];
        let alignment = Alignment::new(seqs);
        assert!(alignment.sequence_type.is_amino_acid());
        assert!(alignment.sequence_type.is_likely_not_nucleotide());
    }

    #[test]
    fn test_sequence_type_with_gaps() {
        // Amino acid sequence with gaps should still be detected
        let seqs = vec![
            Sequence::new("seq1", "MKF---LILLFNILCLFPVL"),
            Sequence::new("seq2", "MKW---VTFISLLFLFSSAY"),
        ];
        let alignment = Alignment::new(seqs);
        assert!(alignment.sequence_type.is_amino_acid());
    }

    #[test]
    fn test_cursor_movement() {
        let seqs = vec![
            Sequence::new("seq1", "ACGTACGT"),
            Sequence::new("seq2", "TGCATGCA"),
            Sequence::new("seq3", "AAAAAAAA"),
        ];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());
        state.update_viewport_size(3, 4);

        // Initial position
        assert_eq!(state.cursor.row, 0);
        assert_eq!(state.cursor.col, 0);

        // Move down
        state.move_down();
        assert_eq!(state.cursor.row, 1);

        // Move right
        state.move_right();
        assert_eq!(state.cursor.col, 1);

        // Move up
        state.move_up();
        assert_eq!(state.cursor.row, 0);

        // Move left
        state.move_left();
        assert_eq!(state.cursor.col, 0);

        // Boundary: can't go past 0
        state.move_up();
        assert_eq!(state.cursor.row, 0);
        state.move_left();
        assert_eq!(state.cursor.col, 0);
    }

    #[test]
    fn test_help_command() {
        let seqs = vec![Sequence::new("seq1", "ACGT")];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());

        // Initially help is not shown
        assert!(!state.show_help);

        // Enter command mode and type 'h'
        state.enter_command_mode();
        state.command_input('h');
        state.execute_command();

        // Should toggle help overlay on
        assert!(state.show_help);

        // Mode should return to normal
        assert_eq!(state.mode, AppMode::Normal);

        // Dismiss help
        state.dismiss_help();
        assert!(!state.show_help);
    }

    #[test]
    fn test_search_forward() {
        let seqs = vec![
            Sequence::new("seq1", "ACGTACGT"),
            Sequence::new("seq2", "TGCATGCA"),
            Sequence::new("seq3", "AAAAGAAA"),
        ];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());
        state.update_viewport_size(3, 8);

        // Search for "TGC" starting from (0,0)
        state.enter_search_mode(false);
        state.search_input('T');
        state.search_input('G');
        state.search_input('C');
        state.execute_search();

        // Should find in second row
        assert_eq!(state.cursor.row, 1);
        assert_eq!(state.cursor.col, 0);

        // Search for "GAA" - should find in third row
        state.enter_search_mode(false);
        state.search_input('G');
        state.search_input('A');
        state.search_input('A');
        state.execute_search();

        assert_eq!(state.cursor.row, 2);
        assert_eq!(state.cursor.col, 4);
    }

    #[test]
    fn test_search_backward() {
        let seqs = vec![
            Sequence::new("seq1", "ACGTACGT"),
            Sequence::new("seq2", "TGCATGCA"),
            Sequence::new("seq3", "AAAAGAAA"),
        ];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());
        state.update_viewport_size(3, 8);

        // Move to last row, last column
        state.cursor.row = 2;
        state.cursor.col = 7;

        // Search backward for "TGC"
        state.enter_search_mode(true);
        state.search_input('T');
        state.search_input('G');
        state.search_input('C');
        state.execute_search();

        // Should find last occurrence in second row (at position 4)
        assert_eq!(state.cursor.row, 1);
        assert_eq!(state.cursor.col, 4);
    }

    #[test]
    fn test_search_case_insensitive() {
        let seqs = vec![
            Sequence::new("seq1", "ACGTACGT"),
            Sequence::new("seq2", "tgcatgca"),
        ];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());
        state.update_viewport_size(2, 8);

        // Search for lowercase pattern in uppercase sequence
        state.enter_search_mode(false);
        state.search_input('c');
        state.search_input('g');
        state.search_input('t');
        state.execute_search();

        assert_eq!(state.cursor.row, 0);
        assert_eq!(state.cursor.col, 1);
    }

    #[test]
    fn test_find_next_and_previous() {
        let seqs = vec![
            Sequence::new("seq1", "ACGTACGT"),
            Sequence::new("seq2", "ACGTACGT"),
        ];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());
        state.update_viewport_size(2, 8);

        // Search for "CGT"
        state.enter_search_mode(false);
        state.search_input('C');
        state.search_input('G');
        state.search_input('T');
        state.execute_search();

        // First match at (0, 1)
        assert_eq!(state.cursor.row, 0);
        assert_eq!(state.cursor.col, 1);

        // Find next - should find at (0, 5)
        state.find_next();
        assert_eq!(state.cursor.row, 0);
        assert_eq!(state.cursor.col, 5);

        // Find next - should find in second row at (1, 1)
        state.find_next();
        assert_eq!(state.cursor.row, 1);
        assert_eq!(state.cursor.col, 1);

        // Find previous - should go back to (0, 5)
        state.find_previous();
        assert_eq!(state.cursor.row, 0);
        assert_eq!(state.cursor.col, 5);
    }

    #[test]
    fn test_search_not_found() {
        let seqs = vec![
            Sequence::new("seq1", "ACGTACGT"),
        ];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());
        state.update_viewport_size(1, 8);

        state.enter_search_mode(false);
        state.search_input('X');
        state.search_input('Y');
        state.search_input('Z');
        state.execute_search();

        // Cursor should not move
        assert_eq!(state.cursor.row, 0);
        assert_eq!(state.cursor.col, 0);
        assert!(state.status_message.as_ref().unwrap().contains("not found"));
    }

    #[test]
    fn test_search_by_sequence_name() {
        let seqs = vec![
            Sequence::new("Human_BRCA1", "ACGTACGT"),
            Sequence::new("Mouse_BRCA1", "TGCATGCA"),
            Sequence::new("Chicken_BRCA2", "AAAAGAAA"),
        ];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());
        state.update_viewport_size(3, 8);

        // Search for "Mouse" - should find by sequence name
        state.enter_search_mode(false);
        state.search_input('M');
        state.search_input('o');
        state.search_input('u');
        state.search_input('s');
        state.search_input('e');
        state.execute_search();

        assert_eq!(state.cursor.row, 1);
        assert_eq!(state.cursor.col, 0);
        assert!(state.status_message.as_ref().unwrap().contains("name"));

        // Search for "BRCA2" - should find Chicken sequence
        state.enter_search_mode(false);
        state.search_input('B');
        state.search_input('R');
        state.search_input('C');
        state.search_input('A');
        state.search_input('2');
        state.execute_search();

        assert_eq!(state.cursor.row, 2);
        assert_eq!(state.cursor.col, 0);
        assert!(state.status_message.as_ref().unwrap().contains("name"));
    }

    #[test]
    fn test_search_name_case_insensitive() {
        let seqs = vec![
            Sequence::new("Human_Gene", "ACGTACGT"),
            Sequence::new("mouse_gene", "TGCATGCA"),
        ];
        let alignment = Alignment::new(seqs);
        let mut state = AppState::new(alignment, "test".to_string());
        state.update_viewport_size(2, 8);

        // Search for lowercase "human" should match "Human_Gene"
        state.enter_search_mode(false);
        state.search_input('h');
        state.search_input('u');
        state.search_input('m');
        state.search_input('a');
        state.search_input('n');
        state.execute_search();

        // Should wrap around and find Human (row 0)
        assert_eq!(state.cursor.row, 0);
        assert!(state.status_message.as_ref().unwrap().contains("name"));
    }
}