keeper-secrets-manager-core 17.1.0

Rust SDK for Keeper Secrets Manager
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
// -*- coding: utf-8 -*-
//  _  __
// | |/ /___ ___ _ __  ___ _ _ (R)
// | ' </ -_) -_) '_ \/ -_) '_|
// |_|\_\___\___| .__/\___|_|
//              |_|
//
// Keeper Secrets Manager
// Copyright 2024 Keeper Security Inc.
// Contact: sm@keepersecurity.com
//

use base64::{engine::general_purpose::STANDARD, Engine as _};
use chrono::DateTime;
use log::{error, info};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
    collections::HashMap,
    fmt::{self},
    fs::{self, File},
    io::{Read, Write as _},
    path::{Path, PathBuf},
};

use crate::{
    crypto::{unpad_data, CryptoUtils},
    custom_error::KSMRError,
    enums::ValueResult,
    utils::{self, json_to_dict},
};

use super::field_structs::KeeperField;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Record {
    pub record_key_bytes: Vec<u8>,
    pub uid: String,
    pub title: String,
    pub record_type: String,
    pub files: Vec<KeeperFile>,
    pub raw_json: String,
    pub record_dict: HashMap<String, Value>,
    pub password: Option<String>,
    pub revision: Option<i64>,
    pub is_editable: bool,
    pub folder_uid: String,
    pub folder_key_bytes: Option<Vec<u8>>,
    pub inner_folder_uid: Option<String>,
    pub links: Vec<HashMap<String, Value>>, // GraphSync linked records (v16.7.0+)
}

impl Record {
    pub fn new(
        record_dict: &HashMap<String, Value>,
        secret_key: Vec<u8>,
        folder_uid: Option<String>,
    ) -> Result<Self, KSMRError> {
        let uid = record_dict
            .get("recordUid")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string();
        let folder_uid = folder_uid.unwrap_or_default();
        let mut record_key_bytes = Vec::new();
        secret_key.clone_into(&mut record_key_bytes);
        let record_key_encrypted_bytes = record_dict
            .get("recordKey")
            .and_then(Value::as_str)
            .map(|s| STANDARD.decode(s).unwrap_or_default());

        if let Some(encrypted_bytes) = record_key_encrypted_bytes {
            record_key_bytes = CryptoUtils::decrypt_aes(&encrypted_bytes, &secret_key).unwrap();
        }

        let record_encrypted_data_value = record_dict.get("data").and_then(Value::as_str);

        let raw_json = record_encrypted_data_value
            .ok_or_else(|| KSMRError::DecodeError("cannot decrypt record".to_string()))?;

        let raw_json_string = CryptoUtils::decrypt_record(raw_json.as_bytes(), &record_key_bytes)?;

        let record_dict: HashMap<String, Value> = serde_json::from_str(&raw_json_string)
            .map_err(|e| KSMRError::SerializationError(e.to_string()))?;

        // Title and type
        let title = record_dict
            .get("title")
            .and_then(Value::as_str)
            .map(String::from)
            .unwrap_or_default();
        let record_type = record_dict
            .get("type")
            .and_then(Value::as_str)
            .map(String::from)
            .unwrap_or_default();

        let revision = record_dict.get("revision").and_then(Value::as_i64);
        let is_editable = record_dict
            .get("isEditable")
            .and_then(Value::as_bool)
            .unwrap_or(false);

        let mut files = Vec::new();
        if let Some(file_list) = record_dict.get("files").and_then(Value::as_array) {
            for file_data in file_list {
                if let Some(file_map) = file_data.as_object() {
                    let file_map_hashmap: HashMap<String, Value> = file_map
                        .clone()
                        .into_iter()
                        .map(|(k, v)| (k.clone(), v.clone()))
                        .collect();

                    let created_keeper_file =
                        KeeperFile::new_from_json(file_map_hashmap, record_key_bytes.clone());
                    match created_keeper_file {
                        Ok(file) => files.push(file),
                        Err(e) => {
                            let msg = format!("Error loading file: {}", e);
                            eprintln!("{}", msg);
                        }
                    }
                }
            }
        }

        let password = if record_type == "login" {
            record_dict
                .get("fields")
                .and_then(|fields| fields.as_array())
                .and_then(|fields| {
                    fields
                        .iter()
                        .find(|field| {
                            field.get("type") == Some(&Value::String("password".to_string()))
                        })
                        .and_then(|field| field.get("value"))
                        .and_then(|value| value.as_array())
                        .and_then(|arr| arr.first())
                        .and_then(Value::as_str)
                })
                .map(String::from)
        } else {
            None
        };

        // Parse links array (GraphSync linked records)
        let links = record_dict
            .get("links")
            .and_then(Value::as_array)
            .map(|arr| {
                arr.iter()
                    .filter_map(|link| {
                        link.as_object().map(|obj| {
                            obj.iter()
                                .map(|(k, v)| (k.clone(), v.clone()))
                                .collect::<HashMap<String, Value>>()
                        })
                    })
                    .collect()
            })
            .unwrap_or_default();

        Ok(Self {
            uid,
            title,
            record_type,
            files,
            raw_json: raw_json_string,
            record_dict: record_dict.clone(),
            password,
            revision,
            is_editable,
            folder_uid,
            inner_folder_uid: record_dict
                .get("innerFolderUid")
                .and_then(Value::as_str)
                .map(|s| s.to_string()),
            record_key_bytes,
            folder_key_bytes: None,
            links,
        })
    }

    /// Finds a file by title within the Record's files.
    pub fn find_file_by_title(
        &mut self,
        title: &str,
    ) -> Result<Option<&mut KeeperFile>, KSMRError> {
        Ok(self
            .files
            .iter_mut()
            .find(|file: &&mut KeeperFile| file.title == *title))
    }

    pub fn update(&mut self) -> Result<(), KSMRError> {
        // Update the title and type in the record_dict HashMap
        self.record_dict
            .insert("title".to_string(), Value::String(self.title.clone()));
        self.record_dict.insert(
            "record_type".to_string(),
            Value::String(self.record_type.clone()),
        );

        // Find the password field in fields, and update the password attribute if it exists
        if let Some(fields) = self
            .record_dict
            .get_mut("fields")
            .and_then(|f| f.as_array_mut())
        {
            if let Some(password_field) = fields
                .iter_mut()
                .find(|field| field.get("record_type").and_then(|t| t.as_str()) == Some("password"))
            {
                if let Some(values) = password_field.get("value").and_then(|v| v.as_array()) {
                    if let Some(Value::String(password)) = values.first() {
                        self.password = Some(password.clone());
                    }
                }
            }
        }

        self.raw_json = serde_json::to_string(&self.record_dict).map_err(|_| {
            KSMRError::SerializationError("Failed to serialize record_dict".to_string())
        })?;

        Ok(())
    }

    pub fn _value(&self, values: Option<Vec<&[Value]>>, single: bool) -> ValueResult {
        if single {
            let first_value = values
                .and_then(|v| v.first().cloned())
                .map(|v| v.to_owned());
            ValueResult::Single(first_value)
        } else {
            let all_values = values
                .map(|v| v.iter().map(|s| s.to_vec()).collect())
                .unwrap_or_default();
            ValueResult::Multiple(all_values)
        }
    }

