gouqi 0.20.0

Rust interface for Jira
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
// Third party

use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::collections::BTreeMap;
use time::{OffsetDateTime, format_description::well_known::Iso8601};
use tracing::error;

// Ours
use crate::{Jira, Result};
// Forward reference - Component is defined later in this file but used by Project

/// Custom serde module for JIRA datetime format
/// JIRA requires dates in format: "2024-01-01T09:00:00.000+0000"
/// This differs from standard ISO8601 in three ways:
/// 1. No year sign prefix (2024 not +002024)
/// 2. Milliseconds not nanoseconds (.000 not .000000000)
/// 3. Timezone as +0000 not Z
pub(crate) mod jira_datetime {
    use serde::{Deserialize, Deserializer, Serializer};
    use time::OffsetDateTime;

    /// Serializes OffsetDateTime to JIRA's expected format
    pub fn serialize<S>(dt: &Option<OffsetDateTime>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match dt {
            Some(dt) => {
                // Format as: "2024-01-01T09:00:00.000+0000"
                let format = time::format_description::parse(
                    "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3][offset_hour sign:mandatory][offset_minute]"
                ).map_err(serde::ser::Error::custom)?;

                let formatted = dt.format(&format).map_err(serde::ser::Error::custom)?;
                serializer.serialize_str(&formatted)
            }
            None => serializer.serialize_none(),
        }
    }

    /// Deserializes from JIRA datetime format or standard ISO8601
    #[allow(dead_code)]
    pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<OffsetDateTime>, D::Error>
    where
        D: Deserializer<'de>,
    {
        Option::<String>::deserialize(deserializer)?
            .map(|s| {
                // Try JIRA format first, then fall back to standard ISO8601
                let format = time::format_description::parse(
                    "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3][offset_hour sign:mandatory][offset_minute]"
                ).map_err(|e| serde::de::Error::custom(format!("Format parse error: {}", e)))?;

                OffsetDateTime::parse(&s, &format)
                    .or_else(|_| OffsetDateTime::parse(&s, &time::format_description::well_known::Iso8601::DEFAULT))
                    .map_err(|e| serde::de::Error::custom(format!("Date parse error: {}", e)))
            })
            .transpose()
    }
}

/// Represents an general jira error response
#[derive(Serialize, Deserialize, Debug)]
pub struct Errors {
    #[serde(rename = "errorMessages", default)]
    pub error_messages: Vec<String>,
    #[serde(default)]
    pub errors: BTreeMap<String, String>,
    // Support for V3 API error format
    #[serde(default)]
    pub error: Option<String>,
}

/// Represents a single jira issue
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Issue {
    #[serde(rename = "self")]
    pub self_link: String,
    pub key: String,
    pub id: String,
    pub fields: BTreeMap<String, ::serde_json::Value>,
}

impl Issue {
    /// Resolves a typed field from an issues lists of arbitrary fields
    pub fn field<F>(&self, name: &str) -> Option<Result<F>>
    where
        for<'de> F: Deserialize<'de>,
    {
        self.fields
            .get(name)
            .map(|value| Ok(serde_json::value::from_value::<F>(value.clone())?))
    }

    fn user_field(&self, name: &str) -> Option<Result<User>> {
        self.field::<User>(name)
    }

    fn string_field(&self, name: &str) -> Option<Result<String>> {
        self.field::<String>(name)
    }

    /// User assigned to issue
    pub fn assignee(&self) -> Option<User> {
        self.user_field("assignee").and_then(|value| value.ok())
    }

    /// User that created the issue
    pub fn creator(&self) -> Option<User> {
        self.user_field("creator").and_then(|value| value.ok())
    }

    /// User that reported the issue
    pub fn reporter(&self) -> Option<User> {
        self.user_field("reporter").and_then(|value| value.ok())
    }

    /// The current status of the issue
    pub fn status(&self) -> Option<Status> {
        self.field::<Status>("status").and_then(|value| value.ok())
    }

    /// Brief summary of the issue
    pub fn summary(&self) -> Option<String> {
        self.string_field("summary").and_then(|value| value.ok())
    }

    /// Description of the issue
    ///
    /// Supports both legacy string format (JIRA v2) and Atlassian Document Format (JIRA v3).
    /// For ADF format, converts the structured document to plain text.
    pub fn description(&self) -> Option<String> {
        // First try to get as string (legacy v2 API format)
        if let Some(Ok(desc)) = self.string_field("description") {
            return Some(desc);
        }

        // If that fails, try to parse as ADF (v3 API format)
        if let Some(Ok(adf)) = self.field::<AdfDocument>("description") {
            let plain_text = adf.to_plain_text();
            return if plain_text.is_empty() {
                None
            } else {
                Some(plain_text)
            };
        }

        None
    }

