proton-drive-rs 0.1.8

High-level Proton Drive client for Rust
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
//! Serde DTOs for the Drive read endpoints (shares, volumes, links, folders).
//!
//! Some fields are deserialized for wire-format fidelity / forthcoming
//! milestones (uploads, signature verification) but not yet read.
#![allow(dead_code)]

use serde::{Deserialize, Serialize};

use proton_sdk::ids::{
    AddressId, AddressKeyId, DeviceUid, DriveEventId, LinkId, ShareId, ShareMembershipId, VolumeId,
};

/// `GET v2/shares/my-files`
#[derive(Debug, Deserialize)]
pub struct MyFilesShareResponse {
    #[serde(rename = "Volume")]
    pub volume: ShareVolumeDto,
    #[serde(rename = "Share")]
    pub share: ShareDto,
    #[serde(rename = "Link")]
    pub link: LinkDetailsDto,
}

#[derive(Debug, Deserialize)]
pub struct ShareVolumeDto {
    #[serde(rename = "VolumeID")]
    pub id: VolumeId,
}

#[derive(Debug, Deserialize)]
pub struct ShareDto {
    #[serde(rename = "ShareID")]
    pub id: ShareId,
    #[serde(rename = "Key")]
    pub key: String,
    #[serde(rename = "Passphrase")]
    pub passphrase: String,
    #[serde(rename = "PassphraseSignature")]
    pub passphrase_signature: Option<String>,
    #[serde(rename = "AddressID")]
    pub address_id: AddressId,
}

/// `POST v2/volumes/{vid}/links` request body.
#[derive(Debug, Serialize)]
pub struct LinkDetailsRequest<'a> {
    #[serde(rename = "LinkIDs")]
    pub link_ids: &'a [LinkId],
}

/// `POST v2/volumes/{vid}/links` response.
#[derive(Debug, Deserialize)]
pub struct LinkDetailsResponse {
    #[serde(rename = "Links")]
    pub links: Vec<LinkDetailsDto>,
}

/// `GET volumes/{vid}/photos` response (C# `TimelinePhotoListResponse`).
#[derive(Debug, Deserialize)]
pub struct TimelinePhotoListResponse {
    #[serde(rename = "Photos")]
    pub photos: Vec<TimelinePhotoDto>,
}

/// One timeline entry (C# `TimelinePhotoDto`). Only the id + capture time are
/// consumed; the remaining fields are kept for wire fidelity.
#[derive(Debug, Deserialize)]
pub struct TimelinePhotoDto {
    #[serde(rename = "LinkID")]
    pub id: LinkId,
    #[serde(rename = "CaptureTime")]
    pub capture_time: i64,
    #[serde(rename = "Hash")]
    pub name_hash: Option<String>,
    #[serde(rename = "ContentHash")]
    pub content_hash: Option<String>,
}

/// `GET volumes/{vid}/trash` response. Trashed links are grouped by the share
/// they belong to (C# `VolumeTrashResponse` / `ShareTrashDto`).
#[derive(Debug, Deserialize)]
pub struct VolumeTrashResponse {
    #[serde(rename = "Trash")]
    pub trash_by_share: Vec<ShareTrashDto>,
}

#[derive(Debug, Deserialize)]
pub struct ShareTrashDto {
    #[serde(rename = "ShareID")]
    pub share_id: ShareId,
    #[serde(rename = "LinkIDs")]
    pub link_ids: Vec<LinkId>,
    #[serde(rename = "ParentIDs", default)]
    pub parent_ids: Vec<LinkId>,
}

#[derive(Debug, Deserialize)]
pub struct LinkDetailsDto {
    #[serde(rename = "Link")]
    pub link: LinkDto,
    #[serde(rename = "Folder")]
    pub folder: Option<FolderDto>,
    #[serde(rename = "File")]
    pub file: Option<FileDto>,
    /// Photos-volume `/links` returns file properties under `Photo` (a superset
    /// of `File`) rather than `File`. Deserialized as [`FileDto`]; the extra
    /// photo fields are ignored. C# `linkDetailsDto.File ?? linkDetailsDto.Photo`.
    #[serde(rename = "Photo")]
    pub photo: Option<FileDto>,
    /// Present when the node is shared (with members and/or via a public link).
    #[serde(rename = "Sharing", default)]
    pub sharing: Option<LinkSharingDto>,
    /// Present when the node is shared *with us*: our membership in the sharer's
    /// share. Carries the ids needed to leave the share.
    #[serde(rename = "Membership", default)]
    pub membership: Option<ShareMembershipSummaryDto>,
}

/// The sharing state of a node (C# `LinkSharingDto`). Its mere presence means
/// the node is shared; a `ShareURLID` means it is also shared by public link.
#[derive(Debug, Deserialize)]
pub struct LinkSharingDto {
    #[serde(rename = "ShareID")]
    pub share_id: ShareId,
    #[serde(rename = "ShareURLID", default)]
    pub share_url_id: Option<String>,
}

/// Our membership in a share someone else owns (C# `ShareMembershipSummaryDto`).
#[derive(Debug, Deserialize)]
pub struct ShareMembershipSummaryDto {
    #[serde(rename = "ShareID")]
    pub share_id: ShareId,
    #[serde(rename = "MembershipID")]
    pub membership_id: ShareMembershipId,
    #[serde(rename = "Permissions", default)]
    pub permissions: i32,
}

impl LinkDetailsDto {
    /// File properties for a file/photo node, preferring `File` and falling back
    /// to the photos-volume `Photo` block.
    pub fn file_properties(&self) -> Option<&FileDto> {
        self.file.as_ref().or(self.photo.as_ref())
    }
}

#[derive(Debug, Deserialize)]
pub struct LinkDto {
    #[serde(rename = "LinkID")]
    pub id: LinkId,
    #[serde(rename = "Type")]
    pub link_type: i32,
    #[serde(rename = "ParentLinkID")]
    pub parent_id: Option<LinkId>,
    #[serde(rename = "State")]
    pub state: i32,
    #[serde(rename = "CreateTime")]
    pub creation_time: i64,
    #[serde(rename = "ModifyTime")]
    pub modification_time: i64,
    #[serde(rename = "Trashed")]
    pub trash_time: Option<i64>,
    #[serde(rename = "Name")]
    pub name: String,
    /// Lowercase-hex HMAC-SHA256 name hash under the parent's hash key (C#
    /// `LinkDto.NameHashDigest`, JSON `NameHash`). Cached as a node's
    /// `OriginalHash` for later move/rename without re-decrypting the name.
    #[serde(rename = "NameHash", default)]
    pub name_hash: Option<String>,
    #[serde(rename = "NodeKey")]
    pub key: String,
    #[serde(rename = "NodePassphrase")]
    pub passphrase: String,
    #[serde(rename = "NodePassphraseSignature")]
    pub passphrase_signature: Option<String>,
    #[serde(rename = "SignatureEmail")]
    pub signature_email: Option<String>,
    #[serde(rename = "NameSignatureEmail")]
    pub name_signature_email: Option<String>,
}

impl LinkDto {
    pub fn parsed_type(&self) -> LinkType {
        LinkType::from_raw(self.link_type)
    }

    pub fn is_trashed(&self) -> bool {
        self.state == LinkState::Trashed as i32 || self.trash_time.is_some()
    }
}

