inline-sdk 0.4.0

Rust SDK for Inline API calls, uploads, and realtime RPC.
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
//! HTTP API helpers for auth, uploads, and selected REST-style Inline endpoints.

use reqwest::{Client, StatusCode};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::fmt;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
use thiserror::Error;
use url::Url;

use crate::client_info::{self, AuthMetadata, ClientIdentity};

/// Default timeout for API HTTP requests made by SDK-created clients.
pub const DEFAULT_API_TIMEOUT: Duration = Duration::from_secs(60);

/// Error returned by [`ApiClient`] HTTP calls.
#[derive(Error)]
#[non_exhaustive]
pub enum ApiError {
    /// Invalid Inline API base URL supplied to an API client builder.
    #[error("invalid API base URL: {message}")]
    InvalidBaseUrl {
        /// Original URL value supplied by the caller.
        url: String,
        /// Human-readable validation failure.
        message: String,
    },
    /// A network, TLS, redirect, or HTTP client error from `reqwest`.
    #[error("http error: {0}")]
    Http(#[from] reqwest::Error),
    /// Local file system error, currently used when reading upload inputs.
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
    /// JSON encoding or decoding error.
    #[error("json error: {0}")]
    Json(#[from] serde_json::Error),
    /// SDK input validation error before sending a request.
    #[error("invalid input: {message}")]
    InvalidInput {
        /// Human-readable validation failure.
        message: String,
    },
    /// Non-success HTTP status where the body could not be decoded as an Inline error.
    #[error("api request failed with HTTP {status}: {message}")]
    Status {
        /// HTTP status code.
        status: u16,
        /// Human-readable HTTP status text.
        message: String,
        /// Short normalized response-body preview, when available.
        body: Option<String>,
    },
    /// Inline API error decoded from a structured response body.
    #[error("api error: {error}: {description}")]
    Api {
        /// HTTP status code, when the error came from an HTTP response.
        status: Option<u16>,
        /// Stable server error name or fallback error label.
        error: String,
        /// Numeric server error code, when provided.
        error_code: Option<i32>,
        /// Human-readable error description.
        description: String,
    },
}

impl fmt::Debug for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ApiError::InvalidBaseUrl { url, message } => f
                .debug_struct("InvalidBaseUrl")
                .field("url", &api_base_url_for_debug(url))
                .field("message", message)
                .finish(),
            ApiError::Http(error) => f.debug_tuple("Http").field(error).finish(),
            ApiError::Io(error) => f.debug_tuple("Io").field(error).finish(),
            ApiError::Json(error) => f.debug_tuple("Json").field(error).finish(),
            ApiError::InvalidInput { message } => f
                .debug_struct("InvalidInput")
                .field("message", message)
                .finish(),
            ApiError::Status {
                status,
                message,
                body,
            } => f
                .debug_struct("Status")
                .field("status", status)
                .field("message", message)
                .field("body", body)
                .finish(),
            ApiError::Api {
                status,
                error,
                error_code,
                description,
            } => f
                .debug_struct("Api")
                .field("status", status)
                .field("error", error)
                .field("error_code", error_code)
                .field("description", description)
                .finish(),
        }
    }
}

/// Thin HTTP client for Inline API endpoints.
#[must_use]
#[derive(Clone)]
pub struct ApiClient {
    base_url: String,
    http: Client,
    request_timeout: Option<Duration>,
}

impl fmt::Debug for ApiClient {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ApiClient")
            .field("base_url", &self.base_url)
            .field("http", &"<reqwest::Client>")
            .field("request_timeout", &self.request_timeout)
            .finish()
    }
}

/// Builder for [`ApiClient`].
#[must_use]
#[derive(Clone)]
pub struct ApiClientBuilder {
    base_url: String,
    identity: ClientIdentity,
    http: Option<Client>,
    request_timeout: Option<Duration>,
}

impl fmt::Debug for ApiClientBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ApiClientBuilder")
            .field("base_url", &api_base_url_for_debug(&self.base_url))
            .field("identity", &self.identity)
            .field(
                "http",
                &self.http.as_ref().map(|_| "<custom reqwest::Client>"),
            )
            .field("request_timeout", &self.request_timeout)
            .finish()
    }
}

impl ApiClient {
    /// Starts an [`ApiClient`] builder for an Inline API base URL.
    pub fn builder(base_url: impl Into<String>) -> ApiClientBuilder {
        ApiClientBuilder::new(base_url)
    }

    /// Creates an API client with the default SDK identity.
    pub fn try_new(base_url: impl Into<String>) -> Result<Self, ApiError> {
        Self::builder(base_url).build()
    }

    /// Creates an API client with a custom client identity.
    pub fn try_new_with_identity(
        base_url: impl Into<String>,
        identity: ClientIdentity,
    ) -> Result<Self, ApiError> {
        Self::builder(base_url).identity(identity).build()
    }

    /// Returns the normalized API base URL.
    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// Returns the underlying `reqwest` client.
    pub fn http_client(&self) -> &Client {
        &self.http
    }

    /// Returns the request timeout configured by the SDK builder.
    ///
    /// This is `None` when the caller provided a custom `reqwest` client.
    pub fn request_timeout(&self) -> Option<Duration> {
        self.request_timeout
    }
}

impl ApiClientBuilder {
    /// Creates a builder with the default SDK identity.
    pub fn new(base_url: impl Into<String>) -> Self {
        Self {
            base_url: base_url.into(),
            identity: ClientIdentity::sdk(),
            http: None,
            request_timeout: Some(DEFAULT_API_TIMEOUT),
        }
    }

    /// Sets the client identity used for default HTTP headers and user agent.
    pub fn identity(mut self, identity: ClientIdentity) -> Self {
        self.identity = identity;
        self
    }

    /// Uses a caller-provided `reqwest` client.
    ///
    /// When this is set, the SDK does not inject identity headers into that
    /// client and does not apply request timeout settings; configure them
    /// before passing the client if needed.
    pub fn http_client(mut self, http: Client) -> Self {
        self.http = Some(http);
        self
    }

    /// Sets the timeout for API HTTP requests made by SDK-created clients.
    pub fn request_timeout(mut self, timeout: Duration) -> Self {
        self.request_timeout = Some(timeout);
        self
    }

