tftio-asana-cli 3.1.0

An interface to the Asana API
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
//! Task-oriented data structures, builders, and request payloads.

use super::{
    attachment::Attachment,
    custom_field::{CustomField, CustomFieldValue},
    user::UserReference,
    workspace::WorkspaceReference,
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::ops::Deref;
use thiserror::Error;

/// Lightweight reference to a task or subtask.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct TaskReference {
    /// Globally unique identifier.
    pub gid: String,
    /// Optional display name.
    #[serde(default)]
    pub name: Option<String>,
    /// Resource type marker.
    #[serde(default)]
    pub resource_type: Option<String>,
}

impl TaskReference {
    /// Produce a human readable label for the reference.
    #[must_use]
    pub fn label(&self) -> String {
        self.name.clone().unwrap_or_else(|| self.gid.clone())
    }
}

/// Task membership metadata linking it to projects and sections.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct TaskMembership {
    /// Parent project reference.
    #[serde(default)]
    pub project: Option<TaskProjectReference>,
    /// Section reference when available.
    #[serde(default)]
    pub section: Option<TaskSectionReference>,
}

/// Compact project reference used within task payloads.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd)]
#[serde(rename_all = "snake_case")]
pub struct TaskProjectReference {
    /// Globally unique identifier.
    pub gid: String,
    /// Optional project name.
    #[serde(default)]
    pub name: Option<String>,
    /// Resource type marker.
    #[serde(default)]
    pub resource_type: Option<String>,
}

impl TaskProjectReference {
    /// Human readable label.
    #[must_use]
    pub fn label(&self) -> String {
        self.name.clone().unwrap_or_else(|| self.gid.clone())
    }
}

/// Compact section reference.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct TaskSectionReference {
    /// Globally unique identifier.
    pub gid: String,
    /// Section name.
    #[serde(default)]
    pub name: Option<String>,
    /// Resource type marker.
    #[serde(default)]
    pub resource_type: Option<String>,
}

impl TaskSectionReference {
    /// Human readable label.
    #[must_use]
    pub fn label(&self) -> String {
        self.name.clone().unwrap_or_else(|| self.gid.clone())
    }
}

/// Compact tag reference used within tasks.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd)]
#[serde(rename_all = "snake_case")]
pub struct TaskTagReference {
    /// Globally unique identifier.
    pub gid: String,
    /// Tag name.
    #[serde(default)]
    pub name: Option<String>,
    /// Resource type marker.
    #[serde(default)]
    pub resource_type: Option<String>,
}

impl TaskTagReference {
    /// Human readable label.
    #[must_use]
    pub fn label(&self) -> String {
        self.name.clone().unwrap_or_else(|| self.gid.clone())
    }
}

/// Status of a task relative to the assignee's prioritisation buckets.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum TaskAssigneeStatus {
    /// Task recently assigned.
    #[default]
    Inbox,
    /// Deferred for later.
    Later,
    /// Scheduled for upcoming work.
    Upcoming,
    /// Due today.
    Today,
    /// Waiting on someone/something else.
    Waiting,
    /// Fallback for unsupported values.
    #[serde(other)]
    Unknown,
}

