atim-im 0.1.0

AI Agent Through IM
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
//! Feishu (Lark) IM adapter.
//!
//! Architecture:
//!   - WebSocket-based (Feishu pushes events via WSS persistent connection)
//!   - openlark crate for WS connection + token management
//!   - Interactive cards for inline keyboard UI
//!   - String IDs (open_id, chat_id) hashed to i64 for Atim's core types

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use tokio::sync::RwLock;
use tokio::sync::mpsc;

use atim_core::error::{Error, Result};
use atim_core::im::ImAdapter;
use atim_core::message::{
    Button, ChatId, ImEvent, ImEventKind, MessageId, MessageTarget, ThreadId, UserId,
};

use open_lark::Config;
use open_lark::ws_client::{EventDispatcherHandler, LarkWsClient};

const FEISHU_BASE_URL: &str = "https://open.feishu.cn";

// ── FeishuAdapter ──

pub struct FeishuAdapter {
    app_id: String,
    app_secret: String,
    client: reqwest::Client,
    token_cache: Arc<RwLock<TokenCache>>,
    /// Stable hash → original Feishu string ID (for outbound API calls).
    id_map: Arc<RwLock<IdMap>>,
    /// Persistence file for id_map (survives restarts).
    id_map_file: PathBuf,
    event_tx: Arc<RwLock<Option<mpsc::UnboundedSender<ImEvent>>>>,
    /// Bot's own open_id, used to detect @-mentions in group chats.
    bot_open_id: Arc<RwLock<Option<String>>>,
    /// Bot's display name, also used for @-mention detection.
    bot_name: Arc<RwLock<Option<String>>>,
    /// Cached group chat names: Feishu chat_id → display name.
    chat_names: Arc<RwLock<HashMap<String, String>>>,
}

/// Cached tenant access token with auto-refresh via AuthService.
struct TokenCache {
    token: String,
    expires_at: Instant,
    app_id: String,
    app_secret: String,
}

/// Bi-directional mapping between Atim i64 IDs and Feishu string IDs.
struct IdMap {
    user_ids: HashMap<i64, String>, // hash → open_id
    chat_ids: HashMap<i64, String>, // hash → chat_id
    thread_ids: HashMap<i64, String>, // hash → root_id (for topic-based groups)
}

impl FeishuAdapter {
    pub fn new(app_id: String, app_secret: String, atim_dir: PathBuf) -> Self {
        let id_map_file = atim_dir.join("feishu_id_map.json");
        let adapter = Self {
            app_id: app_id.clone(),
            app_secret: app_secret.clone(),
            client: reqwest::Client::builder()
                .timeout(Duration::from_secs(30))
                .build()
                .expect("valid reqwest client"),
            token_cache: Arc::new(RwLock::new(TokenCache {
                token: String::new(),
                expires_at: Instant::now(),
                app_id,
                app_secret,
            })),
            id_map: Arc::new(RwLock::new(IdMap {
                user_ids: HashMap::new(),
                chat_ids: HashMap::new(),
                thread_ids: HashMap::new(),
            })),
            id_map_file,
            event_tx: Arc::new(RwLock::new(None)),
            bot_open_id: Arc::new(RwLock::new(None)),
            bot_name: Arc::new(RwLock::new(None)),
            chat_names: Arc::new(RwLock::new(HashMap::new())),
        };
        // Load persisted id_map so previously-registered chat_ids resolve on restart
        adapter.load_id_map_sync();
        adapter
    }

    // ── Token management (via openlark AuthService) ──

    /// Get a valid tenant_access_token, refreshing if needed.
    async fn get_token(&self) -> Result<String> {
        let mut cache = self.token_cache.write().await;
        if cache.token.is_empty() || Instant::now() >= cache.expires_at {
            cache.refresh().await?;
        }
        Ok(cache.token.clone())
    }

    // ── API helpers (keep reqwest for REST calls) ──