    /// Disables the SDK default API request timeout.
    pub fn without_request_timeout(mut self) -> Self {
        self.request_timeout = None;
        self
    }

    /// Builds the API client.
    pub fn build(self) -> Result<ApiClient, ApiError> {
        let base_url = normalize_api_base_url(self.base_url)?;
        log::debug!(
            target: "inline_sdk::api",
            "building API client base_url={base_url} identity_type={} request_timeout={:?} custom_http_client={}",
            self.identity.client_type(),
            self.request_timeout,
            self.http.is_some()
        );
        let (http, request_timeout) = match self.http {
            Some(http) => (http, None),
            None => {
                let mut builder = client_info::http_client_builder_for(&self.identity);
                if let Some(timeout) = self.request_timeout {
                    builder = builder.timeout(timeout);
                }
                (builder.build()?, self.request_timeout)
            }
        };
        Ok(ApiClient {
            base_url,
            http,
            request_timeout,
        })
    }
}

impl ApiClient {
    /// Creates an API client from a caller-provided `reqwest` client.
    pub fn try_with_http_client(
        base_url: impl Into<String>,
        http: Client,
    ) -> Result<Self, ApiError> {
        Ok(Self {
            base_url: normalize_api_base_url(base_url)?,
            http,
            request_timeout: None,
        })
    }

    /// Sends an email login code.
    pub async fn send_email_code(
        &self,
        email: &str,
        metadata: &AuthMetadata,
    ) -> Result<SendCodeResult, ApiError> {
        validate_required_str("email", email)?;
        validate_auth_metadata(metadata)?;
        let url = format!("{}/sendEmailCode", self.base_url);
        let mut payload = serde_json::Map::new();
        payload.insert("email".to_string(), json!(email));
        add_auth_metadata(&mut payload, metadata);
        self.post(url, payload).await
    }

    /// Verifies an email login code and returns an auth token on success.
    pub async fn verify_email_code(
        &self,
        email: &str,
        code: &str,
        challenge_token: Option<&str>,
        metadata: &AuthMetadata,
    ) -> Result<VerifyCodeResult, ApiError> {
        validate_required_str("email", email)?;
        validate_required_str("verification code", code)?;
        validate_auth_metadata(metadata)?;
        let url = format!("{}/verifyEmailCode", self.base_url);
        let mut payload = serde_json::Map::new();
        payload.insert("email".to_string(), json!(email));
        payload.insert("code".to_string(), json!(code));
        if let Some(challenge_token) =
            challenge_token.filter(|challenge_token| !challenge_token.trim().is_empty())
        {
            payload.insert("challengeToken".to_string(), json!(challenge_token));
        }
        add_auth_metadata(&mut payload, metadata);
        self.post(url, payload).await
    }

    /// Sends an SMS login code.
    pub async fn send_sms_code(
        &self,
        phone_number: &str,
        metadata: &AuthMetadata,
    ) -> Result<SendCodeResult, ApiError> {
        validate_required_str("phone number", phone_number)?;
        validate_auth_metadata(metadata)?;
        let url = format!("{}/sendSmsCode", self.base_url);
        let mut payload = serde_json::Map::new();
        payload.insert("phoneNumber".to_string(), json!(phone_number));
        add_auth_metadata(&mut payload, metadata);
        self.post(url, payload).await
    }

    /// Verifies an SMS login code and returns an auth token on success.
    pub async fn verify_sms_code(
        &self,
        phone_number: &str,
        code: &str,
        metadata: &AuthMetadata,
    ) -> Result<VerifyCodeResult, ApiError> {
        validate_required_str("phone number", phone_number)?;
        validate_required_str("verification code", code)?;
        validate_auth_metadata(metadata)?;
        let url = format!("{}/verifySmsCode", self.base_url);
        let mut payload = serde_json::Map::new();
        payload.insert("phoneNumber".to_string(), json!(phone_number));
        payload.insert("code".to_string(), json!(code));
        add_auth_metadata(&mut payload, metadata);
        self.post(url, payload).await
    }

    /// Uploads a local file using the Inline upload endpoint.
    pub async fn upload_file(
        &self,
        token: &str,
        input: UploadFileInput,
    ) -> Result<UploadFileResult, ApiError> {
        validate_bearer_token(token)?;
        validate_upload_file_input(&input)?;
        let url = format!("{}/uploadFile", self.base_url);
        log::debug!(
            target: "inline_sdk::api",
            "uploading file type={} has_mime_type={} has_video_metadata={}",
            input.file_type,
            input.mime_type.is_some(),
            input.video_metadata.is_some()
        );
        let mut form = reqwest::multipart::Form::new().text("type", input.file_type.as_str());
        let bytes = fs::read(&input.path)?;
        let mut file_part = reqwest::multipart::Part::bytes(bytes);
        file_part = file_part.file_name(input.file_name);
        if let Some(mime) = input.mime_type {
            file_part = file_part.mime_str(&mime)?;
        }
        form = form.part("file", file_part);

        if let Some(video) = input.video_metadata {
            form = form
                .text("width", video.width.to_string())
                .text("height", video.height.to_string())
                .text("duration", video.duration.to_string());
        }

        let response = self
            .http
            .post(url)
            .bearer_auth(token)
            .multipart(form)
            .send()
            .await?;
        log::trace!(
            target: "inline_sdk::api",
            "uploadFile response status={}",
            response.status()
        );
        decode_api_response(response).await
    }

    /// Marks messages as read for a peer.
    pub async fn read_messages(
        &self,
        token: &str,
        input: ReadMessagesInput,
    ) -> Result<ReadMessagesResult, ApiError> {
        validate_bearer_token(token)?;
        validate_peer_id(input.peer)?;
        if let Some(max_id) = input.max_id {
            validate_positive_id("max_id", max_id)?;
        }
        let url = format!("{}/readMessages", self.base_url);
        let mut payload = serde_json::Map::new();
        add_peer_selector_fields(&mut payload, input.peer);
        if let Some(max_id) = input.max_id {
            payload.insert("maxId".to_string(), json!(max_id));
        }
        self.post_with_token(url, token, payload).await
    }