/// Full task payload returned by the Asana API.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct Task {
    /// Globally unique identifier.
    pub gid: String,
    /// Display name.
    pub name: String,
    /// Resource type marker.
    #[serde(default)]
    pub resource_type: Option<String>,
    /// Resource subtype marker.
    #[serde(default)]
    pub resource_subtype: Option<String>,
    /// Optional notes in plain text.
    #[serde(default)]
    pub notes: Option<String>,
    /// Optional notes in HTML.
    #[serde(default)]
    pub html_notes: Option<String>,
    /// Completion flag.
    #[serde(default)]
    pub completed: bool,
    /// Time the task was completed.
    #[serde(default)]
    pub completed_at: Option<String>,
    /// User who completed the task.
    #[serde(default)]
    pub completed_by: Option<UserReference>,
    /// Creation timestamp.
    #[serde(default)]
    pub created_at: Option<String>,
    /// Last modification timestamp.
    #[serde(default)]
    pub modified_at: Option<String>,
    /// Due date (all day).
    #[serde(default)]
    pub due_on: Option<String>,
    /// Due timestamp (specific time).
    #[serde(default)]
    pub due_at: Option<String>,
    /// Start date (all day).
    #[serde(default)]
    pub start_on: Option<String>,
    /// Start timestamp (specific time).
    #[serde(default)]
    pub start_at: Option<String>,
    /// Assigned user.
    #[serde(default)]
    pub assignee: Option<UserReference>,
    /// Assignee status bucket.
    #[serde(default)]
    pub assignee_status: Option<TaskAssigneeStatus>,
    /// Workspace reference.
    #[serde(default)]
    pub workspace: Option<WorkspaceReference>,
    /// Parent task reference when this is a subtask.
    #[serde(default)]
    pub parent: Option<TaskReference>,
    /// Project/section memberships.
    #[serde(default)]
    pub memberships: Vec<TaskMembership>,
    /// Projects referenced directly.
    #[serde(default)]
    pub projects: Vec<TaskProjectReference>,
    /// Tag references.
    #[serde(default)]
    pub tags: Vec<TaskTagReference>,
    /// Followers for notifications.
    #[serde(default)]
    pub followers: Vec<UserReference>,
    /// Tasks this task depends on.
    #[serde(default)]
    pub dependencies: Vec<TaskReference>,
    /// Tasks blocked by this task.
    #[serde(default)]
    pub dependents: Vec<TaskReference>,
    /// Custom field payloads.
    #[serde(default)]
    pub custom_fields: Vec<CustomField>,
    /// Attachment metadata when requested.
    #[serde(default)]
    pub attachments: Vec<Attachment>,
    /// Public permalink.
    #[serde(default)]
    pub permalink_url: Option<String>,
    /// Number of subtasks this task contains.
    #[serde(default)]
    pub num_subtasks: Option<i64>,
}

impl Task {
    /// Determines whether the task is currently incomplete.
    #[must_use]
    pub const fn is_open(&self) -> bool {
        !self.completed
    }
}

/// Parameters for listing tasks via the API.
#[derive(Debug, Clone, Default)]
pub struct TaskListParams {
    /// Workspace filter.
    pub workspace: Option<String>,
    /// Project filter.
    pub project: Option<String>,
    /// Section filter.
    pub section: Option<String>,
    /// Assignee filter.
    pub assignee: Option<String>,
    /// Completed since timestamp.
    pub completed_since: Option<String>,
    /// Modified since timestamp.
    pub modified_since: Option<String>,
    /// Exact due date filter.
    pub due_on: Option<String>,
    /// Include subtasks in listing.
    pub include_subtasks: bool,
    /// Maximum number of items to fetch (client side).
    pub limit: Option<usize>,
    /// Additional fields to request.
    pub fields: BTreeSet<String>,
    /// Sort order applied post-fetch.
    pub sort: Option<TaskSort>,
    /// Post-fetch completion filter.
    pub completed: Option<bool>,
    /// Post-fetch due date upper bound (inclusive, YYYY-MM-DD).
    pub due_before: Option<String>,
    /// Post-fetch due date lower bound (inclusive, YYYY-MM-DD).
    pub due_after: Option<String>,
}

