lark-webhook-notify 0.1.1

Send rich notification cards to Lark (Feishu) bots via webhooks
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
use chrono::Local;
use serde_json::Value;

use crate::blocks;
use crate::blocks::{
    Card, CollapsiblePanel, Column, ColumnSet, ColumnWidth, HeaderBlock, Markdown, TextAlign,
    TextSize, TextTag,
};

/// A fully-rendered Lark card as a `serde_json::Value`.
///
/// Produced by [`LarkTemplate::generate`] and consumed by
/// [`LarkWebhookNotifier::send_raw_content`].
pub type CardContent = serde_json::Value;

/// Language for translated field labels in card bodies.
///
/// User-supplied strings (task names, descriptions, error messages) are always
/// passed through unchanged. Only built-in label keys (e.g. "Job Name",
/// "Start Time") are translated.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LanguageCode {
    /// Simplified Chinese (`zh`)
    Zh,
    /// English (`en`)
    En,
}

/// Severity of an alert notification, controlling header color and icon.
///
/// | Level | Color | Icon |
/// |-------|-------|------|
/// | `Info` | Blue | `:InfoCircle:` |
/// | `Warning` | Orange | `:WarningTriangle:` |
/// | `Error` | Red | `:CrossMark:` |
/// | `Critical` | Red | `:Fire:` |
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SeverityLevel {
    Info,
    Warning,
    Error,
    Critical,
}

/// Card header background color / status tag color.
///
/// Maps directly to the Lark card API `"template"` field values.
/// [`CardBuilder::header`] can auto-detect the color from a status string —
/// supply `None` for `color` to use that behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorTheme {
    /// `"blue"` — informational / neutral
    Blue,
    /// `"green"` — success / completed
    Green,
    /// `"red"` — failure / error
    Red,
    /// `"orange"` — warning / caution
    Orange,
    /// `"wathet"` — in-progress / running
    Wathet,
    /// `"purple"`
    Purple,
    /// `"grey"`
    Grey,
}

impl ColorTheme {
    pub fn as_str(&self) -> &'static str {
        match self {
            ColorTheme::Blue => "blue",
            ColorTheme::Green => "green",
            ColorTheme::Red => "red",
            ColorTheme::Orange => "orange",
            ColorTheme::Wathet => "wathet",
            ColorTheme::Purple => "purple",
            ColorTheme::Grey => "grey",
        }
    }
}

impl std::fmt::Display for ColorTheme {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl std::str::FromStr for ColorTheme {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "blue" => Ok(ColorTheme::Blue),
            "green" => Ok(ColorTheme::Green),
            "red" => Ok(ColorTheme::Red),
            "orange" => Ok(ColorTheme::Orange),
            "wathet" => Ok(ColorTheme::Wathet),
            "purple" => Ok(ColorTheme::Purple),
            "grey" | "gray" => Ok(ColorTheme::Grey),
            _ => Err(format!("unknown color: {s}")),
        }
    }
}

impl SeverityLevel {
    pub fn as_str(&self) -> &'static str {
        match self {
            SeverityLevel::Info => "info",
            SeverityLevel::Warning => "warning",
            SeverityLevel::Error => "error",
            SeverityLevel::Critical => "critical",
        }
    }

    pub fn color(&self) -> &'static str {
        match self {
            SeverityLevel::Info => "blue",
            SeverityLevel::Warning => "orange",
            SeverityLevel::Error | SeverityLevel::Critical => "red",
        }
    }

    pub fn icon(&self) -> &'static str {
        match self {
            SeverityLevel::Info => ":InfoCircle:",
            SeverityLevel::Warning => ":WarningTriangle:",
            SeverityLevel::Error => ":CrossMark:",
            SeverityLevel::Critical => ":Fire:",
        }
    }
}

impl std::fmt::Display for SeverityLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl std::str::FromStr for SeverityLevel {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "info" => Ok(SeverityLevel::Info),
            "warning" => Ok(SeverityLevel::Warning),
            "error" => Ok(SeverityLevel::Error),
            "critical" => Ok(SeverityLevel::Critical),
            _ => Err(format!("unknown severity: {s}")),
        }
    }
}