    /// Creates or opens a private chat with a user.
    pub async fn create_private_chat(
        &self,
        token: &str,
        user_id: i64,
    ) -> Result<CreatePrivateChatResult, ApiError> {
        validate_bearer_token(token)?;
        validate_positive_id("user_id", user_id)?;
        let url = format!("{}/createPrivateChat", self.base_url);
        let mut payload = serde_json::Map::new();
        payload.insert("userId".to_string(), json!(user_id));
        self.post_with_token(url, token, payload).await
    }

    /// Creates a Linear issue from an Inline message.
    pub async fn create_linear_issue(
        &self,
        token: &str,
        input: CreateLinearIssueInput,
    ) -> Result<CreateLinearIssueResult, ApiError> {
        validate_bearer_token(token)?;
        validate_create_linear_issue_input(&input)?;
        let url = format!("{}/createLinearIssue", self.base_url);
        let mut payload = serde_json::Map::new();
        payload.insert("text".to_string(), json!(input.text));
        payload.insert("messageId".to_string(), json!(input.message_id));
        payload.insert("chatId".to_string(), json!(input.chat_id));
        payload.insert("fromId".to_string(), json!(input.from_id));

        payload.insert("peerId".to_string(), json!(peer_id_object(input.peer)));

        if let Some(space_id) = input.space_id {
            payload.insert("spaceId".to_string(), json!(space_id));
        }

        self.post_with_token(url, token, payload).await
    }

    /// Creates a Notion task from an Inline message.
    pub async fn create_notion_task(
        &self,
        token: &str,
        input: CreateNotionTaskInput,
    ) -> Result<CreateNotionTaskResult, ApiError> {
        validate_bearer_token(token)?;
        validate_create_notion_task_input(&input)?;
        let url = format!("{}/createNotionTask", self.base_url);
        let mut payload = serde_json::Map::new();
        payload.insert("spaceId".to_string(), json!(input.space_id));
        payload.insert("messageId".to_string(), json!(input.message_id));
        payload.insert("chatId".to_string(), json!(input.chat_id));

        payload.insert("peerId".to_string(), json!(peer_id_object(input.peer)));

        self.post_with_token(url, token, payload).await
    }

    async fn post<T: for<'de> Deserialize<'de>>(
        &self,
        url: String,
        payload: serde_json::Map<String, serde_json::Value>,
    ) -> Result<T, ApiError> {
        log::trace!(
            target: "inline_sdk::api",
            "POST {}",
            api_url_path_for_log(&url)
        );
        let response = self.http.post(url).json(&payload).send().await?;
        log::trace!(
            target: "inline_sdk::api",
            "API response status={}",
            response.status()
        );
        decode_api_response(response).await
    }

    async fn post_with_token<T: for<'de> Deserialize<'de>>(
        &self,
        url: String,
        token: &str,
        payload: serde_json::Map<String, serde_json::Value>,
    ) -> Result<T, ApiError> {
        log::trace!(
            target: "inline_sdk::api",
            "POST {} with bearer auth",
            api_url_path_for_log(&url)
        );
        let response = self
            .http
            .post(url)
            .bearer_auth(token)
            .json(&payload)
            .send()
            .await?;
        log::trace!(
            target: "inline_sdk::api",
            "API response status={}",
            response.status()
        );
        decode_api_response(response).await
    }
}

/// Response from sending an auth code.
#[derive(Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct SendCodeResult {
    /// Whether the contact belongs to an existing user.
    pub existing_user: bool,
    /// Whether the login flow requires an invite code.
    pub needs_invite_code: bool,
    /// Challenge token required by some email verification flows.
    pub challenge_token: Option<String>,
}

impl fmt::Debug for SendCodeResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SendCodeResult")
            .field("existing_user", &self.existing_user)
            .field("needs_invite_code", &self.needs_invite_code)
            .field(
                "challenge_token",
                &self.challenge_token.as_ref().map(|_| "<redacted>"),
            )
            .finish()
    }
}

/// Successful auth verification response.
#[derive(Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct VerifyCodeResult {
    /// Inline user id.
    pub user_id: i64,
    /// Bearer token for authenticated API and realtime calls.
    pub token: String,
}

impl fmt::Debug for VerifyCodeResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("VerifyCodeResult")
            .field("user_id", &self.user_id)
            .field("token", &"<redacted>")
            .finish()
    }
}

/// Upload type accepted by the Inline upload endpoint.
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[serde(rename_all = "lowercase")]
pub enum UploadFileType {
    /// Photo/image upload.
    Photo,
    /// Video upload.
    Video,
    /// Generic document upload.
    Document,
}

impl UploadFileType {
    /// Returns the wire-format upload type string.
    pub fn as_str(&self) -> &'static str {
        match self {
            UploadFileType::Photo => "photo",
            UploadFileType::Video => "video",
            UploadFileType::Document => "document",
        }
    }
}

impl fmt::Display for UploadFileType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for UploadFileType {
    type Err = UploadFileTypeParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.trim().to_ascii_lowercase().as_str() {
            "photo" => Ok(UploadFileType::Photo),
            "video" => Ok(UploadFileType::Video),
            "document" => Ok(UploadFileType::Document),
            _ => Err(UploadFileTypeParseError {
                value: value.to_string(),
            }),
        }
    }
}

/// Error returned when parsing an [`UploadFileType`] from a string.
#[derive(Debug, Clone, Error, PartialEq, Eq)]
#[error("unknown upload file type `{value}`")]
pub struct UploadFileTypeParseError {
    /// Original value that could not be parsed.
    pub value: String,
}

/// Video metadata sent with video uploads.
#[must_use]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UploadVideoMetadata {
    /// Video width in pixels.
    pub width: i32,
    /// Video height in pixels.
    pub height: i32,
    /// Video duration in seconds.
    pub duration: i32,
}

impl UploadVideoMetadata {
    /// Creates video metadata for a video upload.
    pub fn new(width: i32, height: i32, duration: i32) -> Self {
        Self {
            width,
            height,
            duration,
        }
    }
}

/// Local file upload input.
#[must_use]
#[derive(Clone, PartialEq, Eq)]
pub struct UploadFileInput {
    /// Local path to read and upload.
    pub path: PathBuf,
    /// File name reported to the server.
    pub file_name: String,
    /// Optional MIME type override.
    pub mime_type: Option<String>,
    /// Inline upload category.
    pub file_type: UploadFileType,
    /// Required video details when uploading a video.
    pub video_metadata: Option<UploadVideoMetadata>,
}