impl TaskListParams {
    /// Convert the structure into query string pairs.
    #[must_use]
    pub fn to_query(&self) -> Vec<(String, String)> {
        let mut pairs = Vec::new();
        if let Some(workspace) = &self.workspace {
            pairs.push(("workspace".into(), workspace.clone()));
        }
        if let Some(project) = &self.project {
            pairs.push(("project".into(), project.clone()));
        }
        if let Some(section) = &self.section {
            pairs.push(("section".into(), section.clone()));
        }
        if let Some(assignee) = &self.assignee {
            pairs.push(("assignee".into(), assignee.clone()));
        }
        if let Some(completed_since) = &self.completed_since {
            pairs.push(("completed_since".into(), completed_since.clone()));
        }
        if let Some(modified_since) = &self.modified_since {
            pairs.push(("modified_since".into(), modified_since.clone()));
        }
        if let Some(due_on) = &self.due_on {
            pairs.push(("due_on".into(), due_on.clone()));
        }
        // Note: include_subtasks flag is used to control separate subtask fetching
        // after the main API call. The deprecated opt_expand=subtasks parameter
        // no longer returns complete field data and has been removed.
        if !self.fields.is_empty() {
            let field_list = self.fields.iter().cloned().collect::<Vec<_>>().join(",");
            pairs.push(("opt_fields".into(), field_list));
        }
        pairs
    }

    /// Apply local filters after API pagination.
    pub fn apply_post_filters(&self, tasks: &mut Vec<Task>) {
        if let Some(expected) = self.completed {
            tasks.retain(|task| task.completed == expected);
        }
        if let Some(due_before) = &self.due_before {
            tasks.retain(|task| task.due_on.as_ref().is_some_and(|due| due <= due_before));
        }
        if let Some(due_after) = &self.due_after {
            tasks.retain(|task| task.due_on.as_ref().is_some_and(|due| due >= due_after));
        }
    }
}

/// Supported sort orders for task listings.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskSort {
    /// Alphabetical by task name.
    Name,
    /// Due date ascending.
    DueOn,
    /// Creation timestamp ascending.
    CreatedAt,
    /// Modification timestamp ascending.
    ModifiedAt,
    /// Assignee display name.
    Assignee,
}

/// Parameters for searching tasks.
#[derive(Debug, Clone, Default)]
pub struct TaskSearchParams {
    /// Workspace to search in (required).
    pub workspace: String,
    /// Full-text search query.
    pub text: Option<String>,
    /// Resource subtype filter.
    pub resource_subtype: Option<String>,
    /// Completion status filter.
    pub completed: Option<bool>,
    /// Include subtasks.
    pub is_subtask: Option<bool>,
    /// Filter blocked tasks.
    pub is_blocked: Option<bool>,
    /// Filter tasks with attachments.
    pub has_attachment: Option<bool>,
    /// Assignee filter (gid or "me").
    pub assignee: Option<String>,
    /// Project filter (gid).
    pub projects: Vec<String>,
    /// Section filter (gid).
    pub sections: Vec<String>,
    /// Tag filter (gid).
    pub tags: Vec<String>,
    /// Created after date (YYYY-MM-DD).
    pub created_after: Option<String>,
    /// Created before date (YYYY-MM-DD).
    pub created_before: Option<String>,
    /// Modified after date (YYYY-MM-DD).
    pub modified_after: Option<String>,
    /// Modified before date (YYYY-MM-DD).
    pub modified_before: Option<String>,
    /// Due after date (YYYY-MM-DD).
    pub due_after: Option<String>,
    /// Due before date (YYYY-MM-DD).
    pub due_before: Option<String>,
    /// Sort field.
    pub sort_by: Option<String>,
    /// Sort ascending.
    pub sort_ascending: bool,
    /// Maximum number of items to fetch.
    pub limit: Option<usize>,
    /// Additional fields to request.
    pub fields: BTreeSet<String>,
}