impl std::fmt::Display for LanguageCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LanguageCode::Zh => write!(f, "zh"),
            LanguageCode::En => write!(f, "en"),
        }
    }
}

impl std::str::FromStr for LanguageCode {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "zh" => Ok(LanguageCode::Zh),
            "en" => Ok(LanguageCode::En),
            _ => Err(format!("unknown language: {s}")),
        }
    }
}

pub fn get_translation(key: &str, lang: LanguageCode) -> String {
    let s = match lang {
        LanguageCode::Zh => zh_translation(key),
        LanguageCode::En => en_translation(key),
    };
    s.map(|s| s.to_owned()).unwrap_or_else(|| key.to_owned())
}

fn zh_translation(key: &str) -> Option<&'static str> {
    Some(match key {
        "task_name" => "作业名称",
        "start_time" => "开始时间",
        "completion_time" => "完成时间",
        "task_description" => "作业描述",
        "estimated_duration" => "预计用时",
        "execution_duration" => "执行时长",
        "execution_status" => "执行状态",
        "result_storage" => "结果存储",
        "storage_prefix" => "存储前缀",
        "result_overview" => "结果概览",
        "running_overview" => "运行概览",
        "state_overview" => "状态概览",
        "metadata_overview" => "元数据概览",
        "running" => "正在运行",
        "completed" => "已成功完成",
        "failed" => "失败",
        "success" => "已完成",
        "failure" => "失败",
        "task_notification" => "作业运行情况通知",
        "task_completion_notification" => "作业完成情况通知",
        "task_failure_notification" => "作业失败情况通知",
        "no_description" => "*No description provided*",
        "timestamp" => "时间",
        "unknown_task" => "未知任务",
        "return_code" => "返回值",
        "group" => "归属组别",
        "network_submission_started" => "网络提交已开始",
        "network_submission_complete" => "网络提交已完成",
        "network_submission_failed" => "网络提交失败",
        "network_set_name" => "网络集名称",
        "network_type" => "网络类型",
        "expected_count" => "预期数量",
        "submitted_count" => "提交总数",
        "submitted" => "已提交",
        "config_uploaded" => "配置已上传",
        "config_name" => "配置名称",
        "files_uploaded" => "已上传文件数",
        "description" => "配置描述",
        "uploaded_files" => "已上传文件的标签",
        "task_submission_started" => "任务提交已开始",
        "task_submission_complete" => "任务提交已完成",
        "task_submission_failed" => "任务提交失败",
        "task_set_name" => "任务集名称",
        "iterations" => "迭代次数",
        "duration" => "持续时间",
        "submission_overview" => "提交概览",
        "status" => "状态",
        "successfully_completed" => "已成功完成",
        "submitted_before_failure" => "失败前已提交",
        "error_details" => "错误详情",
        "task_set_complete" => "任务集已完成",
        "task_set_failed" => "任务集失败",
        "task_set_progress" => "任务集进度",
        "task_set_count" => "任务集数量",
        "result_collection_started" => "结果收集已开始",
        "result_collection_complete" => "结果收集已完成",
        "task_sets" => "任务集",
        "rows" => "行数",
        "columns" => "列数",
        "comparison_complete" => "比较已完成",
        "comparison_name" => "比较名称",
        "task_sets_compared" => "已比较任务集",
        "common_networks" => "公共网络数",
        "result_rows" => "结果行数",
        "result_columns" => "结果列数",
        "comparison_results" => "比较结果",
        "total_items" => "总项目数",
        "summary" => "摘要",
        "items" => "项目",
        _ => return None,
    })
}