    /// Send an authenticated POST request to the Feishu API.
    async fn api_post(&self, path: &str, body: &serde_json::Value) -> Result<serde_json::Value> {
        let token = self.get_token().await?;
        let url = format!("{FEISHU_BASE_URL}/open-apis{path}");
        let resp = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {token}"))
            .json(body)
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("HTTP error: {e}")))?;

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Feishu(format!("JSON decode: {e}")))?;

        if json["code"].as_i64().unwrap_or(-1) != 0 {
            let msg = json["msg"].as_str().unwrap_or("unknown");
            return Err(Error::Feishu(format!("API error: {msg}")));
        }
        Ok(json["data"].clone())
    }

    /// Send an authenticated GET request to the Feishu API.
    async fn api_get(&self, path: &str) -> Result<serde_json::Value> {
        let token = self.get_token().await?;
        let url = format!("{FEISHU_BASE_URL}/open-apis{path}");
        let resp = self
            .client
            .get(&url)
            .header("Authorization", format!("Bearer {token}"))
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("HTTP error: {e}")))?;

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Feishu(format!("JSON decode: {e}")))?;

        if json["code"].as_i64().unwrap_or(-1) != 0 {
            let msg = json["msg"].as_str().unwrap_or("unknown");
            return Err(Error::Feishu(format!("API error: {msg}")));
        }
        Ok(json["data"].clone())
    }

    /// Send an authenticated DELETE request (message recall).
    /// Only used when `ATIM_FEISHU_ENABLE_DELETE` is set — otherwise recall is skipped.
    async fn api_delete(&self, path: &str) -> Result<serde_json::Value> {
        let token = self.get_token().await?;
        let url = format!("{FEISHU_BASE_URL}/open-apis{path}");
        let resp = self
            .client
            .delete(&url)
            .header("Authorization", format!("Bearer {token}"))
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("HTTP error: {e}")))?;

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Feishu(format!("JSON decode: {e}")))?;

        let code = json["code"].as_i64().unwrap_or(-1);
        if code != 0 {
            let msg = json["msg"].as_str().unwrap_or("unknown");
            // 1000001 = message too old to recall, 99991663 = token expired
            if code != 1000001 && code != 99991663 {
                return Err(Error::Feishu(format!("API error: {msg}")));
            }
            tracing::warn!("Feishu message recall failed (code {code}): {msg} — continuing");
        }
        Ok(json["data"].clone())
    }

    /// Check whether message deletion/recall is enabled via env var.
    fn delete_enabled() -> bool {
        std::env::var("ATIM_FEISHU_ENABLE_DELETE").is_ok()
    }

    /// Upload image data and return the image_key.
    async fn upload_image(&self, data: &[u8], filename: &str) -> Result<String> {
        let token = self.get_token().await?;
        let url = "https://open.feishu.cn/open-apis/im/v1/images";
        let mime = mime_guess::from_path(filename).first_or_octet_stream();

        let form = reqwest::multipart::Form::new()
            .part(
                "image",
                reqwest::multipart::Part::bytes(data.to_vec())
                    .file_name(filename.to_string())
                    .mime_str(mime.as_ref())
                    .map_err(|e| Error::Feishu(format!("mime error: {e}")))?,
            )
            .text("image_type", "message");

        let resp = self
            .client
            .post(url)
            .header("Authorization", format!("Bearer {token}"))
            .multipart(form)
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("upload error: {e}")))?;

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Feishu(format!("JSON decode: {e}")))?;

        if json["code"].as_i64().unwrap_or(-1) != 0 {
            let msg = json["msg"].as_str().unwrap_or("unknown");
            return Err(Error::Feishu(format!("image upload error: {msg}")));
        }
        Ok(json["data"]["image_key"].as_str().unwrap_or("").to_string())
    }

    // ── ID mapping ──

    /// Deterministically hash a Feishu string ID to i64.
    ///
    /// Uses FNV-1a so the output is stable across process restarts.
    /// `DefaultHasher` (SipHash with random seed) would produce different
    /// values on every process start, breaking persisted state.
    fn hash_id(id: &str) -> i64 {
        let mut hash: u64 = 0xcbf29ce484222325; // FNV offset basis
        for byte in id.bytes() {
            hash ^= byte as u64;
            hash = hash.wrapping_mul(0x100000001b3); // FNV prime
        }
        hash as i64
    }

    /// Register a Feishu open_id and return its stable i64 UserId.
    async fn register_user(&self, open_id: &str) -> UserId {
        let uid = Self::hash_id(open_id);
        {
            let mut map = self.id_map.write().await;
            map.user_ids
                .entry(uid)
                .or_insert_with(|| open_id.to_string());
        }
        self.save_id_map().await;
        UserId(uid)
    }

    /// Register a Feishu chat_id and return its stable i64 ChatId.
    async fn register_chat(&self, chat_id: &str) -> ChatId {
        let cid = Self::hash_id(chat_id);
        {
            let mut map = self.id_map.write().await;
            map.chat_ids
                .entry(cid)
                .or_insert_with(|| chat_id.to_string());
        }
        self.save_id_map().await;
        ChatId(cid)
    }

    /// Register a Feishu topic root_id and return its stable i64 thread_id.
    async fn register_thread(&self, root_id: &str) -> ThreadId {
        let tid = Self::hash_id(root_id);
        {
            let mut map = self.id_map.write().await;
            map.thread_ids
                .entry(tid)
                .or_insert_with(|| root_id.to_string());
        }
        self.save_id_map().await;
        ThreadId(tid)
    }

    /// Look up the original Feishu root_id for a ThreadId.
    async fn get_thread_root_id(&self, tid: &ThreadId) -> Option<String> {
        let map = self.id_map.read().await;
        map.thread_ids.get(&tid.0).cloned()
    }

    /// Look up the original Feishu chat_id for a ChatId.
    async fn resolve_chat(&self, cid: &ChatId) -> Option<String> {
        let map = self.id_map.read().await;
        map.chat_ids.get(&cid.0).cloned()
    }

    /// Fetch the group chat name from Feishu API, caching the result.
    ///
    /// Returns `None` if the API call fails or the name is empty.
    async fn fetch_chat_name(&self, chat_id: &str) -> Option<String> {
        // Check cache first
        {
            let cache = self.chat_names.read().await;
            if let Some(name) = cache.get(chat_id) {
                return Some(name.clone());
            }
        }

        // Fetch from API
        let path = format!("/im/v1/chats/{chat_id}");
        match self.api_get(&path).await {
            Ok(data) => {
                let name = data["name"].as_str().unwrap_or("").to_string();
                if !name.is_empty() {
                    let mut cache = self.chat_names.write().await;
                    cache.insert(chat_id.to_string(), name.clone());
                    Some(name)
                } else {
                    None
                }
            }
            Err(e) => {
                tracing::warn!("Failed to fetch chat name for {chat_id}: {e}");
                None
            }
        }
    }

    /// Add thread_id to a request body if the target has one.
    async fn add_thread_id(&self, body: &mut serde_json::Value, target: &MessageTarget) {
        if let Some(ref thread_id) = target.thread_id
            && let Some(root_id) = self.get_thread_root_id(thread_id).await {
                body["thread_id"] = serde_json::json!(root_id);
            }
    }

    /// Persist the current id_map to disk.
    async fn save_id_map(&self) {
        let map = self.id_map.read().await;
        let data = serde_json::json!({
            "user_ids": map.user_ids,
            "chat_ids": map.chat_ids,
            "thread_ids": map.thread_ids,
        });
        let json = serde_json::to_string_pretty(&data).unwrap_or_default();
        // Use std::fs for simplicity (called infrequently)
        let _ = std::fs::create_dir_all(self.id_map_file.parent().unwrap_or(&self.id_map_file));
        let _ = std::fs::write(&self.id_map_file, &json);
    }

    /// Load id_map from disk synchronously (called once at construction).
    fn load_id_map_sync(&self) {
        let data = match std::fs::read_to_string(&self.id_map_file) {
            Ok(d) => d,
            Err(_) => return,
        };
        let parsed: serde_json::Value = match serde_json::from_str(&data) {
            Ok(v) => v,
            Err(_) => return,
        };
        let mut map = match self.id_map.try_write().ok() {
            Some(m) => m,
            None => return,
        };
        if let Some(users) = parsed["user_ids"].as_object() {
            for (k, v) in users {
                if let (Some(k), Some(v)) = (k.parse::<i64>().ok(), v.as_str()) {
                    map.user_ids.entry(k).or_insert_with(|| v.to_string());
                }
            }
        }
        if let Some(chats) = parsed["chat_ids"].as_object() {
            for (k, v) in chats {
                if let (Some(k), Some(v)) = (k.parse::<i64>().ok(), v.as_str()) {
                    map.chat_ids.entry(k).or_insert_with(|| v.to_string());
                }
            }
        }
        if let Some(threads) = parsed["thread_ids"].as_object() {
            for (k, v) in threads {
                if let (Some(k), Some(v)) = (k.parse::<i64>().ok(), v.as_str()) {
                    map.thread_ids.entry(k).or_insert_with(|| v.to_string());
                }
            }
        }
    }

    /// Fetch bot info (app name, open_id) from Feishu API.
    async fn get_bot_info(&self) -> Result<serde_json::Value> {
        self.api_get("/bot/v3/info").await
    }

    /// Process a raw WS event payload (schema 2.0).
    async fn handle_raw_payload(&self, payload: &[u8]) -> Result<()> {
        let value: serde_json::Value = serde_json::from_slice(payload)
            .map_err(|e| Error::Feishu(format!("payload JSON: {e}")))?;

        let event_type = value["header"]["event_type"].as_str().unwrap_or("");
        if event_type == "card.action.trigger" {
            tracing::debug!(
                "Card payload top keys: {:?}",
                value.as_object().map(|o| o.keys().collect::<Vec<_>>())
            );
            tracing::debug!(
                "Card payload event: {}",
                &value["event"]
                    .to_string()
                    .chars()
                    .take(500)
                    .collect::<String>()
            );
        }
        match event_type {
            "im.message.receive_v1" => handle_message_event(self, &value).await,
            "card.action.trigger" => handle_card_action(self, &value).await,
            _ => {
                tracing::debug!("Unhandled Feishu event type: {event_type}");
                Ok(())
            }
        }
    }
}