impl TaskSearchParams {
    /// Convert to query parameters.
    #[must_use]
    pub fn to_query(&self) -> Vec<(String, String)> {
        let mut pairs = Vec::new();

        if let Some(text) = &self.text {
            pairs.push(("text".into(), text.clone()));
        }
        if let Some(subtype) = &self.resource_subtype {
            pairs.push(("resource_subtype".into(), subtype.clone()));
        }
        if let Some(completed) = self.completed {
            pairs.push(("completed".into(), completed.to_string()));
        }
        if let Some(is_subtask) = self.is_subtask {
            pairs.push(("is_subtask".into(), is_subtask.to_string()));
        }
        if let Some(is_blocked) = self.is_blocked {
            pairs.push(("is_blocked".into(), is_blocked.to_string()));
        }
        if let Some(has_attachment) = self.has_attachment {
            pairs.push(("has_attachment".into(), has_attachment.to_string()));
        }
        if let Some(assignee) = &self.assignee {
            pairs.push(("assignee.any".into(), assignee.clone()));
        }

        for project in &self.projects {
            pairs.push(("projects.any".into(), project.clone()));
        }
        for section in &self.sections {
            pairs.push(("sections.any".into(), section.clone()));
        }
        for tag in &self.tags {
            pairs.push(("tags.any".into(), tag.clone()));
        }

        if let Some(date) = &self.created_after {
            pairs.push(("created_at.after".into(), date.clone()));
        }
        if let Some(date) = &self.created_before {
            pairs.push(("created_at.before".into(), date.clone()));
        }
        if let Some(date) = &self.modified_after {
            pairs.push(("modified_at.after".into(), date.clone()));
        }
        if let Some(date) = &self.modified_before {
            pairs.push(("modified_at.before".into(), date.clone()));
        }
        if let Some(date) = &self.due_after {
            pairs.push(("due_on.after".into(), date.clone()));
        }
        if let Some(date) = &self.due_before {
            pairs.push(("due_on.before".into(), date.clone()));
        }

        if let Some(sort_by) = &self.sort_by {
            pairs.push(("sort_by".into(), sort_by.clone()));
            pairs.push(("sort_ascending".into(), self.sort_ascending.to_string()));
        }

        if !self.fields.is_empty() {
            let field_list = self.fields.iter().cloned().collect::<Vec<_>>().join(",");
            pairs.push(("opt_fields".into(), field_list));
        }

        pairs
    }
}

/// Payload for creating tasks.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct TaskCreateData {
    /// Task name (required).
    pub name: String,
    /// Optional notes (plain text).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
    /// Optional notes in HTML format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub html_notes: Option<String>,
    /// Workspace or organization identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace: Option<String>,
    /// Associated project identifiers.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub projects: Vec<String>,
    /// Section identifier when provided.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub section: Option<String>,
    /// Parent task identifier for creating subtasks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,
    /// Assigned user (gid or email).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee: Option<String>,
    /// Due date (all day).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due_on: Option<String>,
    /// Due timestamp.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due_at: Option<String>,
    /// Start date (all day).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_on: Option<String>,
    /// Start timestamp.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_at: Option<String>,
    /// Tags to apply.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
    /// Followers to notify.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub followers: Vec<String>,
    /// Custom field assignments.
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub custom_fields: BTreeMap<String, serde_json::Value>,
}

/// API envelope for create requests.
#[derive(Debug, Clone, Serialize)]
pub struct TaskCreateRequest {
    /// Wrapped data payload.
    pub data: TaskCreateData,
}

/// Builder for constructing validated task create payloads.
#[derive(Debug, Clone)]
pub struct TaskCreateBuilder {
    data: TaskCreateData,
}