fn en_translation(key: &str) -> Option<&'static str> {
    Some(match key {
        "task_name" => "Job Name",
        "start_time" => "Start Time",
        "completion_time" => "Completion Time",
        "task_description" => "Job Description",
        "estimated_duration" => "Estimated Duration",
        "execution_duration" => "Execution Duration",
        "execution_status" => "Execution Status",
        "result_storage" => "Result Storage",
        "storage_prefix" => "Storage Prefix",
        "result_overview" => "Result Overview",
        "running_overview" => "Running Overview",
        "state_overview" => "State Overview",
        "metadata_overview" => "Metadata Overview",
        "running" => "Running Now",
        "completed" => "Successfully Completed",
        "failed" => "Failed",
        "success" => "Completed",
        "failure" => "Failed",
        "task_notification" => "Job Status Notification",
        "task_completion_notification" => "Job Completion Notification",
        "task_failure_notification" => "Job Failure Notification",
        "no_description" => "*No description provided*",
        "timestamp" => "Timestamp",
        "unknown_task" => "Unknown Task",
        "return_code" => "Return Status",
        "group" => "Group",
        "network_submission_started" => "Network Submission Started",
        "network_submission_complete" => "Network Submission Complete",
        "network_submission_failed" => "Network Submission Failed",
        "network_set_name" => "Network Set Name",
        "network_type" => "Network Type",
        "expected_count" => "Expected Count",
        "submitted_count" => "Total Count Submitted",
        "submitted" => "Submitted",
        "config_uploaded" => "Configuration Uploaded",
        "config_name" => "Config Name",
        "files_uploaded" => "Files Uploaded",
        "description" => "Config Description",
        "uploaded_files" => "Labels of Uploaded Files",
        "task_submission_started" => "Task Submission Started",
        "task_submission_complete" => "Task Submission Complete",
        "task_submission_failed" => "Task Submission Failed",
        "task_set_name" => "Task Set Name",
        "iterations" => "Iterations",
        "duration" => "Duration",
        "submission_overview" => "Submission Overview",
        "status" => "Status",
        "successfully_completed" => "Successfully Completed",
        "submitted_before_failure" => "Submitted Before Failure",
        "error_details" => "Error Details",
        "task_set_complete" => "Task Set Complete",
        "task_set_failed" => "Task Set Failed",
        "task_set_progress" => "Task Set Progress",
        "task_set_count" => "Task Set Count",
        "result_collection_started" => "Result Collection Started",
        "result_collection_complete" => "Result Collection Complete",
        "task_sets" => "Task Sets",
        "rows" => "Rows",
        "columns" => "Columns",
        "comparison_complete" => "Comparison Complete",
        "comparison_name" => "Comparison Name",
        "task_sets_compared" => "Task Sets Compared",
        "common_networks" => "Common Networks",
        "result_rows" => "Result Rows",
        "result_columns" => "Result Columns",
        "comparison_results" => "Comparison Results",
        "total_items" => "Total Items",
        "summary" => "Summary",
        "items" => "Items",
        _ => return None,
    })
}

/// A type that can render itself into a Lark card JSON payload.
///
/// Implement this trait to define custom templates that work with
/// [`LarkWebhookNotifier::send_template`].
///
/// All pre-built template structs and the output of [`CardBuilder::build`]
/// implement this trait.
pub trait LarkTemplate {
    /// Render this template into a [`CardContent`] (a `serde_json::Value`).
    fn generate(&self) -> CardContent;
}

/// Legacy task notification using a fixed Lark template reference ID.
///
/// Sends a card built from a Lark-hosted template (`template_id = "AAqz08XD5HCzP"`)
/// with variables injected at render time. Use [`StartTaskTemplate`] or
/// [`ReportTaskResultTemplate`] for fully custom cards instead.
pub struct LegacyTaskTemplate {
    pub task_name: String,
    pub status: Option<i32>,
    pub group: String,
    pub prefix: String,
    pub task_summary: String,
    pub language: LanguageCode,
}

impl LegacyTaskTemplate {
    fn t(&self, key: &str) -> String {
        get_translation(key, self.language)
    }
}

impl LarkTemplate for LegacyTaskTemplate {
    fn generate(&self) -> CardContent {
        let task_time = Local::now().format("%Y-%m-%d %H:%M").to_string();
        let task_status = match self.status {
            Some(s) if s != 0 => format!(
                "<font color='red'> :CrossMark: {}: {} {s}</font>",
                self.t("failed"),
                self.t("return_code")
            ),
            _ => format!(
                "<font color='green'> :CheckMark: {}</font>",
                self.t("completed")
            ),
        };
        blocks::template_reference(
            "AAqz08XD5HCzP",
            "1.0.3",
            serde_json::json!({
                "task_name": self.task_name,
                "task_time": task_time,
                "attachment_group": self.group,
                "attachment_prefix": self.prefix,
                "task_summary": self.task_summary,
                "task_status": task_status,
            }),
        )
        .into()
    }
}