    /// Environment information for the issue
    ///
    /// Supports both legacy string format (JIRA v2) and Atlassian Document Format (JIRA v3).
    /// For ADF format, converts the structured document to plain text.
    pub fn environment(&self) -> Option<String> {
        // First try to get as string (legacy v2 API format)
        if let Some(Ok(env)) = self.string_field("environment") {
            return Some(env);
        }

        // If that fails, try to parse as ADF (v3 API format)
        if let Some(Ok(adf)) = self.field::<AdfDocument>("environment") {
            let plain_text = adf.to_plain_text();
            return if plain_text.is_empty() {
                None
            } else {
                Some(plain_text)
            };
        }

        None
    }

    fn extract_offset_date_time(&self, field: &str) -> Option<OffsetDateTime> {
        match self.string_field(field) {
            Some(Ok(created)) => match OffsetDateTime::parse(created.as_ref(), &Iso8601::DEFAULT) {
                Ok(offset_date_time) => Some(offset_date_time),
                Err(error) => {
                    error!(
                        "Can't convert '{} = {:?}' into a OffsetDateTime. {:?}",
                        field, created, error
                    );
                    None
                }
            },
            _ => None,
        }
    }

    /// Updated timestamp
    pub fn updated(&self) -> Option<OffsetDateTime> {
        self.extract_offset_date_time("updated")
    }

    /// Created timestamp
    pub fn created(&self) -> Option<OffsetDateTime> {
        self.extract_offset_date_time("created")
    }

    pub fn resolution_date(&self) -> Option<OffsetDateTime> {
        self.extract_offset_date_time("resolutiondate")
    }

    /// An issue type
    pub fn issue_type(&self) -> Option<IssueType> {
        self.field::<IssueType>("issuetype")
            .and_then(|value| value.ok())
    }

    /// Labels associated with the issue
    pub fn labels(&self) -> Vec<String> {
        self.field::<Vec<String>>("labels")
            .and_then(|value| value.ok())
            .unwrap_or_default()
    }

    /// List of versions associated with the issue
    pub fn fix_versions(&self) -> Vec<Version> {
        self.field::<Vec<Version>>("fixVersions")
            .and_then(|value| value.ok())
            .unwrap_or_default()
    }

    /// Priority of the issue
    pub fn priority(&self) -> Option<Priority> {
        self.field::<Priority>("priority")
            .and_then(|value| value.ok())
    }

    /// Links to other issues
    pub fn links(&self) -> Option<Result<Vec<IssueLink>>> {
        self.field::<Vec<IssueLink>>("issuelinks") //.and_then(|value| value.ok()).unwrap_or(vec![])
    }

    pub fn project(&self) -> Option<Project> {
        self.field::<Project>("project")
            .and_then(|value| value.ok())
    }

    pub fn resolution(&self) -> Option<Resolution> {
        self.field::<Resolution>("resolution")
            .and_then(|value| value.ok())
    }

    pub fn attachment(&self) -> Vec<Attachment> {
        self.field::<Vec<Attachment>>("attachment")
            .and_then(|value| value.ok())
            .unwrap_or_default()
    }

    pub fn comments(&self) -> Option<Comments> {
        self.field::<Comments>("comment")
            .and_then(|value| value.ok())
    }

    pub fn parent(&self) -> Option<Issue> {
        self.field::<Issue>("parent").and_then(|value| value.ok())
    }

    pub fn timetracking(&self) -> Option<TimeTracking> {
        self.field::<TimeTracking>("timetracking")
            .and_then(|value| value.ok())
    }

    /// Returns a permanent link to the issue in the Jira web interface
    ///
    /// # Panics
    ///
    /// This function will panic if:
    /// - The host URL cannot be joined with the browse path
    /// - The issue key cannot be added to the URL path
    pub fn permalink(&self, jira: &Jira) -> String {
        //format!("{}/browse/{}", jira.host, self.key)
        jira.host()
            .join("/browse/")
            .unwrap()
            .join(&self.key)
            .unwrap()
            .to_string()
    }

    pub fn try_from_custom_issue<S: Serialize>(custom_issue: &S) -> serde_json::Result<Self> {
        let serialized_data = serde_json::to_string(custom_issue)?;
        serde_json::from_str(&serialized_data)
    }