impl fmt::Debug for UploadFileInput {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UploadFileInput")
            .field("path", &"<redacted>")
            .field("file_name", &self.file_name)
            .field("mime_type", &self.mime_type)
            .field("file_type", &self.file_type)
            .field("video_metadata", &self.video_metadata)
            .finish()
    }
}

impl UploadFileInput {
    /// Creates an upload input with an explicit upload type.
    pub fn new(
        path: impl Into<PathBuf>,
        file_name: impl Into<String>,
        file_type: UploadFileType,
    ) -> Self {
        Self {
            path: path.into(),
            file_name: file_name.into(),
            mime_type: None,
            file_type,
            video_metadata: None,
        }
    }

    /// Creates a photo upload input.
    pub fn photo(path: impl Into<PathBuf>, file_name: impl Into<String>) -> Self {
        Self::new(path, file_name, UploadFileType::Photo)
    }

    /// Creates a video upload input with video metadata.
    pub fn video(
        path: impl Into<PathBuf>,
        file_name: impl Into<String>,
        metadata: UploadVideoMetadata,
    ) -> Self {
        Self::new(path, file_name, UploadFileType::Video).with_video_metadata(metadata)
    }

    /// Creates a generic document upload input.
    pub fn document(path: impl Into<PathBuf>, file_name: impl Into<String>) -> Self {
        Self::new(path, file_name, UploadFileType::Document)
    }

    /// Sets a MIME type override.
    pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
        let mime_type = mime_type.into().trim().to_string();
        if !mime_type.is_empty() {
            self.mime_type = Some(mime_type);
        }
        self
    }

    /// Sets video metadata for a video upload.
    pub fn with_video_metadata(mut self, metadata: UploadVideoMetadata) -> Self {
        self.video_metadata = Some(metadata);
        self
    }
}

/// File upload response.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct UploadFileResult {
    /// Stable unique file id.
    pub file_unique_id: String,
    /// Uploaded photo id when the file was a photo.
    pub photo_id: Option<i64>,
    /// Uploaded video id when the file was a video.
    pub video_id: Option<i64>,
    /// Uploaded document id when the file was a document.
    pub document_id: Option<i64>,
}

/// Input for marking messages read.
#[must_use]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReadMessagesInput {
    /// Peer to mark read.
    pub peer: PeerId,
    /// Highest message id to mark as read.
    pub max_id: Option<i64>,
}

impl ReadMessagesInput {
    /// Creates a read-marker request for a peer.
    pub fn new(peer: PeerId) -> Self {
        Self { peer, max_id: None }
    }

    /// Sets the highest message id to mark as read.
    pub fn with_max_id(mut self, max_id: i64) -> Self {
        self.max_id = Some(max_id);
        self
    }
}

/// Empty response for marking messages read.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ReadMessagesResult {}

/// Peer identifier used by HTTP API helpers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PeerId {
    /// Direct-message peer by user id.
    User(i64),
    /// Chat/thread peer by thread id.
    Thread(i64),
}

impl PeerId {
    /// Creates a direct-message peer id.
    pub fn user(user_id: i64) -> Self {
        Self::User(user_id)
    }

    /// Creates a chat/thread peer id.
    pub fn thread(thread_id: i64) -> Self {
        Self::Thread(thread_id)
    }

    /// Returns the user id when this is a user peer.
    pub fn user_id(self) -> Option<i64> {
        match self {
            Self::User(user_id) => Some(user_id),
            Self::Thread(_) => None,
        }
    }

    /// Returns the thread id when this is a thread peer.
    pub fn thread_id(self) -> Option<i64> {
        match self {
            Self::User(_) => None,
            Self::Thread(thread_id) => Some(thread_id),
        }
    }
}

/// Response from creating or opening a private chat.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct CreatePrivateChatResult {
    /// Raw chat object returned by the server.
    pub chat: Value,
    /// Raw dialog object returned by the server.
    pub dialog: Value,
    /// Raw user object returned by the server.
    pub user: Value,
}

/// Input for creating a Linear issue from a message.
#[must_use]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateLinearIssueInput {
    /// Source message text.
    pub text: String,
    /// Source message id.
    pub message_id: i64,
    /// Source chat id.
    pub chat_id: i64,
    /// Source sender user id.
    pub from_id: i64,
    /// Source peer.
    pub peer: PeerId,
    /// Optional source space id.
    pub space_id: Option<i64>,
}

impl CreateLinearIssueInput {
    /// Creates input for creating a Linear issue from a message.
    pub fn new(
        text: impl Into<String>,
        message_id: i64,
        chat_id: i64,
        from_id: i64,
        peer: PeerId,
    ) -> Self {
        Self {
            text: text.into(),
            message_id,
            chat_id,
            from_id,
            peer,
            space_id: None,
        }
    }

    /// Sets the optional source space id.
    pub fn with_space_id(mut self, space_id: i64) -> Self {
        self.space_id = Some(space_id);
        self
    }
}

/// Response from creating a Linear issue.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct CreateLinearIssueResult {
    /// Created Linear issue URL, if the integration returned one.
    pub link: Option<String>,
}

/// Input for creating a Notion task from a message.
#[must_use]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateNotionTaskInput {
    /// Source space id.
    pub space_id: i64,
    /// Source message id.
    pub message_id: i64,
    /// Source chat id.
    pub chat_id: i64,
    /// Source peer.
    pub peer: PeerId,
}

impl CreateNotionTaskInput {
    /// Creates input for creating a Notion task from a message.
    pub fn new(space_id: i64, message_id: i64, chat_id: i64, peer: PeerId) -> Self {
        Self {
            space_id,
            message_id,
            chat_id,
            peer,
        }
    }
}

/// Response from creating a Notion task.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct CreateNotionTaskResult {
    /// Created Notion task URL.
    pub url: String,
    /// Created task title, when returned by the integration.
    pub task_title: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged, rename_all = "camelCase")]
enum ApiResponse<T> {
    Ok {
        #[serde(rename = "ok")]
        _ok: bool,
        result: T,
    },
    Err {
        #[serde(rename = "ok")]
        _ok: bool,
        error: String,
        #[serde(rename = "errorCode", alias = "error_code")]
        _error_code: Option<i32>,
        description: Option<String>,
    },
}