/// Card template for notifying that a job or task has started.
///
/// Generates a wathet (light blue) header card with the task name, start time,
/// optional description/duration, running status, and optionally a storage
/// column pair and collapsible running-overview panel.
pub struct StartTaskTemplate {
    pub task_name: String,
    pub desc: Option<String>,
    pub group: Option<String>,
    pub prefix: Option<String>,
    pub msg: Option<String>,
    pub estimated_duration: Option<String>,
    pub language: LanguageCode,
}

impl StartTaskTemplate {
    fn t(&self, key: &str) -> String {
        get_translation(key, self.language)
    }
}

impl LarkTemplate for StartTaskTemplate {
    fn generate(&self) -> CardContent {
        let task_time = Local::now().format("%Y-%m-%d %H:%M").to_string();
        let no_desc = get_translation("no_description", self.language);
        let desc = self.desc.as_deref().unwrap_or(&no_desc);
        let duration_text = self
            .estimated_duration
            .as_ref()
            .map(|d| format!("\n**{}:** {d}", self.t("estimated_duration")))
            .unwrap_or_default();
        let task_status = format!(
            "<font color='wathet-400'> :StatusInFlight: {}</font>",
            self.t("running")
        );
        let main_text = format!(
            "**{}:** {}\n**{}:** {}\n**{}:** {}{}\n**{}:** {}",
            self.t("task_name"),
            self.task_name,
            self.t("start_time"),
            task_time,
            self.t("task_description"),
            desc,
            duration_text,
            self.t("execution_status"),
            task_status,
        );

        let mut elements = vec![blocks::markdown(&main_text).into()];

        if self.group.is_some() || self.prefix.is_some() {
            elements.push(storage_columns(
                &self.t("result_storage"),
                self.group.as_deref().unwrap_or(""),
                &self.t("storage_prefix"),
                self.prefix.as_deref().unwrap_or(""),
            ));
        }

        if let Some(msg) = &self.msg {
            elements.push(overview_panel(&self.t("running_overview"), msg));
        }

        let hdr: Value = HeaderBlock {
            title: self.t("task_notification"),
            template: "wathet".into(),
            subtitle: Some("".into()),
            text_tag_list: Some(vec![
                TextTag {
                    text: self.t("running"),
                    color: "wathet".into(),
                }
                .into(),
            ]),
            padding: Some("12px 8px 12px 8px".into()),
        }
        .into();
        Card {
            elements,
            header: hdr,
            config: Some(blocks::config_textsize_normal_v2()),
            ..Default::default()
        }
        .into()
    }
}

/// Card template for reporting that a job completed successfully (green header).
///
/// Shows task name, completion time, optional description, duration, and
/// optionally a storage column pair and result-overview panel.
/// For failure notifications use [`ReportFailureTaskTemplate`].
pub struct ReportTaskResultTemplate {
    pub task_name: String,
    pub status: i32,
    pub group: Option<String>,
    pub prefix: Option<String>,
    pub desc: Option<String>,
    pub msg: Option<String>,
    pub duration: Option<String>,
    pub title: Option<String>,
    pub language: LanguageCode,
}

impl ReportTaskResultTemplate {
    fn t(&self, key: &str) -> String {
        get_translation(key, self.language)
    }
}