impl TaskCreateBuilder {
    /// Start building a new task payload with the required name.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        let name = name.into();
        Self {
            data: TaskCreateData {
                name,
                notes: None,
                html_notes: None,
                workspace: None,
                projects: Vec::new(),
                section: None,
                parent: None,
                assignee: None,
                due_on: None,
                due_at: None,
                start_on: None,
                start_at: None,
                tags: Vec::new(),
                followers: Vec::new(),
                custom_fields: BTreeMap::new(),
            },
        }
    }

    /// Override the task name.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.data.name = name.into();
        self
    }

    /// Provide plain text notes.
    #[must_use]
    pub fn notes(mut self, notes: impl Into<String>) -> Self {
        self.data.notes = Some(notes.into());
        self
    }

    /// Provide HTML formatted notes.
    #[must_use]
    pub fn html_notes(mut self, notes: impl Into<String>) -> Self {
        self.data.html_notes = Some(notes.into());
        self
    }

    /// Set the workspace gid.
    #[must_use]
    pub fn workspace(mut self, workspace: impl Into<String>) -> Self {
        self.data.workspace = Some(workspace.into());
        self
    }

    /// Add a project gid association.
    #[must_use]
    pub fn project(mut self, project: impl Into<String>) -> Self {
        let gid = project.into();
        if !self.data.projects.contains(&gid) {
            self.data.projects.push(gid);
        }
        self
    }

    /// Target a specific section gid when creating within a project.
    #[must_use]
    pub fn section(mut self, section: impl Into<String>) -> Self {
        self.data.section = Some(section.into());
        self
    }

    /// Set the parent task gid to create a subtask.
    #[must_use]
    pub fn parent(mut self, parent: impl Into<String>) -> Self {
        self.data.parent = Some(parent.into());
        self
    }

    /// Assign the task to a user (gid or email).
    #[must_use]
    pub fn assignee(mut self, assignee: impl Into<String>) -> Self {
        self.data.assignee = Some(assignee.into());
        self
    }

    /// Set the due date (all day).
    #[must_use]
    pub fn due_on(mut self, due_on: impl Into<String>) -> Self {
        self.data.due_on = Some(due_on.into());
        self
    }

    /// Set the due timestamp.
    #[must_use]
    pub fn due_at(mut self, due_at: impl Into<String>) -> Self {
        self.data.due_at = Some(due_at.into());
        self
    }

    /// Set the start date (all day).
    #[must_use]
    pub fn start_on(mut self, start_on: impl Into<String>) -> Self {
        self.data.start_on = Some(start_on.into());
        self
    }

    /// Set the start timestamp.
    #[must_use]
    pub fn start_at(mut self, start_at: impl Into<String>) -> Self {
        self.data.start_at = Some(start_at.into());
        self
    }

    /// Add a tag identifier.
    #[must_use]
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        let gid = tag.into();
        if !self.data.tags.contains(&gid) {
            self.data.tags.push(gid);
        }
        self
    }

    /// Add a follower identifier.
    #[must_use]
    pub fn follower(mut self, follower: impl Into<String>) -> Self {
        let gid = follower.into();
        if !self.data.followers.contains(&gid) {
            self.data.followers.push(gid);
        }
        self
    }

    /// Assign a custom field value.
    #[must_use]
    pub fn custom_field(mut self, field_gid: impl Into<String>, value: CustomFieldValue) -> Self {
        self.data
            .custom_fields
            .insert(field_gid.into(), value.into_value());
        self
    }

    /// Finalise the builder into a request payload performing validation.
    ///
    /// # Errors
    ///
    /// Returns a validation error if mandatory fields are missing or invalid.
    pub fn build(mut self) -> Result<TaskCreateRequest, TaskValidationError> {
        if self.data.name.trim().is_empty() {
            return Err(TaskValidationError::MissingName);
        }
        if self.data.workspace.is_none()
            && self.data.projects.is_empty()
            && self.data.parent.is_none()
        {
            return Err(TaskValidationError::MissingScope);
        }
        if let Some(ref html) = self.data.html_notes {
            let prepared = crate::rich_text::prepare_rich_text(
                html,
                crate::rich_text::RichTextContext::TaskNotes,
            )?;
            self.data.html_notes = Some(prepared);
        }
        Ok(TaskCreateRequest { data: self.data })
    }
}