    pub fn try_to_custom_issue<D: DeserializeOwned>(&self) -> serde_json::Result<D> {
        let serialized_data = serde_json::to_string(self)?;
        serde_json::from_str(&serialized_data)
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Attachment {
    pub id: String,
    #[serde(rename = "self")]
    pub self_link: String,
    pub filename: String,
    pub author: User,
    pub created: String,
    pub size: u64,
    #[serde(rename = "mimeType")]
    pub mime_type: String,
    pub content: String,
    pub thumbnail: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Comments {
    pub comments: Vec<Comment>,
    #[serde(rename = "self")]
    pub self_link: String,
    #[serde(rename = "maxResults")]
    pub max_results: u32,
    pub total: u32,
    #[serde(rename = "startAt")]
    pub start_at: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Comment {
    pub id: Option<String>,
    #[serde(rename = "self")]
    pub self_link: String,
    pub author: Option<User>,
    #[serde(rename = "updateAuthor")]
    pub update_author: Option<User>,
    #[serde(default, with = "time::serde::iso8601::option")]
    pub created: Option<OffsetDateTime>,
    #[serde(default, with = "time::serde::iso8601::option")]
    pub updated: Option<OffsetDateTime>,
    pub body: TextContent,
    pub visibility: Option<Visibility>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Visibility {
    #[serde(rename = "type")]
    pub visibility_type: String,
    pub value: String,
}

/// Text content that supports both plain text (JIRA v2) and ADF format (JIRA v3)
///
/// This type provides backward compatibility by accepting both String and ADF JSON
/// during deserialization, while providing string-like access patterns through Deref.
///
/// # Examples
///
/// ```
/// # use gouqi::TextContent;
/// // Acts like a string reference
/// let text = TextContent::from("Hello");
/// assert_eq!(text.len(), 5);
/// assert!(text.contains("Hello"));
/// ```
#[derive(Clone, Debug)]
pub struct TextContent {
    /// Raw JSON value (either String or ADF document)
    raw: serde_json::Value,
    /// Cached extracted text for efficient access
    cached: String,
}

impl TextContent {
    /// Create TextContent from a plain string
    pub fn from_string(s: impl Into<String>) -> Self {
        let text = s.into();
        Self {
            raw: serde_json::Value::String(text.clone()),
            cached: text,
        }
    }

    /// Get the raw JSON value
    pub fn raw(&self) -> &serde_json::Value {
        &self.raw
    }

    /// Extract plain text from a JSON value (String or ADF)
    fn extract_text(value: &serde_json::Value) -> String {
        // First try to get as string (legacy v2 API format)
        if let Ok(text) = serde_json::from_value::<String>(value.clone()) {
            return text;
        }

        // If that fails, try to parse as ADF (v3 API format)
        if let Ok(adf) = serde_json::from_value::<AdfDocument>(value.clone()) {
            return adf.to_plain_text();
        }

        // Fallback: empty string
        String::new()
    }
}

impl std::ops::Deref for TextContent {
    type Target = str;

    fn deref(&self) -> &str {
        &self.cached
    }
}

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

impl AsRef<str> for TextContent {
    fn as_ref(&self) -> &str {
        &self.cached
    }
}

impl std::borrow::Borrow<str> for TextContent {
    fn borrow(&self) -> &str {
        &self.cached
    }
}

impl PartialEq<str> for TextContent {
    fn eq(&self, other: &str) -> bool {
        self.cached == other
    }
}

impl PartialEq<&str> for TextContent {
    fn eq(&self, other: &&str) -> bool {
        self.cached == *other
    }
}

impl PartialEq<String> for TextContent {
    fn eq(&self, other: &String) -> bool {
        &self.cached == other
    }
}

impl PartialEq for TextContent {
    fn eq(&self, other: &Self) -> bool {
        self.cached == other.cached
    }
}

impl Eq for TextContent {}

impl From<String> for TextContent {
    fn from(s: String) -> Self {
        Self::from_string(s)
    }
}

impl From<&str> for TextContent {
    fn from(s: &str) -> Self {
        Self::from_string(s)
    }
}

// Custom serialization: always serialize the raw value
impl Serialize for TextContent {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.raw.serialize(serializer)
    }
}

// Custom deserialization: accept both String and ADF, eagerly extract text
impl<'de> Deserialize<'de> for TextContent {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = serde_json::Value::deserialize(deserializer)?;
        let cached = Self::extract_text(&raw);
        Ok(Self { raw, cached })
    }
}

/// Atlassian Document Format (ADF) structures for V3 API comments
/// See: https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/
/// ADF text node - inline content with optional formatting
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AdfText {
    #[serde(rename = "type")]
    pub node_type: String, // "text"
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub marks: Option<Vec<AdfMark>>,
}

impl AdfText {
    /// Create a plain text node
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            node_type: "text".to_string(),
            text: text.into(),
            marks: None,
        }
    }
}

/// ADF text formatting marks (bold, italic, etc.)
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AdfMark {
    #[serde(rename = "type")]
    pub mark_type: String, // "strong", "em", "code", etc.
}

/// ADF block node - can be paragraph, heading, list, etc.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AdfNode {
    #[serde(rename = "type")]
    pub node_type: String, // "paragraph", "heading", etc.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<Vec<AdfContent>>,
}