#[derive(Debug, Deserialize)]
pub struct FolderDto {
    #[serde(rename = "NodeHashKey")]
    pub hash_key: String,
    #[serde(rename = "XAttr")]
    pub extended_attributes: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct FileDto {
    #[serde(rename = "MediaType")]
    pub media_type: String,
    #[serde(rename = "TotalEncryptedSize")]
    pub total_size_on_storage: i64,
    /// Base64 PKESK packet for the file's content key, addressed to the node key.
    #[serde(rename = "ContentKeyPacket")]
    pub content_key_packet: Option<String>,
    /// Detached signature over the content key (best-effort verification).
    #[serde(rename = "ContentKeyPacketSignature")]
    pub content_key_signature: Option<String>,
    #[serde(rename = "ActiveRevision")]
    pub active_revision: Option<ActiveRevisionDto>,
}

#[derive(Debug, Deserialize)]
pub struct ActiveRevisionDto {
    #[serde(rename = "RevisionID")]
    pub id: String,
    #[serde(rename = "CreateTime")]
    pub creation_time: i64,
    /// Wire revision state (C# `ApiRevisionState`): 0 draft, 1 active, 2 obsolete.
    /// Absent on older responses; a link's active revision is Active by
    /// definition, which is what C# `DtoToMetadataConverter` records.
    #[serde(rename = "State", default)]
    pub state: Option<i32>,
    #[serde(rename = "EncryptedSize")]
    pub encrypted_size: i64,
    /// Email of the revision signer; empty/absent means the node key signed.
    /// Resolves the `XAttr` authorship claim (C# `SignatureEmailAddress`).
    #[serde(rename = "SignatureEmail", default)]
    pub signature_email: Option<String>,
    /// Armored PGP message (encrypted to the node key, signed) carrying the
    /// revision's extended attributes. Decrypts to [`DecryptedExtendedAttributes`].
    #[serde(rename = "XAttr")]
    pub extended_attributes: Option<String>,
}

/// The decrypted `XAttr` JSON payload, read side. Mirrors C# `ExtendedAttributes`
/// / `CommonExtendedAttributes`; every field is optional because the payload is
/// produced by heterogeneous clients (the upload-side [`ExtendedAttributes`]
/// struct only writes a subset).
#[derive(Debug, Default, Deserialize)]
pub struct DecryptedExtendedAttributes {
    #[serde(rename = "Common", default)]
    pub common: Option<DecryptedCommonExtendedAttributes>,
}

#[derive(Debug, Default, Deserialize)]
pub struct DecryptedCommonExtendedAttributes {
    /// Authoritative plaintext file size, in bytes.
    #[serde(rename = "Size", default)]
    pub size: Option<i64>,
    /// ISO-8601 modification timestamp, as written by the uploading client.
    #[serde(rename = "ModificationTime", default)]
    pub modification_time: Option<String>,
    #[serde(rename = "BlockSizes", default)]
    pub block_sizes: Option<Vec<i64>>,
    #[serde(rename = "Digests", default)]
    pub digests: Option<DecryptedFileContentDigests>,
}

#[derive(Debug, Default, Deserialize)]
pub struct DecryptedFileContentDigests {
    /// Lowercase-hex SHA-1 of the full plaintext.
    #[serde(rename = "SHA1", default)]
    pub sha1: Option<String>,
}

/// `GET v2/volumes/{vid}/files/{lid}/revisions/{rid}` — one page of a revision's
/// block listing.
#[derive(Debug, Deserialize)]
pub struct RevisionResponse {
    #[serde(rename = "Revision")]
    pub revision: RevisionDto,
}

#[derive(Debug, Deserialize)]
pub struct RevisionDto {
    #[serde(rename = "ID")]
    pub id: String,
    /// Detached signature over the content manifest (thumbnail + block digests).
    #[serde(rename = "ManifestSignature")]
    pub manifest_signature: Option<String>,
    /// Email of the signer; empty/absent means the node key signed.
    #[serde(rename = "SignatureEmail")]
    pub signature_email: Option<String>,
    #[serde(rename = "XAttr")]
    pub extended_attributes: Option<String>,
    #[serde(rename = "Thumbnails", default)]
    pub thumbnails: Vec<ThumbnailDto>,
    #[serde(rename = "Blocks", default)]
    pub blocks: Vec<BlockDto>,
}

/// One content block of a revision.
#[derive(Debug, Deserialize)]
pub struct BlockDto {
    #[serde(rename = "Index")]
    pub index: i32,
    /// Absolute URL on block storage.
    #[serde(rename = "BareURL")]
    pub bare_url: String,
    /// Per-block storage authorization token (`pm-storage-token` header).
    #[serde(rename = "Token")]
    pub token: String,
}

#[derive(Debug, Deserialize)]
pub struct ThumbnailDto {
    /// Server-assigned thumbnail block id (C# `ThumbnailDto.Id`); resolved to a
    /// download URL via the `volumes/{vid}/thumbnails` endpoint.
    #[serde(rename = "ThumbnailID")]
    pub id: Option<String>,
    #[serde(rename = "Type")]
    pub thumbnail_type: i32,
    /// Base64 SHA-256 digest of the thumbnail's ciphertext (manifest input).
    #[serde(rename = "Hash")]
    pub hash_digest: Option<String>,
}

/// `POST volumes/{vid}/thumbnails` request: resolve thumbnail ids to download
/// URLs (C# `ThumbnailBlockListRequest`).
#[derive(Debug, Serialize)]
pub struct ThumbnailBlockListRequest {
    #[serde(rename = "ThumbnailIDs")]
    pub thumbnail_ids: Vec<String>,
}

/// `POST volumes/{vid}/thumbnails` response (C# `ThumbnailBlockListResponse`).
#[derive(Debug, Deserialize)]
pub struct ThumbnailBlockListResponse {
    #[serde(rename = "Thumbnails", default)]
    pub blocks: Vec<ThumbnailBlock>,
    #[serde(rename = "Errors", default)]
    pub errors: Vec<ThumbnailBlockError>,
}

/// A resolved thumbnail block: where to fetch it and the storage token to use
/// (C# `ThumbnailBlock`).
#[derive(Debug, Deserialize)]
pub struct ThumbnailBlock {
    #[serde(rename = "ThumbnailID")]
    pub thumbnail_id: String,
    #[serde(rename = "BareURL")]
    pub bare_url: String,
    #[serde(rename = "Token")]
    pub token: String,
}

/// Per-thumbnail resolution error (C# `ThumbnailBlockError`).
#[derive(Debug, Deserialize)]
pub struct ThumbnailBlockError {
    #[serde(rename = "ThumbnailID")]
    pub thumbnail_id: String,
    #[serde(rename = "Error")]
    pub error: String,
    #[serde(rename = "Code", default)]
    pub code: i32,
}

/// `GET v2/volumes/{vid}/folders/{lid}/children`
#[derive(Debug, Deserialize)]
pub struct FolderChildrenResponse {
    #[serde(rename = "LinkIDs")]
    pub link_ids: Vec<LinkId>,
    #[serde(rename = "AnchorID")]
    pub anchor_id: Option<LinkId>,
    #[serde(rename = "More")]
    pub more_results_exist: bool,
}

/// Drive link (node) type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LinkType {
    Folder,
    File,
    Album,
    Unknown,
}