impl LarkTemplate for ReportTaskResultTemplate {
    fn generate(&self) -> CardContent {
        let task_time = Local::now().format("%Y-%m-%d %H:%M").to_string();
        let task_status = format!(
            "<font color='green'> :CheckMark: {}</font>",
            self.t("completed")
        );
        let desc_text = self
            .desc
            .as_ref()
            .map(|d| format!("\n**{}:** {d}", self.t("task_description")))
            .unwrap_or_default();
        let dur_text = self
            .duration
            .as_ref()
            .map(|d| format!("\n**{}:** {d}", self.t("execution_duration")))
            .unwrap_or_default();
        let main_text = format!(
            "**{}:** {}\n**{}:** {}{}{}\n**{}:** {}",
            self.t("task_name"),
            self.task_name,
            self.t("completion_time"),
            task_time,
            desc_text,
            dur_text,
            self.t("execution_status"),
            task_status,
        );

        let mut elements = vec![blocks::markdown(&main_text).into()];
        if self.group.is_some() || self.prefix.is_some() {
            elements.push(storage_columns(
                &self.t("group"),
                self.group.as_deref().unwrap_or(""),
                &self.t("storage_prefix"),
                self.prefix.as_deref().unwrap_or(""),
            ));
        }
        if let Some(msg) = &self.msg {
            elements.push(overview_panel(&self.t("result_overview"), msg));
        }

        let default_title = self.t("task_completion_notification");
        let card_title = self.title.as_deref().unwrap_or(&default_title);
        let hdr: Value = HeaderBlock {
            title: card_title.into(),
            template: "green".into(),
            subtitle: Some("".into()),
            text_tag_list: Some(vec![
                TextTag {
                    text: self.t("success"),
                    color: "green".into(),
                }
                .into(),
            ]),
            padding: Some("12px 8px 12px 8px".into()),
        }
        .into();
        Card {
            elements,
            header: hdr,
            config: Some(blocks::config_textsize_normal_v2()),
            ..Default::default()
        }
        .into()
    }
}

/// Card template for reporting a job failure (red header).
///
/// Shows task name, completion time, non-zero exit `status`, optional
/// description/duration, and optionally a storage column pair and
/// result-overview panel.
pub struct ReportFailureTaskTemplate {
    pub task_name: String,
    /// Non-zero process exit code. Displayed in the status line.
    pub status: i32,
    pub group: Option<String>,
    pub prefix: Option<String>,
    pub desc: Option<String>,
    pub msg: Option<String>,
    pub duration: Option<String>,
    pub title: Option<String>,
    pub language: LanguageCode,
}

impl ReportFailureTaskTemplate {
    fn t(&self, key: &str) -> String {
        get_translation(key, self.language)
    }
}

impl LarkTemplate for ReportFailureTaskTemplate {
    fn generate(&self) -> CardContent {
        let task_time = Local::now().format("%Y-%m-%d %H:%M").to_string();
        let status = self.status;
        let task_status = format!(
            "<font color='red'> :CrossMark: {}: {status}</font>",
            self.t("failed")
        );
        let desc_text = self
            .desc
            .as_ref()
            .map(|d| format!("\n**{}:** {d}", self.t("task_description")))
            .unwrap_or_default();
        let dur_text = self
            .duration
            .as_ref()
            .map(|d| format!("\n**{}:** {d}", self.t("execution_duration")))
            .unwrap_or_default();
        let main_text = format!(
            "**{}:** {}\n**{}:** {}{}{}\n**{}:** {}",
            self.t("task_name"),
            self.task_name,
            self.t("completion_time"),
            task_time,
            desc_text,
            dur_text,
            self.t("execution_status"),
            task_status,
        );

        let mut elements = vec![blocks::markdown(&main_text).into()];
        if self.group.is_some() || self.prefix.is_some() {
            elements.push(storage_columns(
                &self.t("group"),
                self.group.as_deref().unwrap_or(""),
                &self.t("storage_prefix"),
                self.prefix.as_deref().unwrap_or(""),
            ));
        }
        if let Some(msg) = &self.msg {
            elements.push(overview_panel(&self.t("result_overview"), msg));
        }

        let default_title = self.t("task_failure_notification");
        let card_title = self.title.as_deref().unwrap_or(&default_title);
        let hdr: Value = HeaderBlock {
            title: card_title.into(),
            template: "red".into(),
            subtitle: Some("".into()),
            text_tag_list: Some(vec![
                TextTag {
                    text: self.t("failure"),
                    color: "red".into(),
                }
                .into(),
            ]),
            padding: Some("12px 8px 12px 8px".into()),
        }
        .into();
        Card {
            elements,
            header: hdr,
            config: Some(blocks::config_textsize_normal_v2()),
            ..Default::default()
        }
        .into()
    }
}

/// A simple one-block card with a title, a color, and a markdown body.
pub struct SimpleMessageTemplate {
    pub title: String,
    pub content: String,
    pub color: ColorTheme,
    pub language: LanguageCode,
}