/// Payload for updating existing tasks.
///
/// Uses `Option<Option<T>>` for certain fields to distinguish three API states:
/// - `None`: Don't update field (omit from JSON payload)
/// - `Some(None)`: Clear field (send `null` in JSON to remove value)
/// - `Some(Some(value))`: Set field to new value
///
/// This is required by the Asana API which treats missing fields differently from
/// explicit `null` values. Omitting a field preserves its current value, while
/// sending `null` clears it.
#[allow(
    clippy::option_option,
    reason = "Option<Option<T>> models the API PATCH tri-state: absent leaves the field unchanged, null clears it, Some(v) sets it"
)]
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct TaskUpdateData {
    /// Task name update.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Plain text notes update.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<Option<String>>,
    /// HTML notes update.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub html_notes: Option<Option<String>>,
    /// Completion flag change.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completed: Option<bool>,
    /// Assignee change (gid/email) or explicit null.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee: Option<Option<String>>,
    /// Due date change (all day) or explicit null.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due_on: Option<Option<String>>,
    /// Due timestamp change or explicit null.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due_at: Option<Option<String>>,
    /// Start date change or explicit null.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_on: Option<Option<String>>,
    /// Start timestamp change or explicit null.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_at: Option<Option<String>>,
    /// Parent assignment or removal.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<Option<String>>,
    /// Replace tags with the provided identifiers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    /// Replace followers with the provided identifiers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub followers: Option<Vec<String>>,
    /// Replace project associations with the provided identifiers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub projects: Option<Vec<String>>,
    /// Custom field updates.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<BTreeMap<String, serde_json::Value>>,
}

impl TaskUpdateData {
    /// Determine whether any fields have been set.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.name.is_none()
            && self.notes.is_none()
            && self.html_notes.is_none()
            && self.completed.is_none()
            && self.assignee.is_none()
            && self.due_on.is_none()
            && self.due_at.is_none()
            && self.start_on.is_none()
            && self.start_at.is_none()
            && self.parent.is_none()
            && self.tags.is_none()
            && self.followers.is_none()
            && self.projects.is_none()
            && self.custom_fields.is_none()
    }
}

/// API envelope for update requests.
#[derive(Debug, Clone, Serialize)]
pub struct TaskUpdateRequest {
    /// Wrapped data payload.
    pub data: TaskUpdateData,
}

/// Builder for constructing validated task update payloads.
#[derive(Debug, Default, Clone)]
pub struct TaskUpdateBuilder {
    data: TaskUpdateData,
}