    fn field_search(
        mut fields: Vec<HashMap<String, Value>>,
        field_key: &str,
    ) -> Option<Vec<HashMap<String, Value>>> {
        let mut fields_returned: Vec<HashMap<String, Value>> = Vec::new();
        // Check for a matching "label" key first
        for field in fields.clone().drain(..) {
            if let Some(item_label) = field.get("label").and_then(Value::as_str) {
                if item_label.eq(field_key) {
                    fields_returned.push(field.clone());
                }
            }
        }
        if !fields_returned.is_empty() {
            return Some(fields_returned.clone());
        }
        // Search for a matching "type" key
        for field in fields.drain(..) {
            if let Some(item_type) = field.get("type").and_then(Value::as_str) {
                if item_type.eq_ignore_ascii_case(field_key) {
                    fields_returned.push(field.clone());
                }
            }
        }
        if !fields_returned.is_empty() {
            return Some(fields_returned.clone());
        }
        None
    }

    // Retrieve a standard field by field type.
    pub fn get_standard_field(&self, field_type: &str) -> Result<Vec<Value>, KSMRError> {
        let fields_searched = self.standard_fields_searched_map(field_type)?;

        let mut fields_searched_map: Vec<Value> = Vec::new();
        for field_searched in fields_searched.clone() {
            let field_searched_mapped: Value = field_searched
                .get("value") //.into_iter().map(|field| field.get("value").cloned().unwrap_or(Value::Null))
                .ok_or_else(|| {
                    KSMRError::RecordDataError(format!("Field {} not found in record", field_type))
                })?
                .clone();
            fields_searched_map.push(field_searched_mapped);
        }

        Ok(fields_searched_map)
    }

    pub fn standard_fields_searched_map(
        &self,
        field_type: &str,
    ) -> Result<Vec<HashMap<String, Value>>, KSMRError> {
        let fields_2 = self.record_dict.get("fields");
        let fields = fields_2.and_then(Value::as_array).ok_or_else(|| {
            KSMRError::RecordDataError(format!(
                "Cannot find standard field {} in record",
                field_type
            ))
        })?;

        // Parse each `Value` into its specific type
        #[allow(clippy::unnecessary_filter_map)]
        let fields_2: Vec<HashMap<String, Value>> = fields
            .iter()
            .filter_map(|value| match value {
                Value::Object(map) => Some(map.clone().into_iter().collect()),
                Value::Array(arr) => Some(
                    arr.iter()
                        .enumerate()
                        .map(|(i, v)| (i.to_string(), v.clone()))
                        .collect(),
                ),
                Value::String(s) => Some(
                    [("string".to_string(), Value::String(s.clone()))]
                        .into_iter()
                        .collect(),
                ),
                Value::Number(num) => Some(
                    [("number".to_string(), Value::Number(num.clone()))]
                        .into_iter()
                        .collect(),
                ),
                Value::Bool(b) => Some(
                    [("bool".to_string(), Value::Bool(*b))]
                        .into_iter()
                        .collect(),
                ),
                Value::Null => Some(HashMap::new()),
            })
            .collect();

        let fields_searched: Vec<HashMap<String, Value>> =
            match Self::field_search(fields_2, field_type) {
                Some(field) => field,
                None => {
                    return Err(KSMRError::RecordDataError(format!(
                        "Field {} not found in record",
                        field_type
                    )))
                }
            };
        Ok(fields_searched)
    }
    // Retrieve the standard field value by type, either as a single value or an array of values.
    pub fn get_standard_field_value(
        &self,
        field_type: &str,
        single: bool,
    ) -> Result<Value, KSMRError> {
        let fields = self.get_standard_field(field_type)?;
        let mut arrayed_values: Vec<Vec<Value>> = Vec::new();

        for field in fields {
            let arrayed_val = field.as_array().cloned().unwrap();
            arrayed_values.push(arrayed_val);
        }

        if arrayed_values.is_empty() || arrayed_values[0].is_empty() {
            return Err(KSMRError::RecordDataError(format!(
                "No standard field with field type: {} exists on record: {}",
                field_type, self.title
            )));
        }

        // Use `_value` to return a single value or an array, based on `single` parameter
        match self._value(
            Some(arrayed_values.iter().map(|v| v.as_slice()).collect()),
            single,
        ) {
            ValueResult::Single(Some(value)) => Ok(value[0].clone()),
            ValueResult::Single(None) => Ok(Value::Null),
            ValueResult::Multiple(values) => {
                // Flatten the 2D array to 1D
                let flat: Vec<Value> = values.into_iter().flatten().collect();
                Ok(Value::Array(flat))
            }
        }
    }

    pub fn get_standard_field_mut(&mut self, field_type: &str) -> Result<&mut Value, KSMRError> {
        // Get mutable reference to "fields"
        let fields = self
            .record_dict
            .get_mut("fields")
            .and_then(Value::as_array_mut)
            .ok_or_else(|| {
                KSMRError::RecordDataError(format!(
                    "Cannot find standard field {} in record",
                    field_type
                ))
            })?;

        // Find the field by "label" or "type"
        let retrieved_field = fields
            .iter_mut()
            .find(|field| {
                field.get("label").and_then(Value::as_str) == Some(field_type)
                    || field
                        .get("type")
                        .and_then(Value::as_str)
                        .map(|t| t.eq_ignore_ascii_case(field_type))
                        .unwrap_or(false)
            })
            .ok_or_else(|| {
                KSMRError::RecordDataError(format!("Field {} not found in record", field_type))
            });

        retrieved_field
    }

    /// Set a standard field's value
    pub fn set_standard_field_value_mut(
        &mut self,
        field_type: &str,
        value: Value,
    ) -> Result<(), KSMRError> {
        // Get a mutable reference to the field
        let field = self.get_standard_field_mut(field_type)?;

        // Ensure the field is an object and update the "value" key
        let field_obj = field.as_object_mut().ok_or_else(|| {
            KSMRError::RecordDataError(format!(
                "Expected an object for standard field {} in record",
                field_type
            ))
        })?;

        match value.is_array() {
            true => {
                field_obj.insert("value".to_string(), value);
            }
            false => {
                field_obj.insert("value".to_string(), [value].into());
            }
        }
        // Update the "value" field
        self.update()?;
        Ok(())
    }