impl LinkType {
    fn from_raw(raw: i32) -> Self {
        match raw {
            1 => Self::Folder,
            2 => Self::File,
            3 => Self::Album,
            _ => Self::Unknown,
        }
    }
}

/// Drive link state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LinkState {
    Draft = 0,
    Active = 1,
    Trashed = 2,
    Deleted = 3,
    Restoring = 4,
}

// ---- Upload (write) DTOs --------------------------------------------------

/// `POST v2/volumes/{vid}/files` — create a new file draft.
///
/// Mirrors C# `FileCreationRequest` (+ its `NodeCreationRequest` base). All
/// encrypted/armored fields are produced client-side; `Hash` is the lowercase
/// hex name HMAC, `ContentKeyPacket` is base64.
#[derive(Debug, Serialize)]
pub struct FileCreationRequest {
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "Hash")]
    pub name_hash: String,
    #[serde(rename = "ParentLinkID")]
    pub parent_link_id: LinkId,
    #[serde(rename = "NodePassphrase")]
    pub passphrase: String,
    #[serde(rename = "NodePassphraseSignature")]
    pub passphrase_signature: String,
    #[serde(rename = "NodeKey")]
    pub key: String,
    #[serde(rename = "MIMEType")]
    pub media_type: String,
    #[serde(rename = "ContentKeyPacket")]
    pub content_key_packet: String,
    #[serde(rename = "ContentKeyPacketSignature")]
    pub content_key_signature: String,
    #[serde(rename = "SignatureAddress")]
    pub signature_address: String,
    #[serde(rename = "ClientUID", skip_serializing_if = "Option::is_none")]
    pub client_uid: Option<String>,
    #[serde(rename = "IntendedUploadSize")]
    pub intended_upload_size: i64,
}

/// `POST v2/volumes/{vid}/files` response.
#[derive(Debug, Deserialize)]
pub struct FileCreationResponse {
    #[serde(rename = "File")]
    pub file: FileCreationIdentifiers,
}

#[derive(Debug, Deserialize)]
pub struct FileCreationIdentifiers {
    #[serde(rename = "ID")]
    pub link_id: LinkId,
    #[serde(rename = "RevisionID")]
    pub revision_id: String,
}

/// `POST v2/volumes/{vid}/folders` — create a new folder.
///
/// Mirrors C# `FolderCreationRequest` (+ its `NodeCreationRequest` base). Like
/// [`FileCreationRequest`] but with a `NodeHashKey` (the folder's child-name
/// HMAC key, encrypted to its own node key) instead of any content-key fields.
#[derive(Debug, Serialize)]
pub struct FolderCreationRequest {
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "Hash")]
    pub name_hash: String,
    #[serde(rename = "ParentLinkID")]
    pub parent_link_id: LinkId,
    #[serde(rename = "NodePassphrase")]
    pub passphrase: String,
    #[serde(rename = "NodePassphraseSignature")]
    pub passphrase_signature: String,
    #[serde(rename = "NodeKey")]
    pub key: String,
    #[serde(rename = "NodeHashKey")]
    pub node_hash_key: String,
    /// Folder create uses `SignatureEmail` (file create uses `SignatureAddress`).
    #[serde(rename = "SignatureEmail")]
    pub signature_email: String,
    /// Encrypted+signed `ExtendedAttributes` JSON (modification time). C#
    /// `FolderCreationRequest.ExtendedAttributes`. Omitted when no modification
    /// time was supplied.
    #[serde(rename = "XAttr", skip_serializing_if = "Option::is_none")]
    pub extended_attributes: Option<String>,
}

/// `POST volumes` request body — create a new volume with its root share and
/// root folder. Mirrors C# `VolumeCreationRequest`. All PGP fields are armored
/// strings produced by [`proton_sdk::crypto::build_volume_creation_material`].
#[derive(Debug, Serialize)]
pub struct VolumeCreationRequest {
    #[serde(rename = "AddressID")]
    pub address_id: AddressId,
    #[serde(rename = "AddressKeyID")]
    pub address_key_id: AddressKeyId,
    #[serde(rename = "ShareKey")]
    pub share_key: String,
    #[serde(rename = "SharePassphrase")]
    pub share_passphrase: String,
    #[serde(rename = "SharePassphraseSignature")]
    pub share_passphrase_signature: String,
    #[serde(rename = "FolderName")]
    pub folder_name: String,
    #[serde(rename = "FolderKey")]
    pub folder_key: String,
    #[serde(rename = "FolderPassphrase")]
    pub folder_passphrase: String,
    #[serde(rename = "FolderPassphraseSignature")]
    pub folder_passphrase_signature: String,
    #[serde(rename = "FolderHashKey")]
    pub folder_hash_key: String,
}

/// `POST v2/volumes/{vid}/folders` response.
#[derive(Debug, Deserialize)]
pub struct FolderCreationResponse {
    #[serde(rename = "Folder")]
    pub folder: FolderCreationIdentifiers,
}

#[derive(Debug, Deserialize)]
pub struct FolderCreationIdentifiers {
    #[serde(rename = "ID")]
    pub link_id: LinkId,
}

/// `PUT v2/volumes/{vid}/links/{lid}/rename` — rename a node.
///
/// Mirrors C# `RenameLinkRequest`. `Hash`/`OriginalHash` are lowercase-hex name
/// HMACs (new and current). `MIMEType` is always present: the media type for a
/// file, `null` for a folder.
#[derive(Debug, Serialize)]
pub struct RenameLinkRequest {
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "Hash")]
    pub name_hash: String,
    #[serde(rename = "NameSignatureEmail")]
    pub name_signature_email: String,
    #[serde(rename = "MIMEType")]
    pub media_type: Option<String>,
    #[serde(rename = "OriginalHash")]
    pub original_hash: String,
}

/// `PUT v2/volumes/{vid}/links/{lid}/move` — move a node to a new parent.
///
/// Mirrors C# `MoveLinkRequest`. `NodePassphrase` is the node passphrase
/// rewrapped (session-key re-encrypted) to the destination parent key; the
/// secret is unchanged, so `NodePassphraseSignature` is carried over untouched.
/// `Name` is re-encrypted + signed to the destination parent. `Hash` is the new
/// name hash under the destination's hash key; `OriginalHash` the current hash
/// under the source parent's. Same-volume moves only (no `NewShareID`).
#[derive(Debug, Serialize)]
pub struct MoveLinkRequest {
    #[serde(rename = "ParentLinkID")]
    pub parent_link_id: LinkId,
    #[serde(rename = "NodePassphrase")]
    pub passphrase: String,
    #[serde(
        rename = "NodePassphraseSignature",
        skip_serializing_if = "Option::is_none"
    )]
    pub passphrase_signature: Option<String>,
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "NameSignatureEmail")]
    pub name_signature_email: String,
    #[serde(rename = "Hash")]
    pub name_hash: String,
    #[serde(rename = "OriginalHash")]
    pub original_hash: String,
}