// ── Token refresh via direct API call ──

impl TokenCache {
    async fn refresh(&mut self) -> Result<()> {
        let url = format!(
            "{}/open-apis/auth/v3/app_access_token/internal",
            FEISHU_BASE_URL
        );
        let body = serde_json::json!({
            "app_id": self.app_id,
            "app_secret": self.app_secret,
        });

        let resp = reqwest::Client::new()
            .post(&url)
            .json(&body)
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("token request failed: {e}")))?;

        let value: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Feishu(format!("token response parse: {e}")))?;

        let code = value["code"].as_i64().unwrap_or(-1);
        if code != 0 {
            let msg = value["msg"].as_str().unwrap_or("unknown");
            return Err(Error::Feishu(format!(
                "token API error: code={code} msg={msg}"
            )));
        }

        // Feishu returns app_access_token at top level (not nested under "data")
        let token = value["app_access_token"]
            .as_str()
            .ok_or_else(|| Error::Feishu("token response missing app_access_token".into()))?
            .to_string();
        let expire = value["expire"].as_i64().unwrap_or(7200) as u64;

        self.token = token;
        self.expires_at = Instant::now() + Duration::from_secs(expire.saturating_sub(300));

        tracing::info!("Feishu token refreshed, expires in {expire}s");
        Ok(())
    }
}

// ── ImAdapter implementation ──