    // Retrieve a custom field by field type.
    pub fn get_custom_field(&self, field_type: &str) -> Result<Vec<Value>, KSMRError> {
        let fields_2 = self.record_dict.get("custom");

        let fields = fields_2.and_then(Value::as_array).ok_or_else(|| {
            KSMRError::RecordDataError(format!("Cannot find custom field {} in record", field_type))
        })?;

        // Parse each `Value` into its specific type
        #[allow(clippy::unnecessary_filter_map)]
        let fields_2: Vec<HashMap<String, Value>> = fields
            .iter()
            .filter_map(|value| match value {
                Value::Object(map) => Some(map.clone().into_iter().collect()),
                Value::Array(arr) => Some(
                    arr.iter()
                        .enumerate()
                        .map(|(i, v)| (i.to_string(), v.clone()))
                        .collect(),
                ),
                Value::String(s) => Some(
                    [("string".to_string(), Value::String(s.clone()))]
                        .into_iter()
                        .collect(),
                ),
                Value::Number(num) => Some(
                    [("number".to_string(), Value::Number(num.clone()))]
                        .into_iter()
                        .collect(),
                ),
                Value::Bool(b) => Some(
                    [("bool".to_string(), Value::Bool(*b))]
                        .into_iter()
                        .collect(),
                ),
                Value::Null => Some(HashMap::new()),
            })
            .collect();

        let fields_searched = match Self::field_search(fields_2, field_type) {
            Some(field) => field,
            None => {
                return Err(KSMRError::RecordDataError(format!(
                    "Field {} not found in record",
                    field_type
                )))
            }
        };

        let mut fields_searched_map: Vec<Value> = Vec::new();
        for field_searched in fields_searched.clone() {
            let field_searched_mapped: Value = field_searched
                .get("value")
                .ok_or_else(|| {
                    KSMRError::RecordDataError(format!("Field {} not found in record", field_type))
                })?
                .clone();
            fields_searched_map.push(field_searched_mapped);
        }

        Ok(fields_searched_map)
    }

    // Retrieve the custom field value by type, either as a single value or an array of values.
    pub fn get_custom_field_value(
        &self,
        field_type: &str,
        single: bool,
    ) -> Result<Value, KSMRError> {
        let fields = self.get_custom_field(field_type)?;
        let mut arrayed_values: Vec<Vec<Value>> = Vec::new();

        for field in fields {
            let arrayed_val = field.as_array().cloned().unwrap();
            arrayed_values.push(arrayed_val);
        }

        // Use `_value` to return a single value or an array, based on `single` parameter
        match self._value(
            Some(arrayed_values.iter().map(|v| v.as_slice()).collect()),
            single,
        ) {
            ValueResult::Single(Some(value)) => Ok(value[0].clone()),
            ValueResult::Single(None) => Ok(Value::Null),
            ValueResult::Multiple(values) => {
                // Flatten the 2D array to 1D
                let flat: Vec<Value> = values.into_iter().flatten().collect();
                Ok(Value::Array(flat))
            }
        }
    }

    pub fn get_custom_field_mut(&mut self, field_type: &str) -> Result<&mut Value, KSMRError> {
        // Get mutable reference to "fields"
        let fields = self
            .record_dict
            .get_mut("custom")
            .and_then(Value::as_array_mut)
            .ok_or_else(|| {
                KSMRError::RecordDataError(format!(
                    "Cannot find standard field {} in record",
                    field_type
                ))
            })?;

        // Find the field by "label" or "type"
        let retrieved_field = fields
            .iter_mut()
            .find(|field| {
                field.get("label").and_then(Value::as_str) == Some(field_type)
                    || field
                        .get("type")
                        .and_then(Value::as_str)
                        .map(|t| t.eq_ignore_ascii_case(field_type))
                        .unwrap_or(false)
            })
            .ok_or_else(|| {
                KSMRError::RecordDataError(format!("Field {} not found in record", field_type))
            });

        retrieved_field
    }

    /// Set a standard field's value
    pub fn set_custom_field_value_mut(
        &mut self,
        field_type: &str,
        value: Value,
    ) -> Result<(), KSMRError> {
        // Get a mutable reference to the field
        let field = self.get_custom_field_mut(field_type)?;

        // Ensure the field is an object and update the "value" key
        let field_obj = field.as_object_mut().ok_or_else(|| {
            KSMRError::RecordDataError(format!(
                "Expected an object for standard field {} in record",
                field_type
            ))
        })?;

        // Update the "value" field
        field_obj.insert("value".to_string(), [value].into());
        self.update()?;
        Ok(())
    }

    pub fn new_from_json(
        record_dict: HashMap<String, serde_json::Value>,
        secret_key: &[u8],
        folder_uid: Option<String>,
    ) -> Result<Self, KSMRError> {
        let mut record = Record::default();

        // Record UID - Extract early for error logging
        if let Some(uid) = record_dict.get("recordUid").and_then(|v| v.as_str()) {
            record.uid = uid.trim().to_string();
        }

        // Record Key
        if let Some(record_key_str) = record_dict
            .get("recordKey")
            .and_then(|v| v.as_str())
            .map(|s| s.trim())
        {
            if !record_key_str.is_empty() {
                let record_key_encrypted = utils::base64_to_bytes(record_key_str)?;
                match CryptoUtils::decrypt_aes(&record_key_encrypted, secret_key) {
                    Ok(record_key_bytes) => {
                        record.record_key_bytes = record_key_bytes;
                    }
                    Err(err) => {
                        let error_msg = format!(
                            "Error decrypting record key: {} - Record UID: {}",
                            err, record.uid
                        );
                        error!("{}", error_msg);
                        return Err(KSMRError::CryptoError(error_msg));
                    }
                }
            }
        } else {
            // Single Record Share
            record.record_key_bytes = secret_key.to_vec();
        }

        let mut decrypted_data = HashMap::new();
        // Encrypted Record Data
        if let Some(record_data_str) = record_dict.get("data").and_then(|v| v.as_str()) {
            if !record.record_key_bytes.is_empty() {
                let record_encrypted_data = utils::base64_to_bytes(record_data_str)?;
                match CryptoUtils::decrypt_record(&record_encrypted_data, &record.record_key_bytes)
                {
                    Ok(record_data_json) => {
                        record.raw_json = record_data_json.clone();
                        record.record_dict = json_to_dict(&record_data_json).unwrap();
                        decrypted_data = json_to_dict(&record_data_json).unwrap();
                    }
                    Err(err) => {
                        let error_msg = format!(
                            "Error decrypting record data: {} - Record UID: {}",
                            err, record.uid
                        );
                        error!("{}", error_msg);
                        return Err(KSMRError::CryptoError(error_msg));
                    }
                }
            }
        }

        if !decrypted_data.is_empty() {
            // Record Title
            if let Some(title) = decrypted_data.get("title").and_then(|v| v.as_str()) {
                record.title = title.trim().to_string();
            }
        }

        // Record Type
        if let Some(record_type) = record.record_dict.get("type").and_then(|v| v.as_str()) {
            record.record_type = record_type.to_string();
            let password = if record_type == "login" {
                record_dict
                    .get("fields")
                    .and_then(|fields| fields.as_array())
                    .and_then(|fields| {
                        fields
                            .iter()
                            .find(|field| {
                                field.get("type") == Some(&Value::String("password".to_string()))
                            })
                            .and_then(|field| field.get("value"))
                            .and_then(|value| value.as_array())
                            .and_then(|arr| arr.first())
                            .and_then(Value::as_str)
                    })
                    .map(String::from)
            } else {
                None
            };
            match password {
                Some(pass) => record.password = Some(pass),
                None => record.password = None,
            }
        }

        if let Some(uid) = folder_uid {
            if !uid.trim().is_empty() {
                record.folder_uid = uid.clone();
                record.folder_key_bytes = Some(secret_key.to_vec());
            }
        }

        // Inner Folder UID
        if let Some(inner_folder_uid) = record_dict.get("innerFolderUid").and_then(|v| v.as_str()) {
            record.inner_folder_uid = Some(inner_folder_uid.trim().to_string());
        }

        // Revision
        if let Some(revision) = record_dict.get("revision").and_then(|v| v.as_f64()) {
            record.revision = Some(revision as i64);
        }

        // Is Editable
        if let Some(is_editable) = record_dict.get("isEditable").and_then(|v| v.as_bool()) {
            record.is_editable = is_editable;
        }
        let mut _files = Vec::new();
        if let Some(file_list) = record_dict.get("files").and_then(Value::as_array) {
            for file_data in file_list {
                if let Some(file_map) = file_data.as_object() {
                    let file_map_hashmap: HashMap<String, Value> = file_map
                        .clone()
                        .into_iter()
                        .map(|(k, v)| (k.clone(), v.clone()))
                        .collect();

                    let created_keeper_file = KeeperFile::new_from_json(
                        file_map_hashmap,
                        record.record_key_bytes.to_vec(),
                    );
                    match created_keeper_file {
                        Ok(file) => _files.push(file),
                        Err(e) => {
                            let msg = format!("Error loading file: {}", e);
                            eprintln!("{}", msg);
                        }
                    }
                }
            }
            record.files = _files;
        }

        // Parse links from server response envelope (GraphSync linked records)
        if let Some(Value::Array(links_array)) = record_dict.get("links") {
            record.links = links_array
                .iter()
                .filter_map(|link| {
                    link.as_object().map(|obj| {
                        obj.iter()
                            .map(|(k, v)| (k.clone(), v.clone()))
                            .collect::<HashMap<String, Value>>()
                    })
                })
                .collect();
        }

        Ok(record)
    }