/// `PUT volumes/{vid}/links/move-multiple` — batch move of several nodes under a
/// single destination parent. Mirrors C# `MoveMultipleLinksRequest`. Same-volume
/// only (the C# batch path throws for cross-volume too). `SignatureEmail` is the
/// anonymous-move passphrase signer and is omitted when not set.
#[derive(Debug, Serialize)]
pub struct MoveMultipleLinksRequest {
    #[serde(rename = "ParentLinkID")]
    pub parent_link_id: LinkId,
    #[serde(rename = "Links")]
    pub links: Vec<MoveMultipleLinksItem>,
    #[serde(rename = "NameSignatureEmail")]
    pub name_signature_email: String,
    #[serde(rename = "SignatureEmail", skip_serializing_if = "Option::is_none")]
    pub signature_email: Option<String>,
}

/// One entry of a [`MoveMultipleLinksRequest`]. Mirrors C# `MoveMultipleLinksItem`:
/// per-node rewrapped passphrase + re-encrypted/signed name + new/original name
/// hashes under the destination/source hash keys.
#[derive(Debug, Serialize)]
pub struct MoveMultipleLinksItem {
    #[serde(rename = "LinkID")]
    pub link_id: LinkId,
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "NodePassphrase")]
    pub passphrase: String,
    #[serde(rename = "Hash")]
    pub name_hash: String,
    #[serde(rename = "OriginalHash")]
    pub original_hash: String,
    #[serde(
        rename = "NodePassphraseSignature",
        skip_serializing_if = "Option::is_none"
    )]
    pub passphrase_signature: Option<String>,
}

/// `POST v2/volumes/{vid}/links/{folderId}/checkAvailableHashes` request: ask
/// which of a batch of candidate name hashes are free in a folder (C#
/// `NodeNameAvailabilityRequest`).
#[derive(Debug, Serialize)]
pub struct NodeNameAvailabilityRequest {
    #[serde(rename = "Hashes")]
    pub name_hashes: Vec<String>,
    #[serde(rename = "ClientUID")]
    pub client_uid: Vec<String>,
}

/// Response to `checkAvailableHashes` (C# `NodeNameAvailabilityResponse`): the
/// subset of the requested hashes that are available, plus the taken ones.
#[derive(Debug, Deserialize)]
pub struct NodeNameAvailabilityResponse {
    #[serde(rename = "AvailableHashes", default)]
    pub available_hashes: Vec<String>,
    #[serde(rename = "PendingHashes", default)]
    pub unavailable_hashes: Vec<NameHashUnavailabilityDto>,
}

/// One taken name hash and the node that holds it (C#
/// `NameHashDigestUnavailabilityDto`).
#[derive(Debug, Deserialize)]
pub struct NameHashUnavailabilityDto {
    #[serde(rename = "Hash")]
    pub name_hash: String,
    #[serde(rename = "LinkID")]
    pub link_id: LinkId,
    #[serde(rename = "ClientUID")]
    pub client_uid: Option<String>,
}

/// `{ LinkIDs: [...] }` — batch link-id body for trash / restore / delete.
/// Mirrors C# `MultipleLinksNullaryRequest`.
#[derive(Debug, Serialize)]
pub struct MultipleLinksRequest<'a> {
    #[serde(rename = "LinkIDs")]
    pub link_ids: &'a [LinkId],
}

/// Aggregate response for batch link operations: a per-link result list. The
/// top-level envelope is `1001 MultipleResponses`; the real per-link status is
/// in each [`LinkIdResponsePair`]. Mirrors C# `AggregateApiResponse<LinkIdResponsePair>`.
#[derive(Debug, Deserialize)]
pub struct AggregateLinksResponse {
    #[serde(rename = "Responses", default)]
    pub responses: Vec<LinkIdResponsePair>,
}

#[derive(Debug, Deserialize)]
pub struct LinkIdResponsePair {
    #[serde(rename = "LinkID")]
    pub link_id: LinkId,
    #[serde(rename = "Response")]
    pub response: proton_sdk::api::ApiResponse,
}

/// `POST v2/volumes/{vid}/files/{lid}/revisions` — create a new revision on an
/// existing file. Mirrors C# `RevisionCreationRequest`.
#[derive(Debug, Serialize)]
pub struct RevisionCreationRequest {
    /// The currently active revision this draft supersedes.
    #[serde(rename = "CurrentRevisionID")]
    pub current_revision_id: String,
    #[serde(rename = "ClientUID", skip_serializing_if = "Option::is_none")]
    pub client_uid: Option<String>,
    #[serde(rename = "IntendedUploadSize")]
    pub intended_upload_size: i64,
}

/// `POST v2/volumes/{vid}/files/{lid}/revisions` response.
#[derive(Debug, Deserialize)]
pub struct RevisionCreationResponse {
    #[serde(rename = "Revision")]
    pub revision: RevisionCreationIdentity,
}

#[derive(Debug, Deserialize)]
pub struct RevisionCreationIdentity {
    #[serde(rename = "ID")]
    pub revision_id: String,
}

/// `GET v2/volumes/{vid}/links/{lid}/revisions/{rid}/verification`.
#[derive(Debug, Deserialize)]
pub struct BlockVerificationInputResponse {
    /// Base64 verification code XORed with the block ciphertext prefix.
    #[serde(rename = "VerificationCode")]
    pub verification_code: String,
    /// Base64 content key packet (re-encrypted to the node key) for the check.
    #[serde(rename = "ContentKeyPacket")]
    pub content_key_packet: String,
}

/// `POST blocks` — request upload targets for content/thumbnail blocks.
///
/// Mirrors C# `BlockUploadPreparationRequest`.
#[derive(Debug, Serialize)]
pub struct BlockUploadPreparationRequest {
    #[serde(rename = "AddressID")]
    pub address_id: AddressId,
    #[serde(rename = "VolumeID")]
    pub volume_id: VolumeId,
    #[serde(rename = "LinkID")]
    pub link_id: LinkId,
    #[serde(rename = "RevisionID")]
    pub revision_id: String,
    #[serde(rename = "BlockList")]
    pub blocks: Vec<BlockCreationRequest>,
    #[serde(rename = "ThumbnailList")]
    pub thumbnails: Vec<ThumbnailCreationRequest>,
}

#[derive(Debug, Serialize)]
pub struct BlockCreationRequest {
    #[serde(rename = "Index")]
    pub index: i32,
    #[serde(rename = "Size")]
    pub size: i32,
    /// Armored PGP message: the block's detached signature, encrypted to the
    /// node key.
    #[serde(rename = "EncSignature")]
    pub encrypted_signature: String,
    /// Base64 SHA-256 of the block ciphertext.
    #[serde(rename = "Hash")]
    pub hash: String,
    #[serde(rename = "Verifier")]
    pub verifier: BlockVerifier,
}

#[derive(Debug, Serialize)]
pub struct BlockVerifier {
    /// Base64 verification token (`code XOR ciphertext_prefix`).
    #[serde(rename = "Token")]
    pub token: String,
}

/// Thumbnail creation entry in a block-upload preparation request. Mirrors C#
/// `ThumbnailCreationRequest` (`Size`, `Type`, base64 ciphertext `Hash`).
#[derive(Debug, Serialize)]
pub struct ThumbnailCreationRequest {
    #[serde(rename = "Size")]
    pub size: i32,
    #[serde(rename = "Type")]
    pub thumbnail_type: i32,
    #[serde(rename = "Hash")]
    pub hash: String,
}