#[async_trait]
impl ImAdapter for FeishuAdapter {
    async fn run(&self, tx: mpsc::UnboundedSender<ImEvent>) -> Result<()> {
        // Store the event sender so WS event handlers can emit events
        {
            let mut stored = self.event_tx.write().await;
            *stored = Some(tx);
        }

        tracing::info!("Feishu adapter starting (openlark WebSocket)");

        // Fetch bot's own info for @-mention detection
        match self.get_bot_info().await {
            Ok(info) => {
                tracing::debug!("Feishu bot info response: {info}");
                if let Some(open_id) = info["app_open_id"].as_str() {
                    *self.bot_open_id.write().await = Some(open_id.to_string());
                    tracing::info!("Feishu bot open_id: {open_id}");
                } else {
                    tracing::warn!("Feishu bot info response missing app_open_id");
                }
                if let Some(name) = info["app_name"].as_str() {
                    *self.bot_name.write().await = Some(name.to_string());
                    tracing::info!("Feishu bot name: {name}");
                } else {
                    tracing::warn!("Feishu bot info response missing app_name");
                }
            }
            Err(e) => {
                tracing::error!("Failed to fetch Feishu bot info: {e}");
            }
        }

        // Reconnection loop: keep the adapter alive across disconnects
        let mut backoff = 1u64;
        loop {
            let (payload_tx, payload_rx) = mpsc::unbounded_channel::<Vec<u8>>();

            let event_handler = EventDispatcherHandler::builder()
                .payload_sender(payload_tx)
                .build();

            // Spawn payload processing in background task
            let this = self.clone();
            tokio::spawn(async move {
                Self::payload_loop(this, payload_rx).await;
            });

            // Build openlark config for WS connection
            let cfg = Config::builder()
                .app_id(&self.app_id)
                .app_secret(&self.app_secret)
                .base_url(FEISHU_BASE_URL)
                .timeout(Duration::from_secs(30))
                .build()
                .map_err(|e| Error::Feishu(format!("config build: {e}")))?;

            tracing::info!("Connecting to Feishu WS via openlark");
            let result = LarkWsClient::open(Arc::new(cfg), event_handler).await;

            match result {
                Ok(()) => {
                    backoff = 1;
                    tracing::info!("Feishu WS disconnected gracefully, reconnecting");
                }
                Err(e) => {
                    tracing::error!("Feishu WS error: {e}, reconnecting in {backoff}s");
                    backoff = backoff.saturating_mul(2).min(60);
                }
            }

            // If the event channel is closed, the server is shutting down
            if self
                .event_tx
                .read()
                .await
                .as_ref()
                .map(|tx| tx.is_closed())
                .unwrap_or(true)
            {
                tracing::info!("Feishu adapter shutting down");
                return Ok(());
            }

            tokio::time::sleep(Duration::from_secs(backoff)).await;
        }
    }

    async fn send_message(&self, target: &MessageTarget, text: &str) -> Result<MessageId> {
        let chat_id = self
            .resolve_chat(&target.chat_id)
            .await
            .ok_or_else(|| Error::Feishu("unknown chat_id".into()))?;

        // Send as interactive card so Feishu renders markdown and future edits
        // can use PATCH (instead of recall+resend).
        let card = serde_json::json!({
            "config": { "wide_screen_mode": true },
            "elements": [
                { "tag": "markdown", "content": text },
            ],
        });

        let mut body = serde_json::json!({
            "receive_id": chat_id,
            "msg_type": "interactive",
            "content": serde_json::to_string(&card)
                .map_err(|e| Error::Feishu(format!("card serialization: {e}")))?,
        });

        self.add_thread_id(&mut body, target).await;

        let data = self
            .api_post("/im/v1/messages?receive_id_type=chat_id", &body)
            .await?;
        let msg_id = data["message_id"].as_str().unwrap_or("").to_string();
        Ok(MessageId(msg_id))
    }