    pub fn field_exists(&self, section: &str, name: &str) -> bool {
        // Check if the section is valid
        if section != "fields" && section != "custom" {
            return false;
        }

        // Retrieve the section from the record dictionary
        let section_data = self.record_dict.get(section);
        if section_data.is_none() {
            return false;
        }

        // Ensure the section is an array
        let arr = section_data.unwrap();
        let section_array = match arr.is_array() {
            true => arr.as_array().unwrap(),
            false => return false,
        };

        // Iterate through the array and check for the field
        for item in section_array {
            let item_obj = match item.is_object() {
                true => item.as_object().unwrap(),
                false => return false,
            };
            let item_type = match item_obj.get("type") {
                Some(t) => t.as_str().unwrap(),
                None => return false,
            };
            if item_type == name {
                let item_val = item_obj.get("value");
                match item_val {
                    Some(item_of_value) => match item_of_value.is_array() {
                        true => match item_of_value.as_array() {
                            Some(arr) => match arr.len() {
                                0 => continue, // Skip empty fields, continue checking
                                _ => return true,
                            },
                            None => return false,
                        },
                        false => return false,
                    },
                    None => return false,
                }
            }
        }
        false
    }

    pub fn insert_field(
        &mut self,
        section: &str,
        field: HashMap<String, serde_json::Value>,
    ) -> Result<(), KSMRError> {
        // Validate section
        if section != "fields" && section != "custom" {
            return Err(KSMRError::RecordDataError(format!(
                "Unknown field section '{}'",
                section
            )));
        }

        // Ensure the section exists and is initialized
        let section_fields = self
            .record_dict
            .entry(section.to_string())
            .or_insert_with(|| serde_json::Value::Array(Vec::new()));

        // Add the field
        if let Some(arr) = section_fields.as_array_mut() {
            arr.push(serde_json::to_value(&field).unwrap());
        } else {
            // Handle the case where section_fields is not an array
            return Err(KSMRError::RecordDataError(format!(
                "Section '{}' is not an array",
                section
            )));
        }
        Ok(())
    }

    /// Consolidate multiple fileRef fields into one (fixes legacy bug where upload_file created separate fields)
    pub fn consolidate_file_refs(&mut self) {
        use log::debug;

        if let Some(Value::Array(fields)) = self.record_dict.get_mut("fields") {
            let mut all_file_uids: Vec<Value> = Vec::new();
            let mut first_fileref_index: Option<usize> = None;

            // Collect all file UIDs from all fileRef fields
            for (idx, field) in fields.iter().enumerate() {
                if let Some(field_obj) = field.as_object() {
                    if field_obj.get("type").and_then(|v| v.as_str()) == Some("fileRef") {
                        if first_fileref_index.is_none() {
                            first_fileref_index = Some(idx);
                        }
                        if let Some(Value::Array(values)) = field_obj.get("value") {
                            all_file_uids.extend(values.clone());
                        }
                    }
                }
            }

            // If there's more than one fileRef field, consolidate
            let fileref_count = fields
                .iter()
                .filter(|f| f.get("type").and_then(|v| v.as_str()) == Some("fileRef"))
                .count();

            if fileref_count > 1 {
                debug!(
                    "Record '{}': Consolidating {} fileRef fields into one (total {} file UIDs)",
                    self.title,
                    fileref_count,
                    all_file_uids.len()
                );

                // Remove all fileRef fields
                fields
                    .retain(|field| field.get("type").and_then(|v| v.as_str()) != Some("fileRef"));

                // Add back a single consolidated fileRef field
                if !all_file_uids.is_empty() {
                    let mut consolidated_field = serde_json::Map::new();
                    consolidated_field
                        .insert("type".to_string(), Value::String("fileRef".to_string()));
                    consolidated_field
                        .insert("value".to_string(), Value::Array(all_file_uids.clone()));

                    // Insert at the original position of the first fileRef field
                    if let Some(idx) = first_fileref_index {
                        fields.insert(idx, Value::Object(consolidated_field));
                    } else {
                        fields.push(Value::Object(consolidated_field));
                    }

                    debug!("Record '{}': Consolidation complete - now has 1 fileRef field with {} UIDs",
                           self.title, all_file_uids.len());
                }

                // Update raw_json to reflect the changes
                if let Ok(json_str) = serde_json::to_string(&self.record_dict) {
                    self.raw_json = json_str;
                }
            }
        }
    }

    pub fn print(&self) {
        println!("===");
        println!("Title: {}", self.title);
        println!("UID:   {}", self.uid);
        println!("Type:  {}", self.record_type);
        println!();

        println!("Fields");
        println!("------");

        if let Some(fields) = self.record_dict.get("fields").and_then(|v| v.as_array()) {
            for field in fields {
                if let (Some(field_type), Some(values)) = (
                    field.get("type").and_then(|v| v.as_str()),
                    field.get("value").and_then(|v| v.as_array()),
                ) {
                    if field_type != "fileRef" && field_type != "oneTimeCode" {
                        let value_str: Vec<_> = values
                            .iter()
                            .map(Record::extract_strings)
                            .map(|v| v.join(","))
                            .collect();
                        println!("{} : {}", field_type, value_str.join(", "));
                    }
                }
            }
        }

        println!();
        println!("Custom Fields");
        println!("------");

        if let Some(custom_fields) = self.record_dict.get("custom").and_then(|v| v.as_array()) {
            for field in custom_fields {
                if let (Some(label), Some(field_type), Some(values)) = (
                    field.get("label").and_then(|v| v.as_str()),
                    field.get("type").and_then(|v| v.as_str()),
                    field.get("value").and_then(|v| v.as_array()),
                ) {
                    let value_str: Vec<_> = values.iter().filter_map(|v| v.as_str()).collect();
                    println!("{} ({}) : {}", label, field_type, value_str.join(", "));
                }
            }
        }
    }