/// `POST blocks` response.
#[derive(Debug, Deserialize)]
pub struct BlockUploadPreparationResponse {
    #[serde(rename = "UploadLinks")]
    pub upload_targets: Vec<BlockUploadTarget>,
    #[serde(rename = "ThumbnailLinks", default)]
    pub thumbnail_upload_targets: Vec<BlockUploadTarget>,
}

#[derive(Debug, Deserialize)]
pub struct BlockUploadTarget {
    #[serde(rename = "BareURL")]
    pub bare_url: String,
    #[serde(rename = "Token")]
    pub token: String,
}

/// `PUT v2/volumes/{vid}/files/{lid}/revisions/{rid}` — seal the revision.
///
/// Mirrors C# `RevisionUpdateRequest`.
#[derive(Debug, Serialize)]
pub struct RevisionUpdateRequest {
    #[serde(rename = "ManifestSignature")]
    pub manifest_signature: String,
    #[serde(rename = "SignatureAddress")]
    pub signature_address: String,
    #[serde(rename = "ChecksumVerified")]
    pub checksum_verified: bool,
    #[serde(rename = "XAttr", skip_serializing_if = "Option::is_none")]
    pub extended_attributes: Option<String>,
    /// Photo-specific seal metadata (capture time, content hash, tags). Present
    /// only for photo uploads. Mirrors C# `RevisionUpdateRequest.PhotosAttributes`
    /// (`[JsonPropertyName("Photo")]`).
    #[serde(rename = "Photo", skip_serializing_if = "Option::is_none")]
    pub photos_attributes: Option<PhotosAttributesDto>,
}

/// Photo-specific revision attributes, attached to the seal request for photo
/// uploads. Mirrors C# `PhotosAttributesDto`.
#[derive(Debug, Serialize)]
pub struct PhotosAttributesDto {
    /// Capture time in seconds since the Unix epoch (C# `EpochSecondsJsonConverter`).
    #[serde(rename = "CaptureTime")]
    pub capture_time: i64,
    /// Lowercase-hex HMAC-SHA256 of the lowercase-hex plaintext SHA-1, keyed by
    /// the parent folder's hash key (C# `ContentHashDigest`,
    /// `ForgivingBytesToHexJsonConverter`).
    #[serde(rename = "ContentHash")]
    pub content_hash: String,
    /// Link id of the main photo, when this is a related photo (live/burst).
    #[serde(rename = "MainPhotoLinkID", skip_serializing_if = "Option::is_none")]
    pub main_photo_link_id: Option<LinkId>,
    /// Photo classification tags (their `PhotoTag` discriminants); always
    /// present, empty when none (C# `Tags ?? []`).
    #[serde(rename = "Tags")]
    pub tags: Vec<i32>,
}

/// The decrypted `XAttr` JSON payload for a revision (encrypted to the node key
/// before upload). Mirrors C# `ExtendedAttributes` / `CommonExtendedAttributes`.
#[derive(Debug, Serialize)]
pub struct ExtendedAttributes {
    #[serde(rename = "Common")]
    pub common: CommonExtendedAttributes,
}

/// All fields are optional, mirroring C# `CommonExtendedAttributes` (every
/// property is nullable): a file-upload seal sets size/block-sizes/digests and
/// optionally a modification time, while a folder create sets only the
/// modification time. Unset fields are omitted from the JSON.
#[derive(Debug, Serialize)]
pub struct CommonExtendedAttributes {
    #[serde(rename = "Size", skip_serializing_if = "Option::is_none")]
    pub size: Option<i64>,
    /// ISO-8601 UTC modification timestamp (C# `CommonExtendedAttributes
    /// .ModificationTime`, written via `Iso8601DateTimeResultJsonConverter`).
    #[serde(rename = "ModificationTime", skip_serializing_if = "Option::is_none")]
    pub modification_time: Option<String>,
    #[serde(rename = "BlockSizes", skip_serializing_if = "Option::is_none")]
    pub block_sizes: Option<Vec<i32>>,
    #[serde(rename = "Digests", skip_serializing_if = "Option::is_none")]
    pub digests: Option<FileContentDigests>,
}

#[derive(Debug, Serialize)]
pub struct FileContentDigests {
    /// Lowercase hex SHA-1 of the full plaintext.
    #[serde(rename = "SHA1")]
    pub sha1: String,
}

/// `GET volumes/{vid}/events/latest` — seeds the enumeration cursor.
/// C# `LatestVolumeEventResponse`.
#[derive(Debug, Deserialize)]
pub struct LatestVolumeEventResponse {
    #[serde(rename = "EventID")]
    pub event_id: DriveEventId,
}

/// `GET v2/volumes/{vid}/events/{cursor}` — one page of volume events.
/// C# `VolumeEventListResponse`.
#[derive(Debug, Deserialize)]
pub struct VolumeEventListResponse {
    /// Cursor to use for the next request (the last event id in this page).
    #[serde(rename = "EventID")]
    pub last_event_id: DriveEventId,
    #[serde(rename = "Events", default)]
    pub events: Vec<VolumeEventDto>,
    /// More pages exist beyond this one.
    #[serde(rename = "More")]
    pub more_entries_exist: bool,
    /// Continuity lost — caller must resync from server state.
    #[serde(rename = "Refresh")]
    pub refresh_required: bool,
}

/// A single volume event. C# `VolumeEventDto`.
#[derive(Debug, Deserialize)]
pub struct VolumeEventDto {
    #[serde(rename = "EventID")]
    pub id: DriveEventId,
    /// `VolumeEventType`: 0 = Delete, 1 = Create, 2 = Update, 3 = UpdateMetadata.
    #[serde(rename = "EventType")]
    pub event_type: i32,
    #[serde(rename = "Link")]
    pub link: VolumeEventLinkDto,
}

/// The affected link of a volume event. C# `VolumeEventLinkDto`.
#[derive(Debug, Deserialize)]
pub struct VolumeEventLinkDto {
    #[serde(rename = "LinkID")]
    pub id: LinkId,
    #[serde(rename = "ParentLinkID")]
    pub parent_id: Option<LinkId>,
    #[serde(rename = "IsShared", default)]
    pub is_shared: bool,
    #[serde(rename = "IsTrashed", default)]
    pub is_trashed: bool,
}

/// `GET shares/{sid}` — a share and the material needed to unlock its key.
/// C# `ShareResponse`; the share fields sit at the top level of the envelope, so
/// they are flattened into the same [`ShareDto`] the my-files lookup returns.
#[derive(Debug, Deserialize)]
pub struct ShareResponse {
    #[serde(flatten)]
    pub share: ShareDto,
    #[serde(rename = "VolumeID")]
    pub volume_id: VolumeId,
    #[serde(rename = "LinkID")]
    pub root_link_id: LinkId,
}

/// `GET v2/sharedwithme` — one page of the items other users share with us.
/// C# `SharedWithMeResponse`.
#[derive(Debug, Deserialize)]
pub struct SharedWithMeResponse {
    #[serde(rename = "Links", default)]
    pub links: Vec<SharedWithMeLinkDto>,
    /// Cursor for the next page.
    #[serde(rename = "AnchorID", default)]
    pub anchor_id: Option<String>,
    #[serde(rename = "More", default)]
    pub more: bool,
}