impl TaskUpdateBuilder {
    /// Create a new empty builder.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the task name.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.data.name = Some(name.into());
        self
    }

    /// Replace notes with the provided plain text.
    #[must_use]
    pub fn notes(mut self, notes: impl Into<String>) -> Self {
        self.data.notes = Some(Some(notes.into()));
        self
    }

    /// Clear the plain text notes.
    #[must_use]
    pub fn clear_notes(mut self) -> Self {
        self.data.notes = Some(None);
        self
    }

    /// Set HTML formatted notes.
    #[must_use]
    pub fn html_notes(mut self, notes: impl Into<String>) -> Self {
        self.data.html_notes = Some(Some(notes.into()));
        self
    }

    /// Clear HTML notes.
    #[must_use]
    pub fn clear_html_notes(mut self) -> Self {
        self.data.html_notes = Some(None);
        self
    }

    /// Mark the task completed/incomplete.
    #[must_use]
    pub fn completed(mut self, completed: bool) -> Self {
        self.data.completed = Some(completed);
        self
    }

    /// Assign the task.
    #[must_use]
    pub fn assignee(mut self, assignee: impl Into<String>) -> Self {
        self.data.assignee = Some(Some(assignee.into()));
        self
    }

    /// Remove the current assignee.
    #[must_use]
    pub fn clear_assignee(mut self) -> Self {
        self.data.assignee = Some(None);
        self
    }

    /// Set the due date (all day).
    #[must_use]
    pub fn due_on(mut self, due_on: impl Into<String>) -> Self {
        self.data.due_on = Some(Some(due_on.into()));
        self
    }

    /// Clear the due date.
    #[must_use]
    pub fn clear_due_on(mut self) -> Self {
        self.data.due_on = Some(None);
        self
    }

    /// Set the due timestamp.
    #[must_use]
    pub fn due_at(mut self, due_at: impl Into<String>) -> Self {
        self.data.due_at = Some(Some(due_at.into()));
        self
    }

    /// Clear the due timestamp.
    #[must_use]
    pub fn clear_due_at(mut self) -> Self {
        self.data.due_at = Some(None);
        self
    }

    /// Set the start date.
    #[must_use]
    pub fn start_on(mut self, start_on: impl Into<String>) -> Self {
        self.data.start_on = Some(Some(start_on.into()));
        self
    }

    /// Clear the start date.
    #[must_use]
    pub fn clear_start_on(mut self) -> Self {
        self.data.start_on = Some(None);
        self
    }

    /// Set the start timestamp.
    #[must_use]
    pub fn start_at(mut self, start_at: impl Into<String>) -> Self {
        self.data.start_at = Some(Some(start_at.into()));
        self
    }

    /// Clear the start timestamp.
    #[must_use]
    pub fn clear_start_at(mut self) -> Self {
        self.data.start_at = Some(None);
        self
    }

    /// Set the parent task.
    #[must_use]
    pub fn parent(mut self, parent: impl Into<String>) -> Self {
        self.data.parent = Some(Some(parent.into()));
        self
    }

    /// Remove the parent task.
    #[must_use]
    pub fn clear_parent(mut self) -> Self {
        self.data.parent = Some(None);
        self
    }

    /// Replace tags with the provided identifiers.
    #[must_use]
    pub fn tags<I, S>(mut self, tags: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut values: Vec<String> = tags.into_iter().map(Into::into).collect();
        values.sort();
        values.dedup();
        self.data.tags = Some(values);
        self
    }

    /// Replace followers with the provided identifiers.
    #[must_use]
    pub fn followers<I, S>(mut self, followers: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut values: Vec<String> = followers.into_iter().map(Into::into).collect();
        values.sort();
        values.dedup();
        self.data.followers = Some(values);
        self
    }

    /// Replace project associations.
    #[must_use]
    pub fn projects<I, S>(mut self, projects: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut values: Vec<String> = projects.into_iter().map(Into::into).collect();
        values.sort();
        values.dedup();
        self.data.projects = Some(values);
        self
    }

    /// Set a custom field value.
    #[must_use]
    pub fn custom_field(mut self, field_gid: impl Into<String>, value: CustomFieldValue) -> Self {
        let map = self.data.custom_fields.get_or_insert_with(BTreeMap::new);
        map.insert(field_gid.into(), value.into_value());
        self
    }

    /// Finalise the builder.
    ///
    /// # Errors
    ///
    /// Returns an error if no fields were modified.
    pub fn build(mut self) -> Result<TaskUpdateRequest, TaskValidationError> {
        if self.data.is_empty() {
            return Err(TaskValidationError::EmptyUpdate);
        }
        if let Some(Some(ref html)) = self.data.html_notes {
            let prepared = crate::rich_text::prepare_rich_text(
                html,
                crate::rich_text::RichTextContext::TaskNotes,
            )?;
            self.data.html_notes = Some(Some(prepared));
        }
        Ok(TaskUpdateRequest { data: self.data })
    }
}

/// Errors emitted during task payload validation.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum TaskValidationError {
    /// Task name was missing or blank.
    #[error("task name cannot be empty")]
    MissingName,
    /// Workspace or project context missing when creating a task.
    #[error("tasks require either a workspace or at least one project")]
    MissingScope,
    /// HTML rich text content is not valid XML.
    #[error("invalid html_notes: {0}")]
    InvalidHtml(#[from] crate::rich_text::RichTextError),
    /// Update payload did not contain any fields.
    #[error("task update payload does not include any changes")]
    EmptyUpdate,
}

impl Deref for TaskUpdateBuilder {
    type Target = TaskUpdateData;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

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

    #[test]
    fn create_builder_requires_name() {
        let builder = TaskCreateBuilder::new("  ");
        let result = builder.build();
        assert_eq!(result.unwrap_err(), TaskValidationError::MissingName);
    }

    #[test]
    fn create_builder_requires_scope() {
        let builder = TaskCreateBuilder::new("Sample task").notes("demo");
        let result = builder.build();
        assert_eq!(result.unwrap_err(), TaskValidationError::MissingScope);
    }