    fn extract_strings(value: &Value) -> Vec<String> {
        let mut results = Vec::new();

        match value {
            Value::String(s) => results.push(s.clone()), // Collect strings
            Value::Number(s) => results.push(s.to_string().clone()),
            Value::Array(arr) => {
                for item in arr {
                    results.extend(Self::extract_strings(item)); // Recurse for arrays
                }
            }
            Value::Object(map) => {
                for val in map.values() {
                    results.extend(Self::extract_strings(val)); // Recurse for map values
                }
            }
            _ => {} // Ignore other types
        }

        results
    }

    pub fn find_file_by_filename(
        &mut self,
        filename: &str,
    ) -> Result<Option<&mut KeeperFile>, KSMRError> {
        Ok(self.files.iter_mut().find(|file| file.name == filename))
    }

    pub fn find_file(&mut self, name: &str) -> Result<Option<&mut KeeperFile>, KSMRError> {
        Ok(self
            .files
            .iter_mut()
            .find(|file| file.uid == name || file.name == name || file.title == name))
    }

    pub fn find_files(&mut self, name: &str) -> Vec<&mut KeeperFile> {
        self.files
            .iter_mut()
            .filter(|file| file.uid == name || file.name == name || file.title == name)
            .collect()
    }

    pub fn download_file_by_title(&mut self, title: &str, path: &str) -> Result<bool, KSMRError> {
        let found_file = self.find_file_by_title(title)?;

        match found_file {
            Some(file) => {
                let file_status = file.save_file(path.to_string(), false)?;
                Ok(file_status)
            }
            None => Err(KSMRError::FileError(format!(
                "File with title {} not found",
                title
            ))),
        }
    }

    pub fn download_file(&mut self, uid: &str, path: &str) -> Result<bool, KSMRError> {
        let found_file = self.find_file(uid)?;

        match found_file {
            Some(file) => {
                let file_status = file.save_file(path.to_string(), false)?;
                Ok(file_status)
            }
            None => {
                info!(
                    "File with name/uid {} not found in record with uid {}",
                    uid, self.uid
                );
                Ok(false)
            }
        }
    }
}

impl fmt::Display for Record {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[Record: uid={}, type={:?}, title={:?}, files count={}]",
            self.uid,
            self.record_type,
            self.title,
            self.files.len()
        )
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeeperFile {
    // Define the fields of KeeperFile here
    file_key: String,
    pub metadata_dict: HashMap<String, Value>,

    data: Vec<u8>,

    pub uid: String,
    pub file_type: String,
    pub title: String,
    pub name: String,
    last_modified: i64,
    size: i64,
    pub url: Option<String>,           // Download URL (v16.7.0+)
    pub thumbnail_url: Option<String>, // Thumbnail URL (v16.7.0+)
    pub proxy_url: Option<String>,     // Proxy URL for HTTP requests

    f: HashMap<String, Value>,
    record_key_bytes: Vec<u8>,
}

#[allow(clippy::inherent_to_string)]
impl KeeperFile {
    pub fn deep_copy(&self) -> KeeperFile {
        KeeperFile {
            file_key: self.file_key.clone(),
            metadata_dict: self.metadata_dict.clone(),
            data: self.data.clone(),
            uid: self.uid.clone(),
            file_type: self.file_type.clone(),
            title: self.title.clone(),
            name: self.name.clone(),
            last_modified: self.last_modified,
            size: self.size,
            url: self.url.clone(),
            thumbnail_url: self.thumbnail_url.clone(),
            proxy_url: self.proxy_url.clone(),
            f: self.f.clone(),
            record_key_bytes: self.record_key_bytes.clone(),
        }
    }

    /// Decrypts the file key using the record key bytes.
    pub fn decrypt_file_key(&self) -> Result<Vec<u8>, KSMRError> {
        // Retrieve the Base64-encoded file key from metadata
        let file_key_encrypted_base64 = self
            .f
            .get("fileKey")
            .ok_or_else(|| {
                KSMRError::KeyNotFoundError("fileKey not found in metadata".to_string())
            })?
            .as_str()
            .ok_or_else(|| KSMRError::DecodeError("fileKey is not a string".to_string()))?;

        // Decode the Base64-encoded string
        let file_key_encrypted = utils::base64_to_bytes(file_key_encrypted_base64)?;

        // Decrypt the file key using AES
        CryptoUtils::decrypt_aes(&file_key_encrypted, &self.record_key_bytes).map_err(|e| {
            log::error!(
                "Error decrypting file key: {}, error: {}",
                file_key_encrypted_base64,
                e
            );
            KSMRError::CryptoError(format!("Failed to decrypt file key: {}", e))
        })
    }

    pub fn get_meta(&mut self) -> Result<HashMap<String, Value>, KSMRError> {
        // If metadata is already populated, return it
        if !self.metadata_dict.is_empty() {
            return Ok(self.metadata_dict.clone());
        }

        // Retrieve the Base64-encoded file metadata
        let data_str = self
            .f
            .get("data")
            .and_then(|data| data.as_str())
            .ok_or_else(|| {
                KSMRError::KeyNotFoundError("Missing 'data' field in metadata".to_string())
            })?;

        // Decrypt the file key
        let file_key = self.decrypt_file_key()?;

        // Decode the Base64-encoded metadata
        let data_bytes = utils::base64_to_bytes(data_str)?;

        // Decrypt the metadata
        let decrypted_meta = CryptoUtils::decrypt_aes(&data_bytes, &file_key)
            .map_err(|e| KSMRError::CryptoError(format!("Failed to decrypt metadata: {}", e)))?;

        // Convert decrypted metadata into a UTF-8 string
        let meta_json = utils::bytes_to_string(&decrypted_meta)?;

        // Parse the JSON string into a HashMap
        self.metadata_dict = json_to_dict(&meta_json).unwrap_or_default();

        Ok(self.metadata_dict.clone())
    }