/// One shared-with-me item. C# `SharedWithMeLinkDto`.
#[derive(Debug, Deserialize)]
pub struct SharedWithMeLinkDto {
    #[serde(rename = "VolumeID")]
    pub volume_id: VolumeId,
    #[serde(rename = "ShareID")]
    pub share_id: ShareId,
    #[serde(rename = "LinkID")]
    pub link_id: LinkId,
    /// What kind of item is shared. See [`ShareTargetType`].
    #[serde(rename = "ShareTargetType", default)]
    pub share_target_type: i32,
}

/// `GET drive/v2/volumes/{volumeID}/shares` — one page of the collaborative
/// shares I own that are still live (have members, invitations or a public URL).
/// TS SDK `SharedByMeResponseDto`.
#[derive(Debug, Deserialize)]
pub struct SharedByMeResponse {
    #[serde(rename = "Links", default)]
    pub links: Vec<SharedByMeLinkDto>,
    /// Cursor for the next page.
    #[serde(rename = "AnchorID", default)]
    pub anchor_id: Option<String>,
    #[serde(rename = "More", default)]
    pub more: bool,
}

/// One shared-by-me item. TS SDK `LinkSharedByMeResponseDto`. The volume is the
/// one queried, so only the link (and its share) come back per entry.
#[derive(Debug, Deserialize)]
pub struct SharedByMeLinkDto {
    #[serde(rename = "ShareID")]
    pub share_id: ShareId,
    #[serde(rename = "LinkID")]
    pub link_id: LinkId,
}

/// The kind of item a share points at. C# `ShareTargetType`.
///
/// The Drive client exposes folders, files and vendor items; albums and photos
/// belong to the Photos client (C# `SharingOperations.DriveShareTargetTypes`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum ShareTargetType {
    Root = 0,
    Folder = 1,
    File = 2,
    Album = 3,
    Photo = 4,
    ProtonVendor = 5,
}

impl ShareTargetType {
    pub fn from_raw(value: i32) -> Option<Self> {
        match value {
            0 => Some(Self::Root),
            1 => Some(Self::Folder),
            2 => Some(Self::File),
            3 => Some(Self::Album),
            4 => Some(Self::Photo),
            5 => Some(Self::ProtonVendor),
            _ => None,
        }
    }

    /// Whether the Drive client (rather than the Photos client) owns this kind.
    pub fn is_drive_item(self) -> bool {
        matches!(self, Self::Folder | Self::File | Self::ProtonVendor)
    }
}

/// `GET devices` — the account's registered devices. C# `DeviceListResponse`.
#[derive(Debug, Deserialize)]
pub struct DeviceListResponse {
    #[serde(rename = "Devices", default)]
    pub devices: Vec<DeviceListItemDto>,
}

/// C# `DeviceListItemDto`: a device and the share holding its root folder.
#[derive(Debug, Deserialize)]
pub struct DeviceListItemDto {
    #[serde(rename = "Device")]
    pub device: DeviceDataDto,
    #[serde(rename = "Share")]
    pub share: DeviceShareDataDto,
}

/// C# `DeviceDataDto`.
#[derive(Debug, Deserialize)]
pub struct DeviceDataDto {
    #[serde(rename = "DeviceID")]
    pub id: DeviceUid,
    #[serde(rename = "VolumeID")]
    pub volume_id: VolumeId,
    /// `DeviceType`: 1 = Windows, 2 = macOS, 3 = Linux.
    #[serde(rename = "Type")]
    pub device_type: i32,
    #[serde(rename = "CreateTime")]
    pub creation_time: i64,
    #[serde(rename = "LastSyncTime", default)]
    pub last_sync_time: Option<i64>,
}

/// C# `DeviceShareDataDto`. `Name` is the *deprecated* device name: it used to
/// live on the share and must be cleared when renaming an old device.
#[derive(Debug, Deserialize)]
pub struct DeviceShareDataDto {
    #[serde(rename = "ShareID")]
    pub id: ShareId,
    #[serde(rename = "LinkID")]
    pub root_link_id: LinkId,
    #[serde(rename = "Name", default)]
    pub name: Option<String>,
}

/// `POST devices` — register a device with its own share and root folder.
/// C# `DeviceCreationRequest`.
#[derive(Debug, Serialize)]
pub struct DeviceCreationRequest {
    #[serde(rename = "Device")]
    pub device: DeviceCreationDeviceDto,
    #[serde(rename = "Share")]
    pub share: DeviceCreationShareDto,
    #[serde(rename = "Link")]
    pub link: DeviceCreationLinkDto,
}

#[derive(Debug, Serialize)]
pub struct DeviceCreationDeviceDto {
    #[serde(rename = "Type")]
    pub device_type: i32,
    /// Synchronisation state; 0 (off) when registering a new device.
    #[serde(rename = "SyncState")]
    pub sync_state: i32,
}

#[derive(Debug, Serialize)]
pub struct DeviceCreationShareDto {
    #[serde(rename = "AddressID")]
    pub address_id: AddressId,
    #[serde(rename = "AddressKeyID")]
    pub address_key_id: AddressKeyId,
    #[serde(rename = "Key")]
    pub key: String,
    #[serde(rename = "Passphrase")]
    pub passphrase: String,
    #[serde(rename = "PassphraseSignature")]
    pub passphrase_signature: String,
}

#[derive(Debug, Serialize)]
pub struct DeviceCreationLinkDto {
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "NodeKey")]
    pub key: String,
    #[serde(rename = "NodePassphrase")]
    pub passphrase: String,
    #[serde(rename = "NodePassphraseSignature")]
    pub passphrase_signature: String,
    #[serde(rename = "NodeHashKey")]
    pub node_hash_key: String,
}

/// `POST devices` response. C# `DeviceCreationResponse`.
#[derive(Debug, Deserialize)]
pub struct DeviceCreationResponse {
    #[serde(rename = "Device")]
    pub device: DeviceCreationResultDto,
}

#[derive(Debug, Deserialize)]
pub struct DeviceCreationResultDto {
    #[serde(rename = "DeviceID")]
    pub id: DeviceUid,
    #[serde(rename = "ShareID")]
    pub share_id: ShareId,
    #[serde(rename = "LinkID")]
    pub root_link_id: LinkId,
}

/// `PUT devices/{uid}` — only ever used to clear the deprecated share-held name.
/// C# `DeviceUpdateRequest`.
#[derive(Debug, Serialize)]
pub struct DeviceUpdateRequest {
    #[serde(rename = "Share")]
    pub share: DeviceUpdateShareDto,
}

#[derive(Debug, Serialize)]
pub struct DeviceUpdateShareDto {
    #[serde(rename = "Name")]
    pub name: String,
}

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

    #[test]
    fn only_drive_share_targets_are_drive_items() {
        // C# `SharingOperations.DriveShareTargetTypes`; albums and photos belong
        // to the Photos client, and a root share is not a shared item.
        for (raw, expected) in [
            (0, false),
            (1, true),
            (2, true),
            (3, false),
            (4, false),
            (5, true),
        ] {
            let kind = ShareTargetType::from_raw(raw).expect("known target type");
            assert_eq!(kind.is_drive_item(), expected, "target type {raw}");
        }
        assert!(ShareTargetType::from_raw(6).is_none());
    }
}