    #[test]
    fn create_builder_success() {
        let builder = TaskCreateBuilder::new("Sample task")
            .workspace("123")
            .assignee("me");
        let request = builder.build().expect("builder should succeed");
        assert_eq!(request.data.name, "Sample task");
        assert_eq!(request.data.workspace.as_deref(), Some("123"));
    }

    #[test]
    fn create_builder_accepts_parent_scope() {
        let request = TaskCreateBuilder::new("Child")
            .parent("T1")
            .build()
            .expect("builder should succeed");
        assert_eq!(request.data.parent.as_deref(), Some("T1"));
    }

    #[test]
    fn create_builder_rejects_invalid_html_notes() {
        let result = TaskCreateBuilder::new("Test")
            .workspace("ws1")
            .html_notes("<body><em>bad</strong></body>")
            .build();

        assert!(matches!(
            result.unwrap_err(),
            TaskValidationError::InvalidHtml(_)
        ));
    }

    #[test]
    fn create_builder_prepares_html_notes() {
        let request = TaskCreateBuilder::new("Test")
            .workspace("ws1")
            .html_notes("Tom & Jerry")
            .build()
            .expect("builder should succeed");

        assert_eq!(
            request.data.html_notes.as_deref(),
            Some("<body>Tom &amp; Jerry</body>")
        );
    }

    #[test]
    fn update_builder_requires_changes() {
        let builder = TaskUpdateBuilder::new();
        let result = builder.build();
        assert_eq!(result.unwrap_err(), TaskValidationError::EmptyUpdate);
    }

    #[test]
    fn update_builder_accepts_changes() {
        let request = TaskUpdateBuilder::new()
            .name("Updated")
            .completed(true)
            .build()
            .expect("builder should succeed");
        assert_eq!(request.data.name.as_deref(), Some("Updated"));
        assert_eq!(request.data.completed, Some(true));
    }

    #[test]
    fn update_builder_validates_html_notes() {
        let result = TaskUpdateBuilder::new()
            .html_notes("<body><em>bad</strong></body>")
            .build();

        assert!(matches!(
            result.unwrap_err(),
            TaskValidationError::InvalidHtml(_)
        ));
    }

    #[test]
    fn update_builder_prepares_html_notes() {
        let request = TaskUpdateBuilder::new()
            .html_notes("fix & update")
            .build()
            .expect("builder should succeed");

        assert_eq!(
            request.data.html_notes,
            Some(Some("<body>fix &amp; update</body>".to_string()))
        );
    }

    #[test]
    fn update_builder_clear_html_notes_skips_validation() {
        let request = TaskUpdateBuilder::new()
            .clear_html_notes()
            .build()
            .expect("builder should succeed");

        assert_eq!(request.data.html_notes, Some(None));
    }

    #[test]
    fn create_builder_serializes_custom_field() {
        let request = TaskCreateBuilder::new("With field")
            .workspace("ws-1")
            .custom_field("cf1", CustomFieldValue::Bool(true))
            .build()
            .expect("builder should succeed");
        assert_eq!(
            request.data.custom_fields.get("cf1"),
            Some(&Value::Bool(true))
        );
    }

    #[test]
    fn update_builder_clears_assignee() {
        let request = TaskUpdateBuilder::new()
            .clear_assignee()
            .build()
            .expect("builder should succeed");
        assert_eq!(request.data.assignee, Some(None));
    }

    #[test]
    fn update_builder_sets_custom_field_value() {
        let request = TaskUpdateBuilder::new()
            .custom_field("cf1", CustomFieldValue::Number(5.0))
            .build()
            .expect("builder should succeed");
        let map = request
            .data
            .custom_fields
            .as_ref()
            .expect("custom fields set");
        assert!(
            map.get("cf1")
                .and_then(Value::as_f64)
                .is_some_and(|value| (value - 5.0).abs() < f64::EPSILON)
        );
    }
}