fn normalize_api_base_url(base_url: impl Into<String>) -> Result<String, ApiError> {
    let original = base_url.into();
    let normalized = original.trim().trim_end_matches('/').to_string();
    if normalized.is_empty() {
        return Err(ApiError::InvalidBaseUrl {
            url: original,
            message: "base URL cannot be empty".to_string(),
        });
    }

    let parsed = Url::parse(&normalized).map_err(|err| ApiError::InvalidBaseUrl {
        url: normalized.clone(),
        message: err.to_string(),
    })?;

    if !matches!(parsed.scheme(), "http" | "https") {
        return Err(ApiError::InvalidBaseUrl {
            url: normalized,
            message: "scheme must be http or https".to_string(),
        });
    }

    if parsed.host_str().is_none() {
        return Err(ApiError::InvalidBaseUrl {
            url: normalized,
            message: "host is required".to_string(),
        });
    }

    if !parsed.username().is_empty() || parsed.password().is_some() {
        return Err(ApiError::InvalidBaseUrl {
            url: normalized,
            message: "credentials are not valid in the API base URL".to_string(),
        });
    }

    if parsed.query().is_some() || parsed.fragment().is_some() {
        return Err(ApiError::InvalidBaseUrl {
            url: normalized,
            message: "query strings and fragments are not valid in the API base URL".to_string(),
        });
    }

    Ok(normalized)
}

fn api_url_path_for_log(url: &str) -> String {
    Url::parse(url)
        .map(|url| url.path().to_string())
        .unwrap_or_else(|_| "<invalid-url>".to_string())
}

fn api_base_url_for_debug(raw_url: &str) -> String {
    Url::parse(raw_url.trim())
        .map(|url| {
            let host = url.host_str().unwrap_or("<missing-host>");
            let port = url
                .port()
                .map(|port| format!(":{port}"))
                .unwrap_or_default();
            let path = url.path().trim_end_matches('/');
            format!("{}://{}{}{}", url.scheme(), host, port, path)
        })
        .unwrap_or_else(|_| "<invalid>".to_string())
}

async fn decode_api_response<T: for<'de> Deserialize<'de>>(
    response: reqwest::Response,
) -> Result<T, ApiError> {
    let status = response.status();
    let text = response.text().await?;
    decode_api_response_text(status, &text)
}

fn decode_api_response_text<T: for<'de> Deserialize<'de>>(
    status: StatusCode,
    text: &str,
) -> Result<T, ApiError> {
    let value: Value = serde_json::from_str(text).map_err(|err| {
        if status.is_success() {
            ApiError::Json(err)
        } else {
            ApiError::Status {
                status: status.as_u16(),
                message: status
                    .canonical_reason()
                    .unwrap_or("HTTP error")
                    .to_string(),
                body: body_preview(text),
            }
        }
    })?;

    if !status.is_success() {
        return Err(
            api_error_from_value(status, &value).unwrap_or_else(|| ApiError::Status {
                status: status.as_u16(),
                message: status
                    .canonical_reason()
                    .unwrap_or("HTTP error")
                    .to_string(),
                body: body_preview(text),
            }),
        );
    }

    if value.get("ok").and_then(Value::as_bool) == Some(false) {
        return Err(
            api_error_from_value(status, &value).unwrap_or_else(|| ApiError::Api {
                status: None,
                error: "API_ERROR".to_string(),
                error_code: None,
                description: "The server returned ok=false without an error description."
                    .to_string(),
            }),
        );
    }

    let api_response: ApiResponse<T> = serde_json::from_value(value)?;
    match api_response {
        ApiResponse::Ok { result, .. } => Ok(result),
        ApiResponse::Err {
            error,
            _error_code,
            description,
            ..
        } => Err(ApiError::Api {
            status: None,
            error,
            error_code: _error_code,
            description: description.unwrap_or_else(|| "Unknown error".to_string()),
        }),
    }
}

fn api_error_from_value(status: StatusCode, value: &Value) -> Option<ApiError> {
    let object = value.as_object()?;
    let error = string_field(value, "error")
        .or_else(|| string_field(value, "code"))
        .or_else(|| string_field(value, "name"))
        .unwrap_or_else(|| {
            if value.get("ok").and_then(Value::as_bool) == Some(false) && status.is_success() {
                return "API_ERROR".to_string();
            }
            status
                .canonical_reason()
                .unwrap_or("HTTP error")
                .to_string()
        });
    let description = string_field(value, "description")
        .or_else(|| string_field(value, "message"))
        .or_else(|| string_field(value, "detail"))
        .unwrap_or_else(|| "No server error description was provided.".to_string());
    let error_code = object
        .get("error_code")
        .or_else(|| object.get("errorCode"))
        .and_then(|value| value.as_i64())
        .and_then(|value| i32::try_from(value).ok());

    Some(ApiError::Api {
        status: Some(status.as_u16()),
        error,
        error_code,
        description,
    })
}

fn string_field(value: &Value, key: &str) -> Option<String> {
    value
        .get(key)
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(ToString::to_string)
}

fn body_preview(text: &str) -> Option<String> {
    let normalized = text.split_whitespace().collect::<Vec<_>>().join(" ");
    if normalized.is_empty() {
        return None;
    }
    const MAX_BODY_PREVIEW_BYTES: usize = 500;
    if normalized.len() <= MAX_BODY_PREVIEW_BYTES {
        return Some(normalized);
    }

    let mut end = MAX_BODY_PREVIEW_BYTES;
    while !normalized.is_char_boundary(end) {
        end -= 1;
    }
    Some(format!("{}...", &normalized[..end]))
}

fn validate_required_str(field: &'static str, value: &str) -> Result<(), ApiError> {
    if value.trim().is_empty() {
        return Err(ApiError::InvalidInput {
            message: format!("{field} cannot be empty"),
        });
    }
    Ok(())
}

fn validate_bearer_token(token: &str) -> Result<(), ApiError> {
    validate_required_str("bearer token", token)
}

fn validate_auth_metadata(metadata: &AuthMetadata) -> Result<(), ApiError> {
    validate_required_str("device id", metadata.device_id())
}