// ---------------------------------------------------------------------------
// Sharing: creating shares, inviting Proton users, members + invitations.
// Ported from the TypeScript SDK (`internal/sharing/apiService.ts`); the C#
// public SDK does not expose share creation.
// ---------------------------------------------------------------------------

/// `POST volumes/{volumeID}/shares` — create a standard share on a node.
#[derive(Debug, Serialize)]
pub struct CreateShareRequest {
    #[serde(rename = "RootLinkID")]
    pub root_link_id: LinkId,
    #[serde(rename = "AddressID")]
    pub address_id: AddressId,
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "ShareKey")]
    pub share_key: String,
    #[serde(rename = "SharePassphrase")]
    pub share_passphrase: String,
    #[serde(rename = "SharePassphraseSignature")]
    pub share_passphrase_signature: String,
    #[serde(rename = "PassphraseKeyPacket")]
    pub passphrase_key_packet: String,
    #[serde(rename = "NameKeyPacket")]
    pub name_key_packet: String,
}

#[derive(Debug, Deserialize)]
pub struct CreateShareResponse {
    #[serde(rename = "Share")]
    pub share: CreatedShareDto,
}

#[derive(Debug, Deserialize)]
pub struct CreatedShareDto {
    #[serde(rename = "ID")]
    pub id: ShareId,
    #[serde(rename = "EditorsCanShare", default)]
    pub editors_can_share: bool,
}

/// `POST v2/shares/{shareID}/invitations` — invite a Proton user.
#[derive(Debug, Serialize)]
pub struct InviteProtonUserRequest {
    #[serde(rename = "Invitation")]
    pub invitation: InviteProtonUserInvitationDto,
    #[serde(rename = "EmailDetails")]
    pub email_details: InviteEmailDetailsDto,
}