    async fn edit_message(
        &self,
        target: &MessageTarget,
        msg_id: &MessageId,
        text: &str,
    ) -> Result<()> {
        let chat_id = self
            .resolve_chat(&target.chat_id)
            .await
            .ok_or_else(|| Error::Feishu("unknown chat_id".into()))?;

        let card = serde_json::json!({
            "config": { "wide_screen_mode": true },
            "elements": [
                { "tag": "markdown", "content": text },
            ],
        });
        let card_str = serde_json::to_string(&card)
            .map_err(|e| Error::Feishu(format!("card serialization: {e}")))?;

        // Try PATCH in-place first (works on card messages we sent earlier).
        // Only sends `content` per Feishu PATCH API spec.
        let token = self.get_token().await?;
        let url = format!("{FEISHU_BASE_URL}/open-apis/im/v1/messages/{}", msg_id.0);
        let patch_body = serde_json::json!({ "content": &card_str });
        let resp = self
            .client
            .patch(&url)
            .header("Authorization", format!("Bearer {token}"))
            .json(&patch_body)
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("HTTP error: {e}")))?;

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Feishu(format!("JSON decode: {e}")))?;

        if json["code"].as_i64().unwrap_or(-1) == 0 {
            return Ok(());
        }

        // PATCH failed — try delete+resend as fallback, but only if delete is enabled
        let msg = json["msg"].as_str().unwrap_or("unknown");
        tracing::warn!("edit PATCH failed ({msg}), trying delete+resend");
        if Self::delete_enabled() {
            self.api_delete(&format!("/im/v1/messages/{}", msg_id.0))
                .await
                .ok();
            let mut post_body = serde_json::json!({
                "receive_id": chat_id,
                "msg_type": "interactive",
                "content": &card_str,
            });
            self.add_thread_id(&mut post_body, target).await;
            self.api_post("/im/v1/messages?receive_id_type=chat_id", &post_body)
                .await?;
            Ok(())
        } else {
            Err(Error::Feishu(format!("edit PATCH failed ({msg})")))
        }
    }

    async fn send_photo(
        &self,
        target: &MessageTarget,
        filename: &str,
        data: &[u8],
    ) -> Result<MessageId> {
        let chat_id = self
            .resolve_chat(&target.chat_id)
            .await
            .ok_or_else(|| Error::Feishu("unknown chat_id".into()))?;

        let image_key = self.upload_image(data, filename).await?;
        if image_key.is_empty() {
            return Err(Error::Feishu("image upload returned empty key".into()));
        }

        let content = serde_json::json!({"image_key": image_key});
        let mut body = serde_json::json!({
            "receive_id": chat_id,
            "msg_type": "image",
            "content": content.to_string(),
        });
        self.add_thread_id(&mut body, target).await;

        let data = self
            .api_post("/im/v1/messages?receive_id_type=chat_id", &body)
            .await?;
        let msg_id = data["message_id"].as_str().unwrap_or("").to_string();
        Ok(MessageId(msg_id))
    }

    async fn send_keyboard(
        &self,
        target: &MessageTarget,
        text: &str,
        buttons: &[Vec<Button>],
    ) -> Result<MessageId> {
        let chat_id = self
            .resolve_chat(&target.chat_id)
            .await
            .ok_or_else(|| Error::Feishu("unknown chat_id".into()))?;

        tracing::debug!(
            "send_keyboard to chat_id={chat_id} target_chat={:?} target_thread={:?} text={}",
            target.chat_id.0,
            target.thread_id,
            &text[..text.floor_char_boundary(text.len().min(50))]
        );

        let card = build_card(text, buttons);

        let mut body = serde_json::json!({
            "receive_id": chat_id,
            "msg_type": "interactive",
            "content": serde_json::to_string(&card)
                .map_err(|e| Error::Feishu(format!("card serialization: {e}")))?,
        });
        self.add_thread_id(&mut body, target).await;

        let data = self
            .api_post("/im/v1/messages?receive_id_type=chat_id", &body)
            .await?;
        let msg_id = data["message_id"].as_str().unwrap_or("").to_string();
        Ok(MessageId(msg_id))
    }

    async fn delete_message(&self, _target: &MessageTarget, msg_id: &MessageId) -> Result<()> {
        if Self::delete_enabled() {
            match self
                .api_delete(&format!("/im/v1/messages/{}", msg_id.0))
                .await
            {
                Ok(_) => tracing::debug!("Feishu message {} recalled", msg_id.0),
                Err(e) => tracing::warn!("Feishu message recall failed: {e}"),
            }
        } else {
            tracing::debug!(
                "Feishu delete_message skipped (ATIM_FEISHU_ENABLE_DELETE not set, msg_id={})",
                msg_id.0
            );
        }
        Ok(())
    }

    async fn edit_keyboard(
        &self,
        target: &MessageTarget,
        msg_id: &MessageId,
        buttons: &[Vec<Button>],
    ) -> Result<()> {
        let _chat_id = self
            .resolve_chat(&target.chat_id)
            .await
            .ok_or_else(|| Error::Feishu("unknown chat_id".into()))?;

        let card = build_card("(updated)", buttons);
        let body = serde_json::json!({
            "content": serde_json::to_string(&card)
                .map_err(|e| Error::Feishu(format!("card serialization: {e}")))?,
        });

        let token = self.get_token().await?;
        let url = format!("{FEISHU_BASE_URL}/open-apis/im/v1/messages/{}", msg_id.0);
        let resp = self
            .client
            .patch(&url)
            .header("Authorization", format!("Bearer {token}"))
            .json(&body)
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("HTTP error: {e}")))?;

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Feishu(format!("JSON decode: {e}")))?;

        if json["code"].as_i64().unwrap_or(-1) != 0 {
            let msg = json["msg"].as_str().unwrap_or("unknown");
            tracing::warn!(
                "Feishu edit_keyboard PATCH failed ({msg}), falling back to new message"
            );
            let _ = self.send_keyboard(target, "(updated)", buttons).await?;
        }
        Ok(())
    }

    async fn answer_callback(&self, _callback_query_id: &str, _text: &str) -> Result<()> {
        // Feishu card actions don't have a direct "answer" mechanism.
        Ok(())
    }

    async fn send_chat_action(&self, target: &MessageTarget) -> Result<()> {
        let chat_id = self
            .resolve_chat(&target.chat_id)
            .await
            .ok_or_else(|| Error::Feishu("unknown chat_id".into()))?;

        let token = self.get_token().await?;
        let url = format!(
            "{FEISHU_BASE_URL}/open-apis/im/v1/messages?container_id_type=chat&container_id={chat_id}&page_size=1"
        );
        let resp = self
            .client
            .get(&url)
            .header("Authorization", format!("Bearer {token}"))
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("chat probe error: {e}")))?;

        let json: serde_json::Value = resp
            .json()
            .await
            .map_err(|e| Error::Feishu(format!("JSON decode: {e}")))?;

        let code = json["code"].as_i64().unwrap_or(-1);
        if code != 0 {
            let msg = json["msg"].as_str().unwrap_or("unknown");
            return Err(Error::Feishu(format!("chat probe error ({code}): {msg}")));
        }
        Ok(())
    }
}

// ── Payload processing loop ──

impl FeishuAdapter {
    /// Background task: drain raw WS payloads and dispatch to event handlers.
    async fn payload_loop(
        adapter: FeishuAdapter,
        mut payload_rx: mpsc::UnboundedReceiver<Vec<u8>>,
    ) {
        while let Some(payload) = payload_rx.recv().await {
            if let Err(e) = adapter.handle_raw_payload(&payload).await {
                tracing::error!("Feishu payload handler: {e}");
            }
        }
    }
}

// ── Image download ──

impl FeishuAdapter {
    /// Download an image by its image_key from Feishu.
    async fn download_image(&self, image_key: &str) -> Result<Vec<u8>> {
        let token = self.get_token().await?;
        let url = format!("{FEISHU_BASE_URL}/open-apis/im/v1/images/{image_key}/download");
        let resp = self
            .client
            .get(&url)
            .header("Authorization", format!("Bearer {token}"))
            .send()
            .await
            .map_err(|e| Error::Feishu(format!("image download error: {e}")))?;

        if !resp.status().is_success() {
            return Err(Error::Feishu(format!(
                "image download failed: HTTP {}",
                resp.status()
            )));
        }

        let bytes = resp
            .bytes()
            .await
            .map_err(|e| Error::Feishu(format!("image read error: {e}")))?;
        Ok(bytes.to_vec())
    }
}