impl LarkTemplate for SimpleMessageTemplate {
    fn generate(&self) -> CardContent {
        let hdr: Value = HeaderBlock {
            title: self.title.clone(),
            template: self.color.as_str().into(),
            ..Default::default()
        }
        .into();
        Card {
            elements: vec![blocks::markdown(&self.content).into()],
            header: hdr,
            ..Default::default()
        }
        .into()
    }
}

/// Alert notification card. Header color and icon are driven by [`SeverityLevel`].
///
/// Use the [`send_alert`](crate::send_alert) convenience function to send one
/// without instantiating this struct directly.
pub struct AlertTemplate {
    pub title: String,
    pub message: String,
    pub severity: SeverityLevel,
    /// Timestamp string to display in the card body (caller-formatted).
    pub timestamp: String,
    pub language: LanguageCode,
}

impl AlertTemplate {
    fn t(&self, key: &str) -> String {
        get_translation(key, self.language)
    }
}

impl LarkTemplate for AlertTemplate {
    fn generate(&self) -> CardContent {
        let color = self.severity.color();
        let icon = self.severity.icon();
        let severity_str = self.severity.as_str().to_uppercase();
        let body_text = format!(
            "{icon} **{}**\n\n**{}:** {}",
            self.message,
            self.t("timestamp"),
            self.timestamp,
        );
        let hdr: Value = HeaderBlock {
            title: self.title.clone(),
            template: color.into(),
            subtitle: Some(severity_str.clone()),
            text_tag_list: Some(vec![
                TextTag {
                    text: severity_str,
                    color: color.into(),
                }
                .into(),
            ]),
            ..Default::default()
        }
        .into();
        Card {
            elements: vec![blocks::markdown(&body_text).into()],
            header: hdr,
            ..Default::default()
        }
        .into()
    }
}

/// Pass-through template that wraps a pre-built [`CardContent`] value.
///
/// Useful when you have a `serde_json::Value` from an external source and want
/// to send it through [`LarkWebhookNotifier::send_template`].
pub struct RawContentTemplate {
    pub content: CardContent,
    pub language: LanguageCode,
}

impl LarkTemplate for RawContentTemplate {
    fn generate(&self) -> CardContent {
        self.content.clone()
    }
}

/// Output type of [`CardBuilder::build`] and all workflow functions.
///
/// Implements [`LarkTemplate`]; pass directly to [`LarkWebhookNotifier::send_template`].
pub struct GenericCardTemplate {
    pub(crate) content: CardContent,
}

impl LarkTemplate for GenericCardTemplate {
    fn generate(&self) -> CardContent {
        self.content.clone()
    }
}

pub(crate) fn storage_columns(
    group_label: &str,
    group_value: &str,
    prefix_label: &str,
    prefix_value: &str,
) -> Value {
    let col1: Value = Column {
        elements: vec![
            Markdown {
                content: format!("**{group_label}**\n{group_value}"),
                text_align: TextAlign::Center,
                text_size: TextSize::NormalV2,
                margin: "0px 4px 0px 4px".into(),
            }
            .into(),
        ],
        width: ColumnWidth::Auto,
        ..Default::default()
    }
    .into();
    let col2: Value = Column {
        elements: vec![
            Markdown {
                content: format!("**{prefix_label}**\n{prefix_value}"),
                text_align: TextAlign::Center,
                text_size: TextSize::NormalV2,
                ..Default::default()
            }
            .into(),
        ],
        width: ColumnWidth::Weighted,
        weight: Some(1),
        ..Default::default()
    }
    .into();
    ColumnSet {
        columns: vec![col1, col2],
        ..Default::default()
    }
    .into()
}