fn validate_upload_file_input(input: &UploadFileInput) -> Result<(), ApiError> {
    if input.file_name.trim().is_empty() {
        return Err(ApiError::InvalidInput {
            message: "upload file name cannot be empty".to_string(),
        });
    }

    match (input.file_type, input.video_metadata) {
        (UploadFileType::Video, Some(metadata)) => validate_upload_video_metadata(metadata),
        (UploadFileType::Video, None) => Err(ApiError::InvalidInput {
            message: "video uploads require video metadata".to_string(),
        }),
        (_, Some(_)) => Err(ApiError::InvalidInput {
            message: "video metadata can only be used with video uploads".to_string(),
        }),
        (_, None) => Ok(()),
    }
}

fn validate_upload_video_metadata(metadata: UploadVideoMetadata) -> Result<(), ApiError> {
    if metadata.width <= 0 || metadata.height <= 0 || metadata.duration <= 0 {
        return Err(ApiError::InvalidInput {
            message: "video metadata width, height, and duration must be positive".to_string(),
        });
    }
    Ok(())
}

fn validate_positive_id(field: &'static str, value: i64) -> Result<(), ApiError> {
    if value <= 0 {
        return Err(ApiError::InvalidInput {
            message: format!("{field} must be positive"),
        });
    }
    Ok(())
}

fn validate_peer_id(peer: PeerId) -> Result<(), ApiError> {
    match peer {
        PeerId::User(user_id) => validate_positive_id("peer user id", user_id),
        PeerId::Thread(thread_id) => validate_positive_id("peer thread id", thread_id),
    }
}

fn validate_create_linear_issue_input(input: &CreateLinearIssueInput) -> Result<(), ApiError> {
    validate_required_str("Linear issue text", &input.text)?;
    validate_positive_id("message id", input.message_id)?;
    validate_positive_id("chat id", input.chat_id)?;
    validate_positive_id("sender user id", input.from_id)?;
    validate_peer_id(input.peer)?;
    if let Some(space_id) = input.space_id {
        validate_positive_id("space id", space_id)?;
    }
    Ok(())
}

fn validate_create_notion_task_input(input: &CreateNotionTaskInput) -> Result<(), ApiError> {
    validate_positive_id("space id", input.space_id)?;
    validate_positive_id("message id", input.message_id)?;
    validate_positive_id("chat id", input.chat_id)?;
    validate_peer_id(input.peer)
}

fn add_auth_metadata(
    payload: &mut serde_json::Map<String, serde_json::Value>,
    metadata: &AuthMetadata,
) {
    payload.insert("deviceId".to_string(), json!(metadata.device_id()));
    payload.insert(
        "clientType".to_string(),
        json!(metadata.client().client_type()),
    );
    payload.insert(
        "clientVersion".to_string(),
        json!(metadata.client().client_version()),
    );
    if let Some(device_name) = metadata.device_name() {
        payload.insert("deviceName".to_string(), json!(device_name));
    }
}

fn add_peer_selector_fields(
    payload: &mut serde_json::Map<String, serde_json::Value>,
    peer: PeerId,
) {
    match peer {
        PeerId::User(user_id) => {
            payload.insert("peerUserId".to_string(), json!(user_id));
        }
        PeerId::Thread(thread_id) => {
            payload.insert("peerThreadId".to_string(), json!(thread_id));
        }
    }
}