impl Clone for FeishuAdapter {
    fn clone(&self) -> Self {
        Self {
            app_id: self.app_id.clone(),
            app_secret: self.app_secret.clone(),
            client: self.client.clone(),
            token_cache: self.token_cache.clone(),
            id_map: self.id_map.clone(),
            id_map_file: self.id_map_file.clone(),
            event_tx: self.event_tx.clone(),
            bot_open_id: self.bot_open_id.clone(),
            bot_name: self.bot_name.clone(),
            chat_names: self.chat_names.clone(),
        }
    }
}

// ── Event handlers ──

async fn handle_message_event(adapter: &FeishuAdapter, payload: &serde_json::Value) -> Result<()> {
    let event = &payload["event"];
    let sender = &event["sender"];
    let message = &event["message"];

    let open_id = sender["sender_id"]["open_id"]
        .as_str()
        .ok_or_else(|| Error::Feishu("missing open_id".into()))?;
    let chat_id = message["chat_id"]
        .as_str()
        .ok_or_else(|| Error::Feishu("missing chat_id".into()))?;
    let msg_type = message["message_type"]
        .as_str()
        .or_else(|| message["msg_type"].as_str())
        .unwrap_or("");
    let _message_id = message["message_id"].as_str().unwrap_or("");
    let chat_type = message["chat_type"].as_str().unwrap_or("p2p");
    let root_id = message["root_id"].as_str(); // thread root (if any)

    tracing::debug!("Feishu message: type={msg_type} chat={chat_type} from={open_id}");

    let user_id = adapter.register_user(open_id).await;
    let chat_id_atim = adapter.register_chat(chat_id).await;

    // For topic/thread messages, use root_id as thread_id (consistent per-topic routing).
    // For non-topic group chats, use the chat_id as thread_id — each Feishu group chat
    // is equivalent to a Telegram topic thread in atim's binding model.
    let thread_id = if let Some(root_id) = root_id {
        Some(adapter.register_thread(root_id).await)
    } else if chat_type == "group" {
        Some(ThreadId(adapter.register_chat(chat_id).await.0))
    } else {
        None
    };

    let target = MessageTarget {
        chat_id: chat_id_atim,
        thread_id,
    };

    // For group chats, fetch the chat name and emit TopicCreated so the
    // server can use it as the window/topic name (e.g. "atim" instead of
    // "atim-<user_id>").
    if chat_type == "group"
        && let Some(chat_name) = adapter.fetch_chat_name(chat_id).await {
            let topic_target = MessageTarget {
                chat_id: chat_id_atim,
                thread_id,
            };
            if let Some(tx) = adapter.event_tx.read().await.as_ref() {
                let _ = tx.send(ImEvent {
                    user_id,
                    target: topic_target,
                    kind: ImEventKind::TopicCreated { name: chat_name },
                });
            }
        }

    let parsed_content: serde_json::Value =
        serde_json::from_str(message["content"].as_str().unwrap_or("{}")).unwrap_or_default();

    let mut text = String::new();
    let mut has_mention = false;

    let kind = match msg_type {
        "text" => {
            let raw = parsed_content["text"].as_str().unwrap_or("").to_string();
            text = raw.clone();
            // Check for @-mention of the bot in group chats
            if chat_type == "group" {
                let bot_id = adapter.bot_open_id.read().await;
                let bot_name = adapter.bot_name.read().await;
                let mentions = message["mentions"].as_array();
                if let Some(mentions) = mentions {
                    for mention in mentions {
                        // Check by bot open_id (preferred)
                        let is_bot = match bot_id.as_ref() {
                            Some(bid) => mention["id"]["open_id"].as_str() == Some(bid),
                            None => false,
                        };
                        // Fallback: check by name
                        let is_bot = is_bot
                            || bot_name.as_ref().is_some_and(|name| {
                                mention["name"]
                                    .as_str()
                                    .is_some_and(|n| n.contains(name) || name.contains(n))
                            });
                        // Broader fallback: any mention named "bot" or the app_id prefix
                        let is_bot = is_bot
                            || mention["name"]
                                .as_str()
                                .is_some_and(|n| n.contains("bot") || adapter.app_id.contains(n));
                        if is_bot {
                            has_mention = true;
                        }
                        // Strip mention placeholder from text
                        if let Some(key) = mention["key"].as_str() {
                            text = text.replace(key, "").trim().to_string();
                        }
                    }
                }
                // Permissive fallback: if mentions array exists and is non-empty but we
                // couldn't match the bot's identity, treat any mention as a bot mention
                if !has_mention && message["mentions"].as_array().map_or(0, |m| m.len()) > 0 {
                    tracing::debug!(
                        "Mentions present but no bot match — permissive fallback, treating as @mention"
                    );
                    has_mention = true;
                }
            }
            ImEventKind::Text {
                text: text.clone(),
                is_mention: has_mention,
                is_group: chat_type == "group",
            }
        }
        "post" => {
            // Rich text: extract from locale-aware content
            let content = &parsed_content;
            let locale = if content["zh_cn"].is_object() {
                "zh_cn"
            } else if content["en_us"].is_object() {
                "en_us"
            } else if content["ja_jp"].is_object() {
                "ja_jp"
            } else {
                ""
            };

            if !locale.is_empty() {
                text = extract_post_text(&content[locale]);
            }
            if text.is_empty() {
                text = parsed_content["text"].as_str().unwrap_or("").to_string();
            }
            ImEventKind::Text {
                text: text.clone(),
                is_mention: has_mention,
                is_group: chat_type == "group",
            }
        }
        "image" => {
            let image_key = parsed_content["image_key"].as_str().unwrap_or("");
            let data = adapter.download_image(image_key).await.unwrap_or_default();
            ImEventKind::Photo {
                caption: None,
                data,
                mime_type: "image/png".into(),
            }
        }
        _ => {
            tracing::debug!("Skipping unknown msg_type: {msg_type}");
            return Ok(()); // skip audio, file, sticker, etc.
        }
    };

    tracing::debug!("Forwarding Feishu message to event loop");
    if let Some(tx) = adapter.event_tx.read().await.as_ref() {
        let _ = tx.send(ImEvent {
            user_id,
            target,
            kind,
        });
    }

    Ok(())
}