#[derive(Debug, Serialize)]
pub struct InviteProtonUserInvitationDto {
    #[serde(rename = "InviterEmail")]
    pub inviter_email: String,
    #[serde(rename = "InviteeEmail")]
    pub invitee_email: String,
    #[serde(rename = "Permissions")]
    pub permissions: i32,
    #[serde(rename = "KeyPacket")]
    pub key_packet: String,
    #[serde(rename = "KeyPacketSignature")]
    pub key_packet_signature: String,
    #[serde(
        rename = "ExternalInvitationID",
        skip_serializing_if = "Option::is_none"
    )]
    pub external_invitation_id: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct InviteEmailDetailsDto {
    #[serde(rename = "Message", skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(rename = "ItemName", skip_serializing_if = "Option::is_none")]
    pub item_name: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct InviteProtonUserResponse {
    #[serde(rename = "Invitation")]
    pub invitation: ShareInvitationDto,
}

/// `GET v2/shares/{shareID}/invitations`
#[derive(Debug, Deserialize)]
pub struct ShareInvitationsResponse {
    #[serde(rename = "Invitations", default)]
    pub invitations: Vec<ShareInvitationDto>,
}

#[derive(Debug, Deserialize)]
pub struct ShareInvitationDto {
    #[serde(rename = "InvitationID")]
    pub invitation_id: String,
    #[serde(rename = "InviterEmail")]
    pub inviter_email: String,
    #[serde(rename = "InviteeEmail")]
    pub invitee_email: String,
    #[serde(rename = "Permissions", default)]
    pub permissions: Option<i32>,
    #[serde(rename = "CreateTime", default)]
    pub create_time: i64,
}

/// `GET v2/shares/{shareID}/members`
#[derive(Debug, Deserialize)]
pub struct ShareMembersResponse {
    #[serde(rename = "Members", default)]
    pub members: Vec<ShareMemberDto>,
}

#[derive(Debug, Deserialize)]
pub struct ShareMemberDto {
    #[serde(rename = "MemberID")]
    pub member_id: ShareMembershipId,
    #[serde(rename = "InviterEmail")]
    pub inviter_email: String,
    #[serde(rename = "Email")]
    pub email: String,
    #[serde(rename = "Permissions", default)]
    pub permissions: Option<i32>,
    #[serde(rename = "CreateTime", default)]
    pub create_time: i64,
}

/// `PUT v2/shares/{shareID}/members/{memberID}` and the invitation equivalent.
#[derive(Debug, Serialize)]
pub struct UpdatePermissionsRequest {
    #[serde(rename = "Permissions")]
    pub permissions: i32,
}

// ---------------------------------------------------------------------------
// Incoming invitations: invitations addressed to the current user, and
// accept/reject. `GET v2/shares/invitations` lists them; the detail, accept and
// reject routes are keyed by invitation id alone.
// ---------------------------------------------------------------------------

/// `GET v2/shares/invitations` — the invitations where we are the invitee.
#[derive(Debug, Deserialize)]
pub struct InvitationsListResponse {
    #[serde(rename = "Invitations", default)]
    pub invitations: Vec<InvitationListItemDto>,
    #[serde(rename = "More", default)]
    pub more: bool,
    #[serde(rename = "AnchorID", default)]
    pub anchor_id: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct InvitationListItemDto {
    #[serde(rename = "InvitationID")]
    pub invitation_id: String,
    #[serde(rename = "ShareID", default)]
    pub share_id: String,
    #[serde(rename = "ShareTargetType", default)]
    pub share_target_type: i32,
}

/// `GET v2/shares/invitations/{invitationID}` — the encrypted invitation, the
/// share crypto needed to decrypt it, and the shared node's link.
#[derive(Debug, Deserialize)]
pub struct InvitationDetailsResponse {
    #[serde(rename = "Invitation")]
    pub invitation: IncomingInvitationDto,
    #[serde(rename = "Share")]
    pub share: InvitationShareDto,
    #[serde(rename = "Link")]
    pub link: InvitationLinkDto,
}

#[derive(Debug, Deserialize)]
pub struct IncomingInvitationDto {
    #[serde(rename = "InvitationID")]
    pub invitation_id: String,
    #[serde(rename = "InviterEmail")]
    pub inviter_email: String,
    #[serde(rename = "InviteeEmail")]
    pub invitee_email: String,
    #[serde(rename = "KeyPacket")]
    pub key_packet: String,
    #[serde(rename = "KeyPacketSignature", default)]
    pub key_packet_signature: String,
    #[serde(rename = "Permissions", default)]
    pub permissions: Option<i32>,
    #[serde(rename = "CreateTime", default)]
    pub create_time: i64,
}

#[derive(Debug, Deserialize)]
pub struct InvitationShareDto {
    #[serde(rename = "ShareKey")]
    pub share_key: String,
    #[serde(rename = "Passphrase")]
    pub passphrase: String,
    #[serde(rename = "CreatorEmail", default)]
    pub creator_email: String,
    #[serde(rename = "VolumeID")]
    pub volume_id: String,
    #[serde(rename = "ShareTargetType", default)]
    pub share_target_type: i32,
}

#[derive(Debug, Deserialize)]
pub struct InvitationLinkDto {
    #[serde(rename = "LinkID")]
    pub link_id: String,
    #[serde(rename = "Type", default)]
    pub link_type: i32,
    #[serde(rename = "MIMEType", default)]
    pub mime_type: Option<String>,
    #[serde(rename = "Name")]
    pub name: String,
}

/// `POST v2/shares/invitations/{invitationID}/accept`.
#[derive(Debug, Serialize)]
pub struct AcceptInvitationRequest {
    #[serde(rename = "SessionKeySignature")]
    pub session_key_signature: String,
}

// ---------------------------------------------------------------------------
// External (non-Proton) invitations. Same share-invitation model, but the
// invitee has no Proton key, so instead of a key packet the inviter carries a
// detached signature over the invitee email + share session key.
// ---------------------------------------------------------------------------

/// `POST v2/shares/{shareID}/external-invitations`.
#[derive(Debug, Serialize)]
pub struct InviteExternalUserRequest {
    #[serde(rename = "ExternalInvitation")]
    pub external_invitation: ExternalInvitationDto,
    #[serde(rename = "EmailDetails")]
    pub email_details: InviteEmailDetailsDto,
}

#[derive(Debug, Serialize)]
pub struct ExternalInvitationDto {
    #[serde(rename = "InviterAddressID")]
    pub inviter_address_id: AddressId,
    #[serde(rename = "InviteeEmail")]
    pub invitee_email: String,
    #[serde(rename = "Permissions")]
    pub permissions: i32,
    #[serde(rename = "ExternalInvitationSignature")]
    pub external_invitation_signature: String,
}

#[derive(Debug, Deserialize)]
pub struct InviteExternalUserResponse {
    #[serde(rename = "ExternalInvitation")]
    pub external_invitation: ExternalInvitationResponseDto,
}

/// `GET v2/shares/{shareID}/external-invitations`.
#[derive(Debug, Deserialize)]
pub struct ExternalInvitationsResponse {
    #[serde(rename = "ExternalInvitations", default)]
    pub external_invitations: Vec<ExternalInvitationResponseDto>,
}

#[derive(Debug, Deserialize)]
pub struct ExternalInvitationResponseDto {
    #[serde(rename = "ExternalInvitationID")]
    pub external_invitation_id: String,
    #[serde(rename = "InviterEmail")]
    pub inviter_email: String,
    #[serde(rename = "InviteeEmail")]
    pub invitee_email: String,
    #[serde(rename = "Permissions", default)]
    pub permissions: Option<i32>,
    #[serde(rename = "CreateTime", default)]
    pub create_time: i64,
    /// `1` = pending (invitee has no Proton account yet), else user-registered.
    #[serde(rename = "State", default)]
    pub state: i32,
}

// ---------------------------------------------------------------------------
// Bookmarks: public links the user has saved to their account.
// ---------------------------------------------------------------------------

/// `GET v2/shared-bookmarks`.
#[derive(Debug, Deserialize)]
pub struct BookmarksResponse {
    #[serde(rename = "Bookmarks", default)]
    pub bookmarks: Vec<BookmarkDto>,
}

#[derive(Debug, Deserialize)]
pub struct BookmarkDto {
    #[serde(rename = "Token")]
    pub token: BookmarkTokenDto,
    #[serde(rename = "CreateTime", default)]
    pub create_time: i64,
    #[serde(rename = "EncryptedUrlPassword", default)]
    pub encrypted_url_password: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct BookmarkTokenDto {
    #[serde(rename = "Token")]
    pub token: String,
    #[serde(rename = "ShareKey")]
    pub share_key: String,
    #[serde(rename = "SharePassphrase")]
    pub share_passphrase: String,
    #[serde(rename = "SharePasswordSalt")]
    pub share_password_salt: String,
    #[serde(rename = "LinkType", default)]
    pub link_type: i32,
    #[serde(rename = "MIMEType", default)]
    pub mime_type: Option<String>,
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "NodeKey", default)]
    pub node_key: String,
    #[serde(rename = "NodePassphrase", default)]
    pub node_passphrase: String,
    #[serde(rename = "ContentKeyPacket", default)]
    pub content_key_packet: Option<String>,
}

/// `POST v2/urls/{token}/bookmark`.
#[derive(Debug, Serialize)]
pub struct CreateBookmarkRequest {
    #[serde(rename = "BookmarkShareURL")]
    pub bookmark_share_url: BookmarkShareUrlDto,
}

#[derive(Debug, Serialize)]
pub struct BookmarkShareUrlDto {
    #[serde(rename = "EncryptedUrlPassword")]
    pub encrypted_url_password: String,
    #[serde(rename = "AddressID")]
    pub address_id: AddressId,
    #[serde(rename = "AddressKeyID")]
    pub address_key_id: AddressKeyId,
}

// ---------------------------------------------------------------------------
// Public share links (`shares/{shareID}/urls`) + SRP modulus fetch.
// ---------------------------------------------------------------------------

/// `POST auth/v4/modulus` (root route) — a fresh signed SRP modulus.
#[derive(Debug, Deserialize)]
pub struct ModulusResponse {
    #[serde(rename = "Modulus")]
    pub modulus: String,
    #[serde(rename = "ModulusID")]
    pub modulus_id: String,
}

/// `POST shares/{shareID}/urls` — create a public link.
#[derive(Debug, Serialize)]
pub struct CreatePublicLinkRequest {
    #[serde(rename = "CreatorEmail")]
    pub creator_email: String,
    #[serde(rename = "Permissions")]
    pub permissions: i32,
    #[serde(rename = "Flags")]
    pub flags: i32,
    #[serde(rename = "ExpirationTime", skip_serializing_if = "Option::is_none")]
    pub expiration_time: Option<i64>,
    #[serde(rename = "SharePasswordSalt")]
    pub share_password_salt: String,
    #[serde(rename = "SharePassphraseKeyPacket")]
    pub share_passphrase_key_packet: String,
    #[serde(rename = "Password")]
    pub password: String,
    #[serde(rename = "UrlPasswordSalt")]
    pub url_password_salt: String,
    #[serde(rename = "SRPVerifier")]
    pub srp_verifier: String,
    #[serde(rename = "SRPModulusID")]
    pub srp_modulus_id: String,
    #[serde(rename = "MaxAccesses")]
    pub max_accesses: i32,
}

#[derive(Debug, Deserialize)]
pub struct CreatePublicLinkResponse {
    #[serde(rename = "ShareURL")]
    pub share_url: ShareUrlDto,
}

/// `GET shares/{shareID}/urls`
#[derive(Debug, Deserialize)]
pub struct ShareUrlsResponse {
    #[serde(rename = "ShareURLs", default)]
    pub share_urls: Vec<ShareUrlDto>,
}

#[derive(Debug, Deserialize)]
pub struct ShareUrlDto {
    #[serde(rename = "ShareURLID")]
    pub share_url_id: String,
    #[serde(rename = "ShareID", default)]
    pub share_id: Option<ShareId>,
    #[serde(rename = "PublicUrl", default)]
    pub public_url: String,
    /// The link password (generated portion + any custom password), PGP-encrypted
    /// to the share creator's address key. Decrypting it recovers the secret URL
    /// fragment. Absent on the create response, present when listing.
    #[serde(rename = "Password", default)]
    pub password: Option<String>,
    #[serde(rename = "CreateTime", default)]
    pub create_time: i64,
    #[serde(rename = "ExpirationTime", default)]
    pub expiration_time: Option<i64>,
    #[serde(rename = "Permissions", default)]
    pub permissions: Option<i32>,
    #[serde(rename = "Flags", default)]
    pub flags: i32,
    #[serde(rename = "NumAccesses", default)]
    pub num_accesses: i64,
}