fn peer_id_object(peer: PeerId) -> serde_json::Map<String, serde_json::Value> {
    let mut peer_id = serde_json::Map::new();
    match peer {
        PeerId::User(user_id) => {
            peer_id.insert("userId".to_string(), json!(user_id));
        }
        PeerId::Thread(thread_id) => {
            peer_id.insert("threadId".to_string(), json!(thread_id));
        }
    }
    peer_id
}

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

    #[derive(Debug, Deserialize, PartialEq)]
    #[serde(rename_all = "camelCase")]
    struct TestResult {
        value: String,
    }

    #[test]
    fn api_client_builder_normalizes_base_url() {
        let client = ApiClient::try_new(" https://api.inline.chat/v1/ ").unwrap();
        assert_eq!(client.base_url(), "https://api.inline.chat/v1");
    }

    #[test]
    fn api_client_builder_uses_default_request_timeout() {
        let client = ApiClient::try_new("https://api.inline.chat/v1").unwrap();
        assert_eq!(client.request_timeout(), Some(DEFAULT_API_TIMEOUT));
    }

    #[test]
    fn api_client_builder_can_override_or_disable_request_timeout() {
        let client = ApiClient::builder("https://api.inline.chat/v1")
            .request_timeout(Duration::from_secs(5))
            .build()
            .unwrap();
        assert_eq!(client.request_timeout(), Some(Duration::from_secs(5)));

        let client = ApiClient::builder("https://api.inline.chat/v1")
            .without_request_timeout()
            .build()
            .unwrap();
        assert_eq!(client.request_timeout(), None);
    }

    #[test]
    fn custom_http_clients_own_their_timeout_policy() {
        let http = reqwest::Client::builder()
            .timeout(Duration::from_secs(10))
            .build()
            .unwrap();
        let client = ApiClient::try_with_http_client("https://api.inline.chat/v1", http).unwrap();
        assert_eq!(client.request_timeout(), None);

        let http = reqwest::Client::builder()
            .timeout(Duration::from_secs(10))
            .build()
            .unwrap();
        let client = ApiClient::builder("https://api.inline.chat/v1")
            .request_timeout(Duration::from_secs(5))
            .http_client(http)
            .build()
            .unwrap();
        assert_eq!(client.request_timeout(), None);
    }

    #[test]
    fn api_client_builder_rejects_invalid_base_url() {
        let err = match ApiClient::try_new("inline.test") {
            Ok(_) => panic!("expected invalid base URL"),
            Err(err) => err,
        };
        match err {
            ApiError::InvalidBaseUrl { url, message } => {
                assert_eq!(url, "inline.test");
                assert!(message.contains("relative URL without a base"));
            }
            other => panic!("expected invalid base URL, got {other:?}"),
        }

        let err = match ApiClient::try_new("wss://api.inline.chat/v1") {
            Ok(_) => panic!("expected invalid base URL"),
            Err(err) => err,
        };
        match err {
            ApiError::InvalidBaseUrl { message, .. } => {
                assert_eq!(message, "scheme must be http or https");
            }
            other => panic!("expected invalid base URL, got {other:?}"),
        }

        let err = match ApiClient::try_new("https://user:secret@api.inline.chat/v1") {
            Ok(_) => panic!("expected invalid base URL"),
            Err(err) => err,
        };
        match &err {
            ApiError::InvalidBaseUrl { message, .. } => {
                assert_eq!(message, "credentials are not valid in the API base URL");
                assert!(!err.to_string().contains("secret"));
            }
            other => panic!("expected invalid base URL, got {other:?}"),
        }

        let err = match ApiClient::try_new("https://api.inline.chat/v1?debug=true") {
            Ok(_) => panic!("expected invalid base URL"),
            Err(err) => err,
        };
        match err {
            ApiError::InvalidBaseUrl { message, .. } => {
                assert_eq!(
                    message,
                    "query strings and fragments are not valid in the API base URL"
                );
            }
            other => panic!("expected invalid base URL, got {other:?}"),
        }
    }

    #[test]
    fn api_debug_output_redacts_unsafe_url_parts() {
        let raw_url = "https://user:url-secret@api.inline.chat/v1?token=query-secret#frag";
        let builder = ApiClient::builder(raw_url);
        let builder_debug = format!("{builder:?}");

        assert!(builder_debug.contains("https://api.inline.chat/v1"));
        assert!(!builder_debug.contains("url-secret"));
        assert!(!builder_debug.contains("query-secret"));

        let err = ApiClient::try_new(raw_url).unwrap_err();
        let err_debug = format!("{err:?}");

        assert!(err_debug.contains("https://api.inline.chat/v1"));
        assert!(!err_debug.contains("url-secret"));
        assert!(!err_debug.contains("query-secret"));
    }

    #[test]
    fn api_url_path_for_log_omits_origin_query_and_fragment() {
        assert_eq!(
            api_url_path_for_log("https://api.inline.chat/v1/getMe?token=secret#frag"),
            "/v1/getMe"
        );
    }

    #[test]
    fn upload_file_type_parses_and_displays_wire_values() {
        assert_eq!(
            "photo".parse::<UploadFileType>().unwrap(),
            UploadFileType::Photo
        );
        assert_eq!(
            "VIDEO".parse::<UploadFileType>().unwrap(),
            UploadFileType::Video
        );
        assert_eq!(UploadFileType::Document.to_string(), "document");

        let err = "avatar".parse::<UploadFileType>().unwrap_err();
        assert_eq!(err.value, "avatar");
    }

    #[test]
    fn upload_file_type_serializes_as_wire_values() {
        assert_eq!(
            serde_json::to_string(&UploadFileType::Photo).unwrap(),
            r#""photo""#
        );
        assert_eq!(
            serde_json::from_str::<UploadFileType>(r#""video""#).unwrap(),
            UploadFileType::Video
        );
    }

    #[test]
    fn upload_input_constructors_set_expected_fields() {
        let metadata = UploadVideoMetadata::new(1920, 1080, 12);
        let video =
            UploadFileInput::video("clip.mp4", "clip.mp4", metadata).with_mime_type(" video/mp4 ");

        assert_eq!(video.path, PathBuf::from("clip.mp4"));
        assert_eq!(video.file_name, "clip.mp4");
        assert_eq!(video.file_type, UploadFileType::Video);
        assert_eq!(video.mime_type.as_deref(), Some("video/mp4"));
        assert_eq!(video.video_metadata, Some(metadata));

        let document = UploadFileInput::document("notes.txt", "notes.txt").with_mime_type(" ");
        assert_eq!(document.file_type, UploadFileType::Document);
        assert!(document.mime_type.is_none());
        assert!(document.video_metadata.is_none());
    }

    #[test]
    fn upload_input_debug_redacts_local_path() {
        let input = UploadFileInput::document("/Users/mo/private/report.pdf", "report.pdf")
            .with_mime_type("application/pdf");
        let debug = format!("{input:?}");

        assert!(debug.contains("report.pdf"));
        assert!(debug.contains("<redacted>"));
        assert!(!debug.contains("/Users/mo/private"));
    }

    #[test]
    fn upload_input_validation_rejects_invalid_video_metadata_shape() {
        let missing_metadata = UploadFileInput::new("clip.mp4", "clip.mp4", UploadFileType::Video);
        match validate_upload_file_input(&missing_metadata).unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(message, "video uploads require video metadata");
            }
            other => panic!("expected invalid input, got {other:?}"),
        }

        let document_with_video = UploadFileInput::document("notes.txt", "notes.txt")
            .with_video_metadata(UploadVideoMetadata::new(1, 1, 1));
        match validate_upload_file_input(&document_with_video).unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(
                    message,
                    "video metadata can only be used with video uploads"
                );
            }
            other => panic!("expected invalid input, got {other:?}"),
        }

        let bad_dimensions = UploadFileInput::video(
            "clip.mp4",
            "clip.mp4",
            UploadVideoMetadata::new(0, 1080, 12),
        );
        match validate_upload_file_input(&bad_dimensions).unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(
                    message,
                    "video metadata width, height, and duration must be positive"
                );
            }
            other => panic!("expected invalid input, got {other:?}"),
        }
    }

    #[test]
    fn upload_input_validation_rejects_empty_file_name() {
        let input = UploadFileInput::document("notes.txt", " ");
        match validate_upload_file_input(&input).unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(message, "upload file name cannot be empty");
            }
            other => panic!("expected invalid input, got {other:?}"),
        }
    }

    #[test]
    fn auth_and_token_validation_reject_blank_required_fields() {
        match validate_required_str("email", "  ").unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(message, "email cannot be empty");
            }
            other => panic!("expected invalid input, got {other:?}"),
        }

        match validate_bearer_token("").unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(message, "bearer token cannot be empty");
            }
            other => panic!("expected invalid input, got {other:?}"),
        }

        match validate_auth_metadata(&AuthMetadata::sdk(" ")).unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(message, "device id cannot be empty");
            }
            other => panic!("expected invalid input, got {other:?}"),
        }
    }

    #[test]
    fn auth_result_debug_redacts_tokens() {
        let send_code = SendCodeResult {
            existing_user: true,
            needs_invite_code: false,
            challenge_token: Some("challenge-secret".to_string()),
        };
        let verify_code = VerifyCodeResult {
            user_id: 42,
            token: "bearer-secret".to_string(),
        };

        let send_debug = format!("{send_code:?}");
        let verify_debug = format!("{verify_code:?}");

        assert!(send_debug.contains("<redacted>"));
        assert!(!send_debug.contains("challenge-secret"));
        assert!(verify_debug.contains("<redacted>"));
        assert!(!verify_debug.contains("bearer-secret"));
    }

    #[test]
    fn api_input_validation_rejects_non_positive_ids() {
        match validate_peer_id(PeerId::thread(0)).unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(message, "peer thread id must be positive");
            }
            other => panic!("expected invalid input, got {other:?}"),
        }

        let linear = CreateLinearIssueInput::new("ship it", 0, 20, 30, PeerId::thread(20));
        match validate_create_linear_issue_input(&linear).unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(message, "message id must be positive");
            }
            other => panic!("expected invalid input, got {other:?}"),
        }

        let notion = CreateNotionTaskInput::new(1, 2, 0, PeerId::thread(3));
        match validate_create_notion_task_input(&notion).unwrap_err() {
            ApiError::InvalidInput { message } => {
                assert_eq!(message, "chat id must be positive");
            }
            other => panic!("expected invalid input, got {other:?}"),
        }
    }

    #[test]
    fn peer_id_helpers_encode_user_and_thread_peers() {
        assert_eq!(PeerId::user(42).user_id(), Some(42));
        assert_eq!(PeerId::user(42).thread_id(), None);
        assert_eq!(PeerId::thread(99).thread_id(), Some(99));
        assert_eq!(PeerId::thread(99).user_id(), None);

        let mut payload = serde_json::Map::new();
        add_peer_selector_fields(&mut payload, PeerId::user(42));
        assert_eq!(payload.get("peerUserId"), Some(&json!(42)));
        assert!(payload.get("peerThreadId").is_none());

        let peer_id = peer_id_object(PeerId::thread(99));
        assert_eq!(peer_id.get("threadId"), Some(&json!(99)));
        assert!(peer_id.get("userId").is_none());
    }

    #[test]
    fn api_input_constructors_keep_required_fields_explicit() {
        let read = ReadMessagesInput::new(PeerId::user(42)).with_max_id(99);
        assert_eq!(read.peer, PeerId::user(42));
        assert_eq!(read.max_id, Some(99));

        let linear = CreateLinearIssueInput::new("ship it", 10, 20, 30, PeerId::thread(20))
            .with_space_id(40);
        assert_eq!(linear.text, "ship it");
        assert_eq!(linear.message_id, 10);
        assert_eq!(linear.chat_id, 20);
        assert_eq!(linear.from_id, 30);
        assert_eq!(linear.peer, PeerId::thread(20));
        assert_eq!(linear.space_id, Some(40));

        let notion = CreateNotionTaskInput::new(1, 2, 3, PeerId::thread(3));
        assert_eq!(notion.space_id, 1);
        assert_eq!(notion.message_id, 2);
        assert_eq!(notion.chat_id, 3);
        assert_eq!(notion.peer, PeerId::thread(3));
    }

    #[test]
    fn decodes_success_envelope() {
        let result: TestResult =
            decode_api_response_text(StatusCode::OK, r#"{"ok":true,"result":{"value":"done"}}"#)
                .unwrap();

        assert_eq!(
            result,
            TestResult {
                value: "done".to_string()
            }
        );
    }

    #[test]
    fn preserves_json_api_error_fields() {
        let err = decode_api_response_text::<TestResult>(
            StatusCode::BAD_REQUEST,
            r#"{"ok":false,"error":"INVALID_CODE","error_code":123,"description":"Code is invalid"}"#,
        )
        .unwrap_err();

        match err {
            ApiError::Api {
                status,
                error,
                error_code,
                description,
            } => {
                assert_eq!(status, Some(400));
                assert_eq!(error, "INVALID_CODE");
                assert_eq!(error_code, Some(123));
                assert_eq!(description, "Code is invalid");
            }
            other => panic!("expected api error, got {other:?}"),
        }
    }

    #[test]
    fn preserves_nonstandard_ok_false_message() {
        let err = decode_api_response_text::<TestResult>(
            StatusCode::OK,
            r#"{"ok":false,"message":"Not enough permissions"}"#,
        )
        .unwrap_err();

        match err {
            ApiError::Api {
                status,
                error,
                error_code,
                description,
            } => {
                assert_eq!(status, Some(200));
                assert_eq!(error, "API_ERROR");
                assert_eq!(error_code, None);
                assert_eq!(description, "Not enough permissions");
            }
            other => panic!("expected api error, got {other:?}"),
        }
    }

    #[test]
    fn preserves_non_json_http_body_preview() {
        let err = decode_api_response_text::<TestResult>(
            StatusCode::INTERNAL_SERVER_ERROR,
            "upstream failed\nwith details",
        )
        .unwrap_err();

        match err {
            ApiError::Status {
                status,
                message,
                body,
            } => {
                assert_eq!(status, 500);
                assert_eq!(message, "Internal Server Error");
                assert_eq!(body.as_deref(), Some("upstream failed with details"));
            }
            other => panic!("expected status error, got {other:?}"),
        }
    }

    #[test]
    fn auth_metadata_includes_client_identity() {
        let mut payload = serde_json::Map::new();
        let identity = ClientIdentity::new("cli", "1.2.3");
        add_auth_metadata(
            &mut payload,
            &AuthMetadata::new("device-1", identity).with_device_name("mo-mac"),
        );

        assert_eq!(payload.get("deviceId"), Some(&json!("device-1")));
        assert_eq!(payload.get("clientType"), Some(&json!("cli")));
        assert_eq!(payload.get("clientVersion"), Some(&json!("1.2.3")));
        assert_eq!(payload.get("deviceName"), Some(&json!("mo-mac")));
    }

    #[test]
    fn auth_metadata_accepts_non_cli_client_identity() {
        let mut payload = serde_json::Map::new();
        add_auth_metadata(
            &mut payload,
            &AuthMetadata::new("device-1", ClientIdentity::new("my-agent", "0.1.0")),
        );

        assert_eq!(payload.get("deviceId"), Some(&json!("device-1")));
        assert_eq!(payload.get("clientType"), Some(&json!("my-agent")));
        assert_eq!(payload.get("clientVersion"), Some(&json!("0.1.0")));
        assert!(payload.get("deviceName").is_none());
    }
}