pub(crate) fn overview_panel(title: &str, content: &str) -> Value {
    CollapsiblePanel {
        title_markdown: format!("**<font color='grey-800'>{title}</font>**"),
        elements: vec![
            Markdown {
                content: content.into(),
                text_size: TextSize::NormalV2,
                ..Default::default()
            }
            .into(),
        ],
        expanded: false,
        ..Default::default()
    }
    .into()
}

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

    #[test]
    fn test_translation_zh() {
        assert_eq!(get_translation("task_name", LanguageCode::Zh), "作业名称");
        assert_eq!(get_translation("running", LanguageCode::Zh), "正在运行");
    }

    #[test]
    fn test_translation_en() {
        assert_eq!(get_translation("task_name", LanguageCode::En), "Job Name");
        assert_eq!(get_translation("running", LanguageCode::En), "Running Now");
    }

    #[test]
    fn test_translation_fallback() {
        let result = get_translation("nonexistent_key_xyz", LanguageCode::Zh);
        assert_eq!(result, "nonexistent_key_xyz");
    }

    #[test]
    fn test_simple_message_generate() {
        let t = SimpleMessageTemplate {
            title: "Hello".to_owned(),
            content: "World".to_owned(),
            color: ColorTheme::Blue,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        assert_eq!(card["schema"], "2.0");
        assert_eq!(card["header"]["title"]["content"], "Hello");
        assert_eq!(card["header"]["template"], "blue");
        assert_eq!(card["body"]["elements"][0]["content"], "World");
        assert!(card.get("config").is_none());
    }

    #[test]
    fn test_alert_template_warning() {
        let t = AlertTemplate {
            title: "Alert!".to_owned(),
            message: "High CPU".to_owned(),
            severity: SeverityLevel::Warning,
            timestamp: "2026-01-01 00:00:00".to_owned(),
            language: LanguageCode::En,
        };
        let card = t.generate();
        assert_eq!(card["header"]["template"], "orange");
        let body_content = card["body"]["elements"][0]["content"].as_str().unwrap();
        assert!(body_content.contains("High CPU"));
    }

    #[test]
    fn test_raw_content_passthrough() {
        let content = serde_json::json!({"schema": "2.0", "custom": true});
        let t = RawContentTemplate {
            content: content.clone(),
            language: LanguageCode::Zh,
        };
        assert_eq!(t.generate(), content);
    }

    #[test]
    fn test_legacy_task_template() {
        let t = LegacyTaskTemplate {
            task_name: "my-task".to_owned(),
            status: Some(0),
            group: "g1".to_owned(),
            prefix: "p/".to_owned(),
            task_summary: "summary".to_owned(),
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        assert_eq!(card["type"], "template");
        assert_eq!(card["data"]["template_id"], "AAqz08XD5HCzP");
        assert_eq!(card["data"]["template_variable"]["task_name"], "my-task");
        assert!(
            card["data"]["template_variable"]["task_status"]
                .as_str()
                .unwrap()
                .contains("CheckMark")
        );
    }

    #[test]
    fn test_start_task_template_structure() {
        let t = StartTaskTemplate {
            task_name: "build".to_owned(),
            desc: None,
            group: None,
            prefix: None,
            msg: None,
            estimated_duration: None,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        assert_eq!(card["schema"], "2.0");
        assert_eq!(card["header"]["template"], "wathet");
        assert!(card.get("config").is_some());
    }

    #[test]
    fn test_report_task_result_green() {
        let t = ReportTaskResultTemplate {
            task_name: "build".to_owned(),
            status: 0,
            group: None,
            prefix: None,
            desc: None,
            msg: None,
            duration: None,
            title: None,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        assert_eq!(card["header"]["template"], "green");
    }

    #[test]
    fn test_report_failure_task_red() {
        let t = ReportFailureTaskTemplate {
            task_name: "build".to_owned(),
            status: 1,
            group: None,
            prefix: None,
            desc: None,
            msg: None,
            duration: None,
            title: None,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        assert_eq!(card["header"]["template"], "red");
    }

    #[test]
    fn test_report_failure_body_contains_status_code() {
        let t = ReportFailureTaskTemplate {
            task_name: "myjob".to_owned(),
            status: 42,
            group: None,
            prefix: None,
            desc: None,
            msg: None,
            duration: None,
            title: None,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        let body_text = card["body"]["elements"][0]["content"].as_str().unwrap();
        assert!(
            body_text.contains("42"),
            "body should contain the return code"
        );
        assert!(body_text.contains("myjob"), "body should contain task name");
    }

    #[test]
    fn test_start_task_template_body_element_count_no_group() {
        let t = StartTaskTemplate {
            task_name: "t".to_owned(),
            desc: None,
            group: None,
            prefix: None,
            msg: None,
            estimated_duration: None,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        let elements = card["body"]["elements"].as_array().unwrap();
        assert_eq!(
            elements.len(),
            1,
            "only markdown block when no group/prefix/msg"
        );
    }

    #[test]
    fn test_start_task_template_with_group_prefix_has_column_set() {
        let t = StartTaskTemplate {
            task_name: "t".to_owned(),
            desc: None,
            group: Some("grp".to_owned()),
            prefix: Some("pfx/".to_owned()),
            msg: None,
            estimated_duration: None,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        let elements = card["body"]["elements"].as_array().unwrap();
        assert_eq!(
            elements.len(),
            2,
            "markdown + column_set when group/prefix set"
        );
        assert_eq!(elements[1]["tag"], "column_set");
    }

    #[test]
    fn test_start_task_template_with_group_prefix_and_msg() {
        let t = StartTaskTemplate {
            task_name: "t".to_owned(),
            desc: None,
            group: Some("grp".to_owned()),
            prefix: Some("pfx/".to_owned()),
            msg: Some("running details".to_owned()),
            estimated_duration: None,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        let elements = card["body"]["elements"].as_array().unwrap();
        assert_eq!(elements.len(), 3, "markdown + column_set + collapsible");
        assert_eq!(elements[2]["tag"], "collapsible_panel");
    }

    #[test]
    fn test_language_switching_body_text() {
        let zh_t = SimpleMessageTemplate {
            title: "T".to_owned(),
            content: "C".to_owned(),
            color: ColorTheme::Blue,
            language: LanguageCode::Zh,
        };
        let en_t = SimpleMessageTemplate {
            title: "T".to_owned(),
            content: "C".to_owned(),
            color: ColorTheme::Blue,
            language: LanguageCode::En,
        };
        // body content is the same (caller-supplied), but header title translations differ
        let zh_card = zh_t.generate();
        let en_card = en_t.generate();
        assert_eq!(zh_card["header"]["title"]["content"], "T");
        assert_eq!(en_card["header"]["title"]["content"], "T");
        // schema and structure identical
        assert_eq!(zh_card["schema"], en_card["schema"]);
    }

    #[test]
    fn test_start_task_language_switching_zh_en() {
        let make = |lang| StartTaskTemplate {
            task_name: "job".to_owned(),
            desc: None,
            group: None,
            prefix: None,
            msg: None,
            estimated_duration: None,
            language: lang,
        };
        let zh_body = make(LanguageCode::Zh).generate();
        let en_body = make(LanguageCode::En).generate();
        let zh_text = zh_body["body"]["elements"][0]["content"].as_str().unwrap();
        let en_text = en_body["body"]["elements"][0]["content"].as_str().unwrap();
        // zh and en produce different label text in the body
        assert_ne!(zh_text, en_text, "zh and en body text should differ");
        assert!(zh_text.contains("job"), "both should contain the task name");
        assert!(en_text.contains("job"));
    }

    #[test]
    fn test_report_task_result_body_contains_task_name() {
        let t = ReportTaskResultTemplate {
            task_name: "myjob".to_owned(),
            status: 0,
            group: Some("g".to_owned()),
            prefix: Some("p/".to_owned()),
            desc: None,
            msg: None,
            duration: None,
            title: None,
            language: LanguageCode::Zh,
        };
        let card = t.generate();
        let elements = card["body"]["elements"].as_array().unwrap();
        assert_eq!(
            elements.len(),
            2,
            "markdown + column_set when group/prefix set"
        );
        let body_text = elements[0]["content"].as_str().unwrap();
        assert!(body_text.contains("myjob"));
        assert_eq!(elements[1]["tag"], "column_set");
    }

    #[test]
    fn test_generic_card_template_passthrough() {
        use crate::builder::CardBuilder;
        let card_val = CardBuilder::new()
            .header("My Card", None, None, None)
            .markdown(
                "hello",
                crate::blocks::TextAlign::Left,
                crate::blocks::TextSize::Normal,
            )
            .build()
            .generate();
        assert_eq!(card_val["schema"], "2.0");
        assert_eq!(card_val["header"]["title"]["content"], "My Card");
        assert_eq!(card_val["body"]["elements"][0]["content"], "hello");
    }
}