    /// Returns the decrypted raw file data.
    pub fn get_file_data(&mut self) -> Result<Option<Vec<u8>>, KSMRError> {
        // Return cached data if it exists
        if !self.data.is_empty() {
            return Ok(Some(self.data.clone()));
        }

        // Decrypt the file key
        let file_key = self.decrypt_file_key()?;

        // Get the file URL
        let file_url = self
            .get_url()
            .map_err(|_| KSMRError::FileError("File URL is invalid".to_string()))?;

        // Fetch the file data from the URL
        let mut client_builder = reqwest::blocking::Client::builder();
        if let Some(ref proxy_url) = self.proxy_url {
            if let Ok(proxy) = reqwest::Proxy::all(proxy_url) {
                client_builder = client_builder.proxy(proxy);
            }
        }
        let http_client = client_builder
            .build()
            .map_err(|e| KSMRError::FileError(format!("Failed to build HTTP client: {}", e)))?;
        let mut response = http_client
            .get(&file_url)
            .send()
            .map_err(|e| KSMRError::FileError(format!("Failed to fetch file: {}", e)))?;

        // Ensure the HTTP request was successful
        if !response.status().is_success() {
            return Err(KSMRError::HTTPError(format!(
                "HTTP request failed with status: {}",
                response.status()
            )));
        }

        // Read the response body
        let mut encrypted_data = Vec::new();
        response
            .read_to_end(&mut encrypted_data)
            .map_err(|e| KSMRError::IOError(format!("Failed to read response body: {}", e)))?;

        // Decrypt the file data
        let decrypted_data = CryptoUtils::decrypt_aes(&encrypted_data, &file_key)
            .map_err(|e| KSMRError::CryptoError(format!("Failed to decrypt file: {}", e)))?;

        // Cache the decrypted data
        self.data = decrypted_data.clone();

        Ok(Some(decrypted_data))
    }

    /// Retrieves the URL from the `f` HashMap, if available.
    pub fn get_url(&self) -> Result<String, KSMRError> {
        // Try url field first (if populated from API), then fall back to f HashMap
        if let Some(url) = &self.url {
            return Ok(url.clone());
        }

        let file_url = self
            .f
            .get("url") // Look for the "url" key in the HashMap
            .and_then(|value| value.as_str()) // Ensure the value is a string
            .unwrap_or_default() // Return the string if found, or an empty string if not
            .to_string(); // Convert to a String
        Ok(file_url)
    }

    /// Downloads and decrypts the file thumbnail.
    ///
    /// # Returns
    /// * `Result<Option<Vec<u8>>, KSMRError>` - Decrypted thumbnail data, or None if no thumbnail available
    ///
    /// # Example
    /// ```rust,ignore
    /// # use keeper_secrets_manager_core::dto::KeeperFile;
    /// # use keeper_secrets_manager_core::custom_error::KSMRError;
    /// # fn example(file: &mut KeeperFile) -> Result<(), KSMRError> {
    /// if let Some(thumbnail_data) = file.get_thumbnail_data()? {
    ///     // Save thumbnail to disk
    ///     std::fs::write("thumbnail.jpg", thumbnail_data)?;
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn get_thumbnail_data(&mut self) -> Result<Option<Vec<u8>>, KSMRError> {
        // Check if thumbnail URL is available
        let thumbnail_url = if let Some(url) = &self.thumbnail_url {
            url.clone()
        } else if let Some(url) = self.f.get("thumbnailUrl").and_then(|v| v.as_str()) {
            url.to_string()
        } else {
            return Ok(None); // No thumbnail available
        };

        // Decrypt the file key
        let file_key = self.decrypt_file_key()?;

        // Fetch the thumbnail data from the URL
        let mut client_builder = reqwest::blocking::Client::builder();
        if let Some(ref proxy_url) = self.proxy_url {
            if let Ok(proxy) = reqwest::Proxy::all(proxy_url) {
                client_builder = client_builder.proxy(proxy);
            }
        }
        let http_client = client_builder
            .build()
            .map_err(|e| KSMRError::FileError(format!("Failed to build HTTP client: {}", e)))?;
        let mut response = http_client
            .get(&thumbnail_url)
            .send()
            .map_err(|e| KSMRError::FileError(format!("Failed to fetch thumbnail: {}", e)))?;

        // Ensure the HTTP request was successful
        if !response.status().is_success() {
            return Err(KSMRError::HTTPError(format!(
                "HTTP request failed with status: {}",
                response.status()
            )));
        }

        // Read the response body
        let mut encrypted_thumbnail = Vec::new();
        response
            .read_to_end(&mut encrypted_thumbnail)
            .map_err(|e| KSMRError::IOError(format!("Failed to read thumbnail: {}", e)))?;

        // Decrypt the thumbnail data
        let decrypted_thumbnail = CryptoUtils::decrypt_aes(&encrypted_thumbnail, &file_key)
            .map_err(|e| KSMRError::CryptoError(format!("Failed to decrypt thumbnail: {}", e)))?;

        Ok(Some(decrypted_thumbnail))
    }

    pub fn new_from_json(
        file_dict: HashMap<String, Value>,
        record_key_bytes: Vec<u8>,
    ) -> Result<Self, KSMRError> {
        // Extract url and thumbnailUrl from file_dict before creating struct
        let url = file_dict
            .get("url")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let thumbnail_url = file_dict
            .get("thumbnailUrl")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let mut file = KeeperFile {
            file_key: String::new(),
            metadata_dict: HashMap::new(),
            data: vec![],
            uid: String::new(),
            file_type: String::new(),
            title: String::new(),
            name: String::new(),
            last_modified: 0,
            size: 0,
            url,
            thumbnail_url,
            proxy_url: None,
            f: file_dict.clone(),
            record_key_bytes,
        };

        // Extract metadata if present
        let meta = file.get_meta()?;
        if let Some(file_uid) = file_dict.get("fileUid").and_then(|v| v.as_str()) {
            file.uid = file_uid.to_string();
        }
        if let Some(file_type) = meta.get("type").and_then(|v| v.as_str()) {
            file.file_type = file_type.to_string();
        }
        if let Some(title) = meta.get("title").and_then(|v| v.as_str()) {
            file.title = title.to_string();
        }
        if let Some(name) = meta.get("name").and_then(|v| v.as_str()) {
            file.name = name.to_string();
        }
        if let Some(last_modified) = meta.get("lastModified").and_then(|v| v.as_f64()) {
            file.last_modified = last_modified as i64;
        }
        if let Some(size) = meta.get("size").and_then(|v| v.as_f64()) {
            file.size = size as i64;
        }

        Ok(file)
    }

    pub fn save_file(&mut self, path: String, create_folders: bool) -> Result<bool, KSMRError> {
        // Resolve the absolute path
        let abs_path = match fs::canonicalize(&path) {
            Ok(p) => p,
            Err(_) => PathBuf::from(&path), // Fallback to given path if canonicalization fails
        };

        // Get the parent directory
        let dir_path = abs_path.parent().ok_or_else(|| {
            KSMRError::PathError(format!(
                "Failed to determine parent directory for path: {}",
                path
            ))
        })?;

        // Create folders if needed
        if create_folders {
            if let Err(err) = fs::create_dir_all(dir_path) {
                error!("Error creating folders: {}", err);
                return Err(KSMRError::IOError(format!(
                    "Failed to create directories: {}",
                    err
                )));
            }
        }

        // Verify that the directory exists
        if !dir_path.exists() {
            return Err(KSMRError::PathError(format!(
                "Directory does not exist: {}",
                dir_path.display()
            )));
        }

        // Write the file data
        let _download_file_data = self.get_file_data()?;

        let mut file = File::create(&abs_path).map_err(|err| {
            KSMRError::IOError(format!(
                "Failed to create file {}: {}",
                abs_path.display(),
                err
            ))
        })?;
        file.write_all(&self.data).map_err(|err| {
            KSMRError::IOError(format!(
                "Failed to write to file {}: {}",
                abs_path.display(),
                err
            ))
        })?;

        Ok(true)
    }