async fn handle_card_action(adapter: &FeishuAdapter, payload: &serde_json::Value) -> Result<()> {
    let event = &payload["event"];
    let action_value = &event["action"]["value"];

    // WS mode: fields are nested under operator/context, not at event top level.
    // Webhook mode: fields are at event top level.
    let operator = &event["operator"];
    let context = &event["context"];

    let open_id = operator["open_id"]
        .as_str()
        .filter(|s| !s.is_empty())
        .or_else(|| event["open_id"].as_str())
        .unwrap_or("");

    let chat_id = context["open_chat_id"]
        .as_str()
        .filter(|s| !s.is_empty())
        .or_else(|| event["chat_id"].as_str())
        .unwrap_or(open_id); // fallback to open_id for p2p

    let message_id = context["open_message_id"]
        .as_str()
        .filter(|s| !s.is_empty())
        .or_else(|| event["message_id"].as_str())
        .unwrap_or("");

    // Determine chat_type from explicit field or infer from chat_id prefix
    let explicit_chat_type = event["chat_type"].as_str().unwrap_or("p2p");
    let is_group = explicit_chat_type == "group" || chat_id.starts_with("oc_");
    let chat_type = if is_group { "group" } else { "p2p" };

    tracing::debug!(
        "Card action event: open_id={open_id} chat_id={chat_id} chat_type={chat_type} msg_id={message_id}"
    );

    // Feishu card action values are JSON objects — extract the "action" field
    let data = match action_value["action"].as_str() {
        Some(a) => a.to_string(),
        None => action_value["data"]
            .as_str()
            .map(String::from)
            .unwrap_or_else(|| serde_json::to_string(action_value).unwrap_or_default()),
    };
    tracing::debug!("Card action: data={data} chat_type={chat_type}");

    let user_id = adapter.register_user(open_id).await;

    // Check for thread_id in the card action context (Feishu may include it
    // for messages sent to topic threads), or root_id in the event envelope.
    let root_id = event["root_id"].as_str().filter(|s| !s.is_empty());
    let ctx_thread_id = context["thread_id"].as_str().filter(|s| !s.is_empty());

    let thread_id = if let Some(tid) = ctx_thread_id.or(root_id) {
        Some(adapter.register_thread(tid).await)
    } else if is_group {
        // Fallback: use chat_id as thread_id (distinguishes group chats)
        Some(ThreadId(adapter.register_chat(chat_id).await.0))
    } else {
        None
    };

    let target = MessageTarget {
        chat_id: adapter.register_chat(chat_id).await,
        thread_id,
    };

    let action_token = event["action"]["token"]
        .as_str()
        .or_else(|| event["action_token"].as_str())
        .map(String::from);

    if let Some(tx) = adapter.event_tx.read().await.as_ref() {
        let _ = tx.send(ImEvent {
            user_id,
            target,
            kind: ImEventKind::CallbackQuery {
                data,
                msg_id: MessageId(message_id.to_string()),
                callback_query_id: action_token,
            },
        });
    }

    Ok(())
}

// ── Card builder ──

/// Build a Feishu interactive card JSON for permission prompts / choices.
fn build_card(text: &str, buttons: &[Vec<Button>]) -> serde_json::Value {
    let mut elements: Vec<serde_json::Value> = vec![serde_json::json!({
        "tag": "markdown",
        "content": text,
    })];

    for row in buttons {
        let actions: Vec<serde_json::Value> = row
            .iter()
            .map(|btn| {
                let btn_type = if btn.callback_data.contains("approve")
                    || btn.callback_data.contains("yes")
                    || btn.callback_data.contains("confirm")
                {
                    "primary"
                } else if btn.callback_data.contains("reject") || btn.callback_data.contains("no") {
                    "danger"
                } else {
                    "default"
                };

                serde_json::json!({
                    "tag": "button",
                    "text": {
                        "tag": "plain_text",
                        "content": btn.text,
                    },
                    "value": {
                        "action": btn.callback_data,
                    },
                    "type": btn_type,
                })
            })
            .collect();

        if !actions.is_empty() {
            elements.push(serde_json::json!({
                "tag": "action",
                "actions": actions,
            }));
        }
    }

    serde_json::json!({
        "config": {
            "wide_screen_mode": true,
        },
        "header": {
            "title": {
                "tag": "plain_text",
                "content": "Atim — Agent Response",
            },
            "template": "blue",
        },
        "elements": elements,
    })
}