impl AdfNode {
    /// Create a paragraph node containing text
    pub fn paragraph(content: Vec<AdfText>) -> Self {
        Self {
            node_type: "paragraph".to_string(),
            content: Some(content.into_iter().map(AdfContent::Text).collect()),
        }
    }
}

/// ADF content - can be either inline text or nested nodes
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum AdfContent {
    Text(AdfText),
    Node(Box<AdfNode>),
}

/// ADF document root structure for V3 API
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AdfDocument {
    pub version: u32,
    #[serde(rename = "type")]
    pub doc_type: String, // "doc"
    pub content: Vec<AdfNode>,
}

impl AdfDocument {
    /// Create an ADF document from plain text
    /// Splits text by newlines and creates a paragraph for each line
    pub fn from_text(text: impl Into<String>) -> Self {
        let text = text.into();
        let content = if text.is_empty() {
            // Empty document needs at least one empty paragraph
            vec![AdfNode::paragraph(vec![])]
        } else {
            // Split by newlines and create a paragraph for each non-empty line
            text.lines()
                .map(|line| {
                    if line.is_empty() {
                        AdfNode::paragraph(vec![])
                    } else {
                        AdfNode::paragraph(vec![AdfText::new(line)])
                    }
                })
                .collect()
        };

        Self {
            version: 1,
            doc_type: "doc".to_string(),
            content,
        }
    }

    /// Extract plain text from an ADF document
    /// Converts the structured document back to plain text, joining paragraphs with newlines
    pub fn to_plain_text(&self) -> String {
        self.content
            .iter()
            .filter_map(Self::extract_text_from_node)
            .collect::<Vec<String>>()
            .join("\n")
    }