    pub fn to_string(&self) -> String {
        format!("[KeeperFile - name: {}, title: {}]", self.name, self.title)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeeperFolder {
    pub folder_key: Vec<u8>,
    pub folder_uid: String,
    pub parent_uid: String,
    pub name: String,
}

impl KeeperFolder {
    pub fn new(
        folder_map: &HashMap<String, serde_json::Value>,
        folder_key: Vec<u8>,
    ) -> Result<Self, KSMRError> {
        let mut folder = KeeperFolder {
            folder_key,
            folder_uid: String::new(),
            parent_uid: String::new(),
            name: String::new(),
        };

        if let Some(serde_json::Value::String(val)) = folder_map.get("folderUid") {
            folder.folder_uid = val.clone();
        }

        if let Some(serde_json::Value::String(val)) = folder_map.get("parent") {
            folder.parent_uid = val.clone();
        }

        if let Some(serde_json::Value::String(val)) = folder_map.get("data") {
            let data = match CryptoUtils::url_safe_str_to_bytes(val) {
                Ok(data) => data,
                Err(e) => {
                    if e.to_string().contains("Invalid padding") {
                        CryptoUtils::url_safe_str_to_bytes_trim_padding(val)?
                    } else {
                        return Err(e);
                    }
                }
            };
            if let Ok(decrypted_data) = CryptoUtils::decrypt_aes_cbc(&data, &folder.folder_key) {
                #[derive(Deserialize)]
                struct FolderName {
                    name: String,
                }
                let decrypted_data_unpadded = unpad_data(decrypted_data.as_slice()).unwrap();
                if let Ok(folder_name) =
                    serde_json::from_slice::<FolderName>(&decrypted_data_unpadded)
                {
                    folder.name = folder_name.name;
                } else {
                    error!("Error parsing folder name from decrypted data");
                }
            }
        }
        Ok(folder)
    }

    pub fn to_serialized_string(&self) -> String {
        let mut clone: HashMap<String, Value> = HashMap::new();
        clone.insert(
            "folderKey".to_string(),
            Value::String(hex::encode(self.folder_key.clone())),
        );
        clone.insert(
            ("folderUid").to_string(),
            Value::String(self.folder_uid.clone()),
        );
        clone.insert(
            "parentUid".to_string(),
            Value::String(self.parent_uid.clone()),
        );
        clone.insert("name".to_string(), Value::String(self.name.clone()));
        serde_json::to_string_pretty(&clone).unwrap_or_else(|_| "Failed to serialize".to_string())
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Folder {
    key: Vec<u8>,
    pub uid: String,
    parent_uid: String,
    name: String,
    data: HashMap<String, Value>,
    folder_records: Vec<HashMap<String, Value>>,
}

impl Folder {
    pub fn new_from_json(folder_dict: HashMap<String, Value>, secret_key: &[u8]) -> Option<Self> {
        let mut folder = Folder {
            key: vec![],
            uid: String::new(),
            parent_uid: String::new(),
            name: String::new(),
            data: folder_dict.clone(),
            folder_records: vec![],
        };

        if let Some(Value::String(uid)) = folder_dict.get("folderUid") {
            folder.uid = uid.trim().to_string();

            if let Some(Value::String(folder_key_enc)) = folder_dict.get("folderKey") {
                let folder_key_bytes = utils::base64_to_bytes(folder_key_enc).unwrap();
                match CryptoUtils::decrypt_aes(&folder_key_bytes, secret_key) {
                    Ok(folder_key) => {
                        folder.key = folder_key;

                        if let Some(Value::Array(records)) = folder_dict.get("records") {
                            for record in records {
                                if let Some(record_map) = record.as_object() {
                                    folder.folder_records.push(
                                        record_map
                                            .clone()
                                            .into_iter()
                                            .map(|(k, v)| (k.clone(), v.clone()))
                                            .collect(),
                                    );
                                } else {
                                    log::error!("Folder records JSON is in incorrect format");
                                }
                            }
                        }
                    }
                    Err(err) => {
                        log::error!(
                            "Error decrypting folder key: {:?} - Folder UID: {}",
                            err,
                            folder.uid
                        );
                    }
                }
            }
        } else {
            log::error!("Not a folder");
            return None;
        }

        Some(folder)
    }

    pub fn get_folder_key(&self) -> Vec<u8> {
        self.key.clone()
    }

    pub fn records(&self) -> Result<Vec<Record>, KSMRError> {
        let mut records = vec![];
        for record_map in &self.folder_records {
            let record_result =
                Record::new_from_json(record_map.clone(), &self.key, Some(self.uid.to_string()));

            // if record_result.is_err() {
            //     log::error!("Error parsing folder record: {:?}", record_map);
            // } else {
            //     records.push(record_result.unwrap());
            // }

            if let Ok(record) = record_result {
                records.push(record);
            } else {
                log::error!("Error parsing folder record: {:?}", record_map);
            }
        }
        Ok(records)
    }
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct AppData {
    title: Option<String>,
    app_type: Option<String>,
}

impl AppData {
    pub fn new(title: Option<String>, app_type: Option<String>) -> Self {
        AppData { title, app_type }
    }
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SecretsManagerResponse {
    pub app_data: AppData,
    pub folders: Vec<Folder>,
    pub records: Vec<Record>,
    pub expires_on: i64,
    pub warnings: Option<String>,
    pub just_bound: bool,
}

impl SecretsManagerResponse {
    pub fn expires_on_str(&self, date_format: Option<&str>) -> String {
        let unix_time_seconds = self.expires_on / 1000;
        let naive_datetime =
            DateTime::from_timestamp(unix_time_seconds.saturating_sub(i64::MIN), 0)
                .unwrap_or_else(|| DateTime::from_timestamp(0, 0).unwrap()); // Handle invalid timestamps gracefully
        let format = date_format.unwrap_or("%Y-%m-%d %H:%M:%S");
        naive_datetime.format(format).to_string()
    }

    pub fn new() -> Self {
        SecretsManagerResponse {
            app_data: AppData::default(),
            folders: Vec::new(),
            records: Vec::new(),
            expires_on: 0,
            warnings: None,
            just_bound: false,
        }
    }
}

pub struct KeeperFileUpload {
    pub name: String,
    pub data: Vec<u8>,
    pub title: String,
    pub mime_type: String,
}

impl KeeperFileUpload {
    pub fn get_file_for_upload(
        file_path: &str,
        file_name: Option<&str>,
        file_title: Option<&str>,
        mime_type: Option<&str>,
    ) -> Result<KeeperFileUpload, KSMRError> {
        // Resolve file name
        let resolved_name = file_name
            .unwrap_or_else(|| {
                Path::new(file_path)
                    .file_name()
                    .and_then(|name| name.to_str())
                    .unwrap_or("")
            })
            .to_string();

        // Resolve file title
        let resolved_title = file_title.unwrap_or(resolved_name.as_str()).to_string();

        // Resolve MIME type
        let resolved_type = mime_type.unwrap_or("application/octet-stream").to_string();

        // Read file data
        let file_data = fs::read(file_path)
            .map_err(|err| KSMRError::IOError(format!("Error reading file data: {}", err)))?;

        // Return KeeperFileUpload instance
        Ok(KeeperFileUpload {
            name: resolved_name,
            title: resolved_title,
            mime_type: resolved_type,
            data: file_data,
        })
    }
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct RecordCreate {
    pub record_type: String,
    pub title: String,
    pub notes: Option<String>,
    pub fields: Option<Vec<KeeperField>>,
    pub custom: Option<Vec<KeeperField>>,
}

pub const VALID_RECORD_FIELDS: [&str; 45] = [
    "accountNumber",
    "address",
    "addressRef",
    "appFiller",
    "bankAccount",
    "birthDate",
    "cardRef",
    "checkbox",
    "databaseType",
    "date",
    "directoryType",
    "dropdown",
    "email",
    "birthDate",
    "expirationDate",
    "fileRef",
    "host",
    "isSSIDHidden",
    "keyPair",
    "licenseNumber",
    "login",
    "multiline",
    "name",
    "note",
    "oneTimeCode",
    "otp",
    "pamHostname",
    "pamRemoteBrowserSettings",
    "pamResources",
    "pamSettings",
    "passkey",
    "password",
    "paymentCard",
    "phone",
    "pinCode",
    "rbiUrl",
    "recordRef",
    "schedule",
    "script",
    "secret",
    "securityQuestion",
    "text",
    "trafficEncryptionSeed",
    "url",
    "wifiEncryption",
];

impl RecordCreate {
    pub fn new(record_type: String, title: String, notes: Option<String>) -> Self {
        Self {
            record_type,
            title,
            notes,
            fields: None,
            custom: None,
        }
    }

    pub fn validate(&self) -> Result<(), KSMRError> {
        // Validate title
        if self.title.trim().is_empty() {
            return Err(KSMRError::RecordDataError(
                "Record title should not be empty.".to_string(),
            ));
        }

        // Validate notes
        if let Some(notes) = &self.notes {
            if notes.trim().is_empty() {
                return Err(KSMRError::RecordDataError(
                    "Record notes should not be empty.".to_string(),
                ));
            }
        }

        // Validate fields
        if let Some(fields) = &self.fields {
            let mut field_type_errors = vec![];
            let mut field_value_errors = vec![];

            for field in fields {
                // Validate field type
                if !VALID_RECORD_FIELDS.contains(&field.field_type.as_str()) {
                    field_type_errors.push(field.field_type.clone());
                }

                // Validate field value
                match field.value.is_array() {
                    true => {
                        if field.value.as_array().unwrap().is_empty() {
                            field_value_errors.push(field.field_type.clone());
                        }
                    }
                    false => {
                        return Err(KSMRError::RecordDataError(
                            "Field value is not Array".to_string(),
                        ))
                    }
                };
            }

            if !field_type_errors.is_empty() {
                return Err(KSMRError::RecordDataError(format!(
                    "Following field types are not allowed: [{}]. Allowed field types are: [{}]",
                    field_type_errors.join(", "),
                    VALID_RECORD_FIELDS.join(", ")
                )));
            }

            if !field_value_errors.is_empty() {
                return Err(KSMRError::RecordDataError(format!(
                    "Fields with the following types should have non-empty list values: [{}]",
                    field_value_errors.join(", ")
                )));
            }
        }

        Ok(())
    }

    pub fn to_dict(&self) -> Result<HashMap<String, Value>, KSMRError> {
        self.validate()?; // Ensure validation passes before creating the dictionary

        let mut rec_dict = HashMap::new();
        rec_dict.insert("type".to_string(), Value::String(self.record_type.clone()));
        rec_dict.insert("title".to_string(), Value::String(self.title.clone()));

        if let Some(notes) = &self.notes {
            rec_dict.insert("notes".to_string(), Value::String(notes.clone()));
        }

        if let Some(fields) = &self.fields {
            rec_dict.insert(
                "fields".to_string(),
                Value::Array(
                    fields
                        .iter()
                        .map(|f| serde_json::to_value(f).unwrap())
                        .collect(),
                ),
            );
        }

        if let Some(custom) = &self.custom {
            rec_dict.insert(
                "custom".to_string(),
                serde_json::to_value(custom.clone()).unwrap(),
            );
        }

        Ok(rec_dict)
    }

    pub fn to_json(&self) -> Result<String, KSMRError> {
        let rec_dict = self.to_dict()?;
        serde_json::to_string(&rec_dict).map_err(|e| {
            KSMRError::SerializationError(format!("Error serializing record field data: {}", e))
        })
    }

    pub fn append_standard_fields(&mut self, field: KeeperField) {
        if self.fields.is_none() {
            self.fields = Some(vec![]);
        }
        self.fields.as_mut().unwrap().push(field);
    }

    pub fn append_custom_field(&mut self, field: KeeperField) {
        if self.custom.is_none() {
            self.custom = Some(vec![]);
        }
        self.custom.as_mut().unwrap().push(field);
    }
}

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

    /// Helper: construct a minimal KeeperFile for testing struct-level behavior.
    /// Private fields are accessible here because we are in the same module.
    fn make_test_file(proxy_url: Option<String>) -> KeeperFile {
        KeeperFile {
            file_key: String::new(),
            metadata_dict: HashMap::new(),
            data: vec![],
            uid: String::new(),
            file_type: String::new(),
            title: String::new(),
            name: String::new(),
            last_modified: 0,
            size: 0,
            url: None,
            thumbnail_url: None,
            proxy_url,
            f: HashMap::new(),
            record_key_bytes: vec![],
        }
    }

    /// Regression test for KSM-791:
    /// KeeperFile.proxy_url must exist and default to None so that
    /// get_file_data() / get_thumbnail_data() build a proxy-aware HTTP client.
    /// Previously the field did not exist and bare reqwest::get() was used.
    #[test]
    fn test_keeper_file_proxy_url_defaults_to_none() {
        let file = make_test_file(None);
        assert!(file.proxy_url.is_none());
    }

    /// Regression test for KSM-791:
    /// proxy_url must be settable so that SecretsManager can propagate its
    /// proxy configuration to files after record decryption.
    #[test]
    fn test_keeper_file_proxy_url_can_be_set() {
        let mut file = make_test_file(None);
        file.proxy_url = Some("http://proxy.example.com:8080".to_string());
        assert_eq!(
            file.proxy_url,
            Some("http://proxy.example.com:8080".to_string())
        );
    }

    /// Regression test for KSM-791:
    /// deep_copy() must preserve proxy_url so that callers that clone a file
    /// (e.g. notation lookups) do not silently lose proxy configuration.
    #[test]
    fn test_keeper_file_deep_copy_preserves_proxy_url() {
        let file_with_proxy = make_test_file(Some("http://proxy.example.com:8080".to_string()));
        let copied = file_with_proxy.deep_copy();
        assert_eq!(
            copied.proxy_url,
            Some("http://proxy.example.com:8080".to_string())
        );

        let file_without_proxy = make_test_file(None);
        let copied_none = file_without_proxy.deep_copy();
        assert!(copied_none.proxy_url.is_none());
    }
}