/// Extract plain text from a Feishu `post` rich-text block.
fn extract_post_content_text(content: &serde_json::Value) -> String {
    let mut result = String::new();

    if let Some(title) = content["title"].as_str()
        && !title.is_empty() {
            result.push_str(title);
            result.push('\n');
        }

    if let Some(paragraphs) = content["content"].as_array() {
        for (i, para) in paragraphs.iter().enumerate() {
            if i > 0 {
                result.push('\n');
            }
            if let Some(elements) = para.as_array() {
                for el in elements {
                    let tag = el["tag"].as_str().unwrap_or("");
                    match tag {
                        "text" => {
                            if let Some(t) = el["text"].as_str() {
                                result.push_str(t);
                            }
                        }
                        "a" => {
                            if let Some(t) = el["text"].as_str() {
                                result.push_str(t);
                            }
                            if let Some(href) = el["href"].as_str() {
                                result.push_str(&format!(" ({href})"));
                            }
                        }
                        "at" => {
                            if let Some(name) = el["user_name"].as_str() {
                                result.push('@');
                                result.push_str(name);
                            } else if let Some(open_id) = el["user_id"].as_str() {
                                result.push_str(&format!("@user:{open_id}"));
                            }
                        }
                        "md" => {
                            if let Some(t) = el["text"].as_str() {
                                result.push_str(t);
                            }
                        }
                        "img" => {
                            result.push_str(" [image] ");
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    result
}

/// Shorthand wrapper for the helper above.
fn extract_post_text(post_content: &serde_json::Value) -> String {
    extract_post_content_text(post_content)
}

// ── Tests ──

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

    #[test]
    fn test_hash_id_deterministic() {
        let id = "ou_abc123";
        let h1 = FeishuAdapter::hash_id(id);
        let h2 = FeishuAdapter::hash_id(id);
        assert_eq!(h1, h2);
    }

    #[test]
    fn test_hash_id_unique_per_input() {
        let h1 = FeishuAdapter::hash_id("ou_a");
        let h2 = FeishuAdapter::hash_id("ou_b");
        assert_ne!(h1, h2);
    }

    #[test]
    fn test_build_card() {
        let buttons = vec![
            vec![Button {
                text: "Yes".into(),
                callback_data: "approve".into(),
            }],
            vec![Button {
                text: "No".into(),
                callback_data: "reject".into(),
            }],
        ];

        let card = build_card("Proceed?", &buttons);
        assert_eq!(card["header"]["title"]["content"], "Atim — Agent Response");
        assert_eq!(card["elements"].as_array().unwrap().len(), 3);

        let actions_row0 = card["elements"][1]["actions"].as_array().unwrap();
        assert_eq!(actions_row0[0]["text"]["content"], "Yes");
        assert_eq!(actions_row0[0]["type"], "primary");
        let actions_row1 = card["elements"][2]["actions"].as_array().unwrap();
        assert_eq!(actions_row1[0]["text"]["content"], "No");
        assert_eq!(actions_row1[0]["type"], "danger");
    }

    #[test]
    fn test_extract_post_content_text_simple() {
        let content = serde_json::json!({
            "title": "Note",
            "content": [[
                { "tag": "text", "text": "Hello, world!" }
            ]]
        });
        let result = extract_post_content_text(&content);
        assert!(result.contains("Hello, world!"));
        assert!(result.contains("Note"));
    }

    #[test]
    fn test_extract_post_content_text_with_link() {
        let content = serde_json::json!({
            "title": "",
            "content": [[
                { "tag": "text", "text": "Check out " },
                { "tag": "a", "text": "this link", "href": "https://example.com" }
            ]]
        });
        let result = extract_post_content_text(&content);
        assert!(result.contains("Check out"));
        assert!(result.contains("this link"));
        assert!(result.contains("example.com"));
    }

    #[test]
    fn test_extract_post_content_text_with_mention() {
        let content = serde_json::json!({
            "title": "",
            "content": [[
                { "tag": "text", "text": "Hey " },
                { "tag": "at", "user_name": "Alice", "user_id": "ou_xxx" }
            ]]
        });
        let result = extract_post_content_text(&content);
        assert!(result.contains("Hey"));
        assert!(result.contains("Alice"));
    }

    #[test]
    fn test_extract_post_content_text_multi_paragraph() {
        let content = serde_json::json!({
            "title": "",
            "content": [
                [{ "tag": "text", "text": "First paragraph" }],
                [{ "tag": "text", "text": "Second paragraph" }],
            ]
        });
        let result = extract_post_content_text(&content);
        assert!(result.contains("First paragraph"));
        assert!(result.contains("Second paragraph"));
        let lines: Vec<&str> = result.lines().collect();
        assert_eq!(lines.len(), 2);
    }

    #[test]
    fn test_extract_post_content_text_empty() {
        let content = serde_json::json!({});
        let result = extract_post_content_text(&content);
        assert_eq!(result, "");
    }

    #[test]
    fn test_extract_post_content_text_markdown_tag() {
        let content = serde_json::json!({
            "title": "",
            "content": [[
                { "tag": "md", "text": "**bold** and `code`" }
            ]]
        });
        let result = extract_post_content_text(&content);
        assert!(result.contains("bold"));
        assert!(result.contains("code"));
    }

    #[test]
    fn test_extract_post_content_text_image_placeholder() {
        let content = serde_json::json!({
            "title": "",
            "content": [[
                { "tag": "text", "text": "See this: " },
                { "tag": "img", "image_key": "img_xxx" }
            ]]
        });
        let result = extract_post_content_text(&content);
        assert!(result.contains("See this"));
        assert!(result.contains("[image]"));
    }
}