    /// Recursively extract text from an ADF node
    fn extract_text_from_node(node: &AdfNode) -> Option<String> {
        if let Some(content) = &node.content {
            let text: Vec<String> = content
                .iter()
                .filter_map(|item| match item {
                    AdfContent::Text(text_node) => Some(text_node.text.clone()),
                    AdfContent::Node(nested_node) => Self::extract_text_from_node(nested_node),
                })
                .collect();

            if text.is_empty() {
                None
            } else {
                Some(text.join(""))
            }
        } else {
            None
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Changelog {
    #[serde(rename = "values")]
    pub histories: Vec<History>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct History {
    pub author: User,
    pub created: String,
    pub items: Vec<HistoryItem>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HistoryItem {
    pub field: String,
    pub from: Option<String>,
    #[serde(rename = "fromString")]
    pub from_string: Option<String>,
    pub to: Option<String>,
    #[serde(rename = "toString")]
    pub to_string: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Project {
    #[serde(rename = "self")]
    pub self_link: String,
    pub id: String,
    pub key: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    pub project_type_key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lead: Option<User>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub components: Option<Vec<ProjectComponent>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub versions: Option<Vec<Version>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub roles: Option<BTreeMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_urls: Option<BTreeMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_category: Option<ProjectCategory>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issue_types: Option<Vec<IssueType>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectComponent {
    pub id: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(rename = "self", skip_serializing_if = "Option::is_none")]
    pub self_link: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CreateProject {
    pub key: String,
    pub name: String,
    pub project_type_key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lead: Option<String>, // username or account ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issue_security_scheme: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permission_scheme: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notification_scheme: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category_id: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct UpdateProject {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lead: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category_id: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectCategory {
    #[serde(rename = "self")]
    pub self_link: String,
    pub id: String,
    pub name: String,
    pub description: String,
}

#[derive(Serialize, Debug, Default)]
pub struct ProjectSearchOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_at: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_results: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order_by: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_type_key: Option<String>,
}

impl ProjectSearchOptions {
    /// Serialize the search options to query parameters
    pub fn serialize(&self) -> Result<Vec<(String, String)>> {
        let mut params = Vec::new();

        if let Some(ref query) = self.query {
            params.push(("query".to_string(), query.clone()));
        }
        if let Some(start_at) = self.start_at {
            params.push(("startAt".to_string(), start_at.to_string()));
        }
        if let Some(max_results) = self.max_results {
            params.push(("maxResults".to_string(), max_results.to_string()));
        }
        if let Some(ref order_by) = self.order_by {
            params.push(("orderBy".to_string(), order_by.clone()));
        }
        if let Some(category_id) = self.category_id {
            params.push(("categoryId".to_string(), category_id.to_string()));
        }
        if let Some(ref project_type_key) = self.project_type_key {
            params.push(("projectTypeKey".to_string(), project_type_key.clone()));
        }

        Ok(params)
    }
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ProjectSearchResults {
    pub start_at: u64,
    pub max_results: u64,
    pub total: u64,
    pub values: Vec<Project>,
}

#[derive(Deserialize, Debug)]
pub struct ProjectRole {
    #[serde(rename = "self")]
    pub self_link: String,
    pub name: String,
    pub id: u64,
    pub description: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actors: Option<Vec<RoleActor>>,
}

#[derive(Deserialize, Debug)]
pub struct RoleActor {
    pub id: u64,
    #[serde(rename = "displayName")]
    pub display_name: String,
    #[serde(rename = "type")]
    pub actor_type: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_url: Option<String>,
}

/// Represents link relationship between issues
#[derive(Serialize, Deserialize, Debug)]
pub struct IssueLink {
    pub id: String,
    #[serde(rename = "self")]
    pub self_link: String,
    #[serde(rename = "outwardIssue")]
    pub outward_issue: Option<Issue>,
    #[serde(rename = "inwardIssue")]
    pub inward_issue: Option<Issue>,
    #[serde(rename = "type")]
    pub link_type: LinkType,
}

/// Represents type of issue relation
#[derive(Serialize, Deserialize, Debug)]
pub struct LinkType {
    pub id: String,
    pub inward: String,
    pub name: String,
    pub outward: String,
    #[serde(rename = "self")]
    pub self_link: String,
}

/// Request to create an issue link
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CreateIssueLinkInput {
    #[serde(rename = "type")]
    pub link_type: IssueLinkType,
    pub inward_issue: IssueKey,
    pub outward_issue: IssueKey,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<LinkComment>,
}

/// Simple issue link type reference for creating links
#[derive(Serialize, Debug)]
pub struct IssueLinkType {
    pub name: String,
}

/// Simple issue key reference for creating links
#[derive(Serialize, Debug)]
pub struct IssueKey {
    pub key: String,
}

/// Optional comment when creating an issue link
///
/// Automatically converts plain text to ADF format for v3 API compatibility
#[derive(Debug)]
pub struct LinkComment {
    body: String,
}

impl LinkComment {
    /// Create a new link comment
    pub fn new(body: impl Into<String>) -> Self {
        Self { body: body.into() }
    }
}

impl Serialize for LinkComment {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;

        // Convert plain text to ADF format for v3 API
        let adf = AdfDocument::from_text(&self.body);

        let mut state = serializer.serialize_struct("LinkComment", 1)?;
        state.serialize_field("body", &adf)?;
        state.end()
    }
}

impl CreateIssueLinkInput {
    /// Create a new issue link
    pub fn new(
        link_type: impl Into<String>,
        inward: impl Into<String>,
        outward: impl Into<String>,
    ) -> Self {
        Self {
            link_type: IssueLinkType {
                name: link_type.into(),
            },
            inward_issue: IssueKey { key: inward.into() },
            outward_issue: IssueKey {
                key: outward.into(),
            },
            comment: None,
        }
    }

    /// Add a comment to the issue link
    pub fn with_comment(mut self, comment: impl Into<String>) -> Self {
        self.comment = Some(LinkComment::new(comment));
        self
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Version {
    pub archived: bool,
    pub id: String,
    pub name: String,
    #[serde(rename = "projectId")]
    pub project_id: u64,
    pub released: bool,
    #[serde(rename = "self")]
    pub self_link: String,
}

#[derive(Serialize, Debug)]
pub struct VersionCreationBody {
    pub name: String,
    #[serde(rename = "projectId")]
    pub project_id: u64,
}

#[derive(Serialize, Debug)]
pub struct VersionMoveAfterBody {
    pub after: String,
}

#[derive(Serialize, Debug)]
pub struct VersionUpdateBody {
    pub released: bool,
    pub archived: bool,
    #[serde(rename = "moveUnfixedIssuesTo")]
    pub move_unfixed_issues_to: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
    #[serde(rename = "accountId")]
    pub account_id: Option<String>,
    pub active: bool,
    #[serde(rename = "avatarUrls")]
    pub avatar_urls: Option<BTreeMap<String, String>>,
    #[serde(rename = "displayName")]
    pub display_name: String,
    #[serde(rename = "emailAddress")]
    pub email_address: Option<String>,
    pub key: Option<String>,
    pub name: Option<String>,
    #[serde(rename = "self")]
    pub self_link: String,
    #[serde(rename = "timeZone")]
    pub timezone: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Status {
    pub description: String,
    #[serde(rename = "iconUrl")]
    pub icon_url: String,
    pub id: String,
    pub name: String,
    #[serde(rename = "self")]
    pub self_link: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Priority {
    #[serde(rename = "iconUrl")]
    pub icon_url: String,
    pub id: String,
    pub name: String,
    #[serde(rename = "self")]
    pub self_link: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IssueType {
    pub description: String,
    #[serde(rename = "iconUrl")]
    pub icon_url: String,
    pub id: String,
    pub name: String,
    #[serde(rename = "self")]
    pub self_link: String,
    pub subtask: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SearchResults {
    /// Total number of issues. Note: V3 API doesn't provide this, so it may be estimated
    pub total: u64,
    #[serde(rename = "maxResults")]
    pub max_results: u64,
    #[serde(rename = "startAt")]
    pub start_at: u64,
    pub expand: Option<String>,
    pub issues: Vec<Issue>,
    /// V3 API specific: Indicates if this is the last page of results
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_last_page: Option<bool>,
    /// V3 API specific: Token for fetching the next page
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
    /// Indicates if the total count is accurate (false for V3 API)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_is_accurate: Option<bool>,
}

/// V3 Search Results format for the new /rest/api/3/search/jql endpoint
#[derive(Serialize, Deserialize, Debug)]
pub struct V3SearchResults {
    pub issues: Vec<Issue>,
    #[serde(rename = "isLast")]
    pub is_last: bool,
    #[serde(rename = "nextPageToken")]
    pub next_page_token: Option<String>,
}

impl V3SearchResults {
    /// Convert V3SearchResults to legacy SearchResults format for backward compatibility
    pub fn to_search_results(self, start_at: u64, max_results: u64) -> SearchResults {
        // V3 API doesn't provide total count. We provide a best-effort estimate
        // but mark it as inaccurate. This maintains backward compatibility while
        // being honest about the limitation.
        let total = if self.is_last {
            // If this is the last page, we know the exact total
            start_at + self.issues.len() as u64
        } else {
            // If not last page, use a high estimate to indicate more pages exist
            // Using u64::MAX would break existing code, so we estimate conservatively
            // Assume at least one more full page exists
            start_at + self.issues.len() as u64 + max_results
        };

        SearchResults {
            total,
            max_results,
            start_at,
            expand: None,
            issues: self.issues,
            is_last_page: Some(self.is_last),
            next_page_token: self.next_page_token,
            total_is_accurate: Some(false), // V3 never provides accurate totals
        }
    }
}

/// Response from V3 approximate-count endpoint
///
/// The API returns: `{"count": 123}`
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct V3ApproximateCountResponse {
    pub count: u64,
}

/// Result of counting issues matching a JQL query
///
/// The `is_exact` field indicates whether the count is accurate:
/// - `true`: Exact count (v2 API or v3 when exact method is available)
/// - `false`: Approximate count (v3 API approximate-count endpoint)
///
/// # Examples
///
/// ```no_run
/// # use gouqi::{Jira, Credentials};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let jira = Jira::new("https://jira.example.com", Credentials::Basic("user".into(), "pass".into()))?;
///
/// // Request exact count (will use best available method)
/// let result = jira.search().count("project = DEMO", true)?;
/// if result.is_exact {
///     println!("Exactly {} issues", result.count);
/// } else {
///     println!("Approximately {} issues", result.count);
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IssueCount {
    /// The number of issues found
    pub count: u64,
    /// Whether the count is exact (`true`) or approximate (`false`)
    ///
    /// This reflects the actual counting method used, which may differ from
    /// the user's preference if a requested method is unavailable.
    pub is_exact: bool,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TimeTracking {
    pub original_estimate: Option<String>,
    pub original_estimate_seconds: Option<u64>,
    pub remaining_estimate: Option<String>,
    pub remaining_estimate_seconds: Option<u64>,
    pub time_spent: Option<String>,
    pub time_spent_seconds: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TransitionOption {
    pub id: String,
    pub name: String,
    pub to: TransitionTo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TransitionTo {
    pub name: String,
    pub id: String,
}

/// Contains list of options an issue can transitions through
#[derive(Serialize, Deserialize, Debug)]
pub struct TransitionOptions {
    pub transitions: Vec<TransitionOption>,
}

#[derive(Serialize, Debug)]
pub struct TransitionTriggerOptions {
    pub transition: Transition,
    pub fields: BTreeMap<String, ::serde_json::Value>,
}

impl TransitionTriggerOptions {
    /// Creates a new instance
    pub fn new<I>(id: I) -> TransitionTriggerOptions
    where
        I: Into<String>,
    {
        TransitionTriggerOptions {
            transition: Transition { id: id.into() },
            fields: BTreeMap::new(),
        }
    }

    pub fn builder<I>(id: I) -> TransitionTriggerOptionsBuilder
    where
        I: Into<String>,
    {
        TransitionTriggerOptionsBuilder::new(id)
    }
}

pub struct TransitionTriggerOptionsBuilder {
    pub transition: Transition,
    pub fields: BTreeMap<String, ::serde_json::Value>,
}

impl TransitionTriggerOptionsBuilder {
    /// Creates a new instance
    pub fn new<I>(id: I) -> TransitionTriggerOptionsBuilder
    where
        I: Into<String>,
    {
        TransitionTriggerOptionsBuilder {
            transition: Transition { id: id.into() },
            fields: BTreeMap::new(),
        }
    }

    /// Appends a field to update as part of transition
    ///
    /// # Panics
    ///
    /// This function will panic if the provided value cannot be serialized to JSON.
    /// This should only happen in exceptional circumstances, such as when a custom type
    /// with a failing serialization implementation is provided.
    pub fn field<N, V>(&mut self, name: N, value: V) -> &mut TransitionTriggerOptionsBuilder
    where
        N: Into<String>,
        V: Serialize,
    {
        self.fields.insert(
            name.into(),
            serde_json::to_value(value).expect("Value to serialize"),
        );
        self
    }

    /// Updates resolution in transition
    pub fn resolution<R>(&mut self, name: R) -> &mut TransitionTriggerOptionsBuilder
    where
        R: Into<String>,
    {
        self.field("resolution", Resolution { name: name.into() });
        self
    }

    /// Adds a comment to the transition
    ///
    /// Automatically converts plain text to ADF format for v3 API compatibility
    pub fn comment<C>(&mut self, comment: C) -> &mut TransitionTriggerOptionsBuilder
    where
        C: Into<String>,
    {
        // Convert plain text to ADF format
        let adf = AdfDocument::from_text(comment.into());
        self.field("comment", serde_json::json!({"body": adf}));
        self
    }

    pub fn build(&self) -> TransitionTriggerOptions {
        TransitionTriggerOptions {
            transition: self.transition.clone(),
            fields: self.fields.clone(),
        }
    }
}

#[derive(Serialize, Debug, Deserialize)]
pub struct Resolution {
    name: String,
}

#[derive(Serialize, Clone, Debug)]
pub struct Transition {
    pub id: String,
}

/// Represents a worklog entry on an issue
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Worklog {
    #[serde(rename = "self")]
    pub self_link: String,
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<User>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub update_author: Option<User>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<TextContent>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "time::serde::iso8601::option"
    )]
    pub created: Option<OffsetDateTime>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "time::serde::iso8601::option"
    )]
    pub updated: Option<OffsetDateTime>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "time::serde::iso8601::option"
    )]
    pub started: Option<OffsetDateTime>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_spent: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_spent_seconds: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issue_id: Option<String>,
}

/// Request to create or update a worklog
///
/// **API Compatibility:**
/// - JIRA Cloud (v3): Requires comments in Atlassian Document Format (ADF)
/// - JIRA Server/Data Center (v2): Requires comments as plain strings
///
/// This type automatically adapts based on the detected JIRA deployment type.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WorklogInput {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", with = "jira_datetime")]
    pub started: Option<OffsetDateTime>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_spent_seconds: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_spent: Option<String>,
}

impl WorklogInput {
    /// Convert to JSON payload for JIRA Cloud v3 API (with ADF comment format)
    pub(crate) fn to_v3_json(&self) -> serde_json::Value {
        let mut obj = serde_json::Map::new();

        // Convert comment to ADF format (required for v3)
        let comment_text = self.comment.as_deref().unwrap_or("");
        let adf_comment = AdfDocument::from_text(comment_text);
        obj.insert(
            "comment".to_string(),
            serde_json::to_value(adf_comment).unwrap(),
        );

        if let Some(started) = self.started {
            let format = time::format_description::parse(
                "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3][offset_hour sign:mandatory][offset_minute]"
            ).unwrap();
            let formatted = started.format(&format).unwrap();
            obj.insert("started".to_string(), serde_json::Value::String(formatted));
        }

        if let Some(time_spent_seconds) = self.time_spent_seconds {
            obj.insert(
                "timeSpentSeconds".to_string(),
                serde_json::Value::Number(time_spent_seconds.into()),
            );
        }

        if let Some(ref time_spent) = self.time_spent {
            obj.insert(
                "timeSpent".to_string(),
                serde_json::Value::String(time_spent.clone()),
            );
        }

        serde_json::Value::Object(obj)
    }
}

impl WorklogInput {
    /// Create a new worklog with time spent in seconds
    pub fn new(time_spent_seconds: u64) -> Self {
        Self {
            comment: None,
            started: None,
            time_spent_seconds: Some(time_spent_seconds),
            time_spent: None,
        }
    }

    /// Create a new worklog with time spent in minutes
    pub fn from_minutes(minutes: u64) -> Self {
        Self::new(minutes * 60)
    }

    /// Create a new worklog with time spent in hours
    pub fn from_hours(hours: u64) -> Self {
        Self::new(hours * 3600)
    }

    /// Create a new worklog with time spent in days (8 hour workday)
    pub fn from_days(days: u64) -> Self {
        Self::new(days * 8 * 3600)
    }

    /// Create a new worklog with time spent in weeks (5 day workweek, 8 hour days)
    pub fn from_weeks(weeks: u64) -> Self {
        Self::new(weeks * 5 * 8 * 3600)
    }

    /// Set a comment for the worklog
    pub fn with_comment(mut self, comment: impl Into<String>) -> Self {
        self.comment = Some(comment.into());
        self
    }

    /// Get the comment text
    pub fn comment(&self) -> Option<&str> {
        self.comment.as_deref()
    }

    /// Set when the work was started
    pub fn with_started(mut self, started: OffsetDateTime) -> Self {
        self.started = Some(started);
        self
    }

    /// Set started time to N hours ago from now
    pub fn started_hours_ago(mut self, hours: i64) -> Self {
        let started = OffsetDateTime::now_utc() - time::Duration::hours(hours);
        self.started = Some(started);
        self
    }

    /// Set started time to N minutes ago from now
    pub fn started_minutes_ago(mut self, minutes: i64) -> Self {
        let started = OffsetDateTime::now_utc() - time::Duration::minutes(minutes);
        self.started = Some(started);
        self
    }

    /// Set started time to N days ago from now
    pub fn started_days_ago(mut self, days: i64) -> Self {
        let started = OffsetDateTime::now_utc() - time::Duration::days(days);
        self.started = Some(started);
        self
    }

    /// Set started time to N weeks ago from now
    pub fn started_weeks_ago(mut self, weeks: i64) -> Self {
        let started = OffsetDateTime::now_utc() - time::Duration::weeks(weeks);
        self.started = Some(started);
        self
    }

    /// Set started time to a specific date and time
    ///
    /// # Examples
    ///
    /// ```rust
    /// use gouqi::WorklogInput;
    /// use time::macros::datetime;
    ///
    /// let worklog = WorklogInput::from_hours(2)
    ///     .started_at(datetime!(2024-01-15 09:00:00 UTC));
    /// ```
    pub fn started_at(mut self, datetime: OffsetDateTime) -> Self {
        self.started = Some(datetime);
        self
    }

    /// Set time spent as a string (e.g., "3h 20m")
    pub fn with_time_spent(mut self, time_spent: impl Into<String>) -> Self {
        self.time_spent = Some(time_spent.into());
        self
    }
}

/// Response containing a list of worklogs
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct WorklogList {
    pub start_at: u64,
    pub max_results: u64,
    pub total: u64,
    pub worklogs: Vec<Worklog>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Session {
    pub name: String,
}