clickup_v2 0.1.1

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

/// Tipo de entidade que pode ser pesquisada
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum EntityType {
    Space,
    Folder,
    List,
    Task,
}

/// Resultado da pesquisa de entidade
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SearchResult {
    pub found: bool,
    pub entity_type: EntityType,
    pub items: Vec<EntityItem>,
    pub cached_at: Option<chrono::DateTime<chrono::Utc>>,
}

/// Item encontrado na pesquisa
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct EntityItem {
    pub id: String,
    pub name: String,
    pub url: String,
    pub entity_type: EntityType,
    pub parent_id: Option<String>,
    pub parent_name: Option<String>,
}

/// Chave para o cache
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct CacheKey {
    name: String,
    entity_type: EntityType,
    team_id: String,
}

/// Entrada no cache com timestamp
#[derive(Debug, Clone)]
struct CacheEntry {
    result: SearchResult,
    cached_at: chrono::DateTime<chrono::Utc>,
}

/// Cliente HTTP para interagir com a API do ClickUp
#[derive(Debug, Clone)]
pub struct ClickUpClient {
    client: Client,
    token: String,
    base_url: String,
    cache: Arc<RwLock<HashMap<CacheKey, CacheEntry>>>,
}

/// Resposta do endpoint de usuário autorizado
#[derive(Debug, serde::Deserialize)]
pub struct AuthorizedUser {
    pub id: u64,
    pub username: String,
    pub email: String,
    pub color: String,
    pub initials: String,
    pub profile_picture: Option<String>,
}

/// Resposta do endpoint de equipes autorizadas
#[derive(Debug, serde::Deserialize)]
pub struct AuthorizedTeam {
    pub id: String,
    pub name: String,
    pub color: String,
    pub avatar: Option<String>,
}

/// Resposta do endpoint de workspaces
#[derive(Debug, serde::Deserialize)]
pub struct Workspace {
    pub id: String,
    pub name: String,
    pub color: String,
    pub avatar: Option<String>,
    pub members: Option<Vec<Value>>,
}

/// Valores possíveis para campos personalizados
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum CustomFieldValue {
    /// Valor de texto
    Text(String),
    /// Valor numérico
    Number(f64),
    /// Valor booleano
    Boolean(bool),
    /// Data em timestamp Unix (milliseconds)
    Date(i64),
    /// URL
    Url(String),
    /// Email
    Email(String),
    /// Telefone com código do país
    Phone(String),
    /// Dropdown - ID da opção
    DropdownOption(String),
    /// Multi-select - array de IDs das opções
    MultiSelect(Vec<String>),
    /// Usuários - array de user IDs
    Users(Vec<i64>),
    /// Localização
    Location {
        lat: f64,
        lng: f64,
        formatted_address: Option<String>,
    },
    /// Rating (1-5)
    Rating(i32),
    /// Moeda
    Currency(f64),
}

/// Campo personalizado para criar/atualizar task
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CustomField {
    /// UUID do campo personalizado
    pub id: String,
    /// Valor do campo
    pub value: CustomFieldValue,
}

/// Prioridade da task (1=Urgent, 2=High, 3=Normal, 4=Low)
#[derive(Debug, Clone, Copy)]
pub enum TaskPriority {
    Urgent = 1,
    High = 2,
    Normal = 3,
    Low = 4,
}

impl Default for TaskPriority {
    fn default() -> Self {
        TaskPriority::Normal
    }
}

impl serde::Serialize for TaskPriority {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_i32(*self as i32)
    }
}

/// Payload para criar uma nova task
#[derive(Debug, Clone, serde::Serialize)]
pub struct CreateTaskRequest {
    /// Nome/título da task (obrigatório)
    pub name: String,

    /// Conteúdo/descrição da task
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,

    /// Conteúdo em markdown
    #[serde(skip_serializing_if = "Option::is_none")]
    pub markdown_content: Option<String>,

    /// IDs dos responsáveis (userids)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignees: Option<Vec<i64>>,

    /// Tags da task
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,

    /// Status da task (nome do status)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,

    /// Prioridade (1=Urgent, 2=High, 3=Normal, 4=Low)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub priority: Option<i32>,

    /// Data de vencimento em timestamp Unix (milliseconds)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due_date: Option<i64>,

    /// Usar horário na data de vencimento
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due_date_time: Option<bool>,

    /// Data de início em timestamp Unix (milliseconds)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_date: Option<i64>,

    /// Usar horário na data de início
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_date_time: Option<bool>,

    /// Tempo estimado em milliseconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_estimate: Option<i64>,

    /// Task pai (para subtasks)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,

    /// Links para outras tasks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub links_to: Option<String>,

    /// Notificar todos incluindo o criador
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notify_all: Option<bool>,

    /// Campos personalizados
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_fields: Option<Vec<CustomField>>,
}

impl CreateTaskRequest {
    /// Cria um novo request básico com apenas o nome
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            content: None,
            markdown_content: None,
            assignees: None,
            tags: None,
            status: None,
            priority: None,
            due_date: None,
            due_date_time: None,
            start_date: None,
            start_date_time: None,
            time_estimate: None,
            parent: None,
            links_to: None,
            notify_all: None,
            custom_fields: None,
        }
    }

    /// Cria um builder para construir o request
    pub fn builder(name: impl Into<String>) -> CreateTaskRequestBuilder {
        CreateTaskRequestBuilder::new(name)
    }
}

/// Builder para CreateTaskRequest
pub struct CreateTaskRequestBuilder {
    request: CreateTaskRequest,
}

impl CreateTaskRequestBuilder {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            request: CreateTaskRequest::new(name),
        }
    }

    pub fn content(mut self, content: impl Into<String>) -> Self {
        self.request.content = Some(content.into());
        self
    }

    pub fn markdown_content(mut self, content: impl Into<String>) -> Self {
        self.request.markdown_content = Some(content.into());
        self
    }

    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.request.content = Some(desc.into());
        self
    }

    pub fn assignees(mut self, assignees: Vec<i64>) -> Self {
        self.request.assignees = Some(assignees);
        self
    }

    pub fn tags(mut self, tags: Vec<String>) -> Self {
        self.request.tags = Some(tags);
        self
    }

    pub fn status(mut self, status: impl Into<String>) -> Self {
        self.request.status = Some(status.into());
        self
    }

    pub fn priority(mut self, priority: TaskPriority) -> Self {
        self.request.priority = Some(priority as i32);
        self
    }

    pub fn due_date(mut self, timestamp: i64, with_time: bool) -> Self {
        self.request.due_date = Some(timestamp);
        self.request.due_date_time = Some(with_time);
        self
    }

    pub fn start_date(mut self, timestamp: i64, with_time: bool) -> Self {
        self.request.start_date = Some(timestamp);
        self.request.start_date_time = Some(with_time);
        self
    }

    pub fn time_estimate(mut self, millis: i64) -> Self {
        self.request.time_estimate = Some(millis);
        self
    }

    pub fn parent(mut self, parent: impl Into<String>) -> Self {
        self.request.parent = Some(parent.into());
        self
    }

    pub fn notify_all(mut self, notify: bool) -> Self {
        self.request.notify_all = Some(notify);
        self
    }

    /// Adiciona um campo personalizado
    pub fn custom_field(mut self, id: impl Into<String>, value: CustomFieldValue) -> Self {
        let field = CustomField {
            id: id.into(),
            value,
        };

        match self.request.custom_fields {
            Some(ref mut fields) => fields.push(field),
            None => self.request.custom_fields = Some(vec![field]),
        }

        self
    }

    /// Define múltiplos campos personalizados
    pub fn custom_fields(mut self, fields: Vec<CustomField>) -> Self {
        self.request.custom_fields = Some(fields);
        self
    }

    /// Adiciona campo de texto personalizado
    pub fn custom_field_text(self, id: impl Into<String>, value: impl Into<String>) -> Self {
        self.custom_field(id, CustomFieldValue::Text(value.into()))
    }

    /// Adiciona campo numérico personalizado
    pub fn custom_field_number(self, id: impl Into<String>, value: f64) -> Self {
        self.custom_field(id, CustomFieldValue::Number(value))
    }

    /// Adiciona campo de data personalizado
    pub fn custom_field_date(self, id: impl Into<String>, timestamp: i64) -> Self {
        self.custom_field(id, CustomFieldValue::Date(timestamp))
    }

    /// Adiciona campo dropdown personalizado
    pub fn custom_field_dropdown(self, id: impl Into<String>, option_id: impl Into<String>) -> Self {
        self.custom_field(id, CustomFieldValue::DropdownOption(option_id.into()))
    }

    /// Adiciona campo de rating personalizado (1-5)
    pub fn custom_field_rating(self, id: impl Into<String>, rating: i32) -> Self {
        let rating = rating.max(1).min(5);
        self.custom_field(id, CustomFieldValue::Rating(rating))
    }

    /// Constrói o request final
    pub fn build(self) -> CreateTaskRequest {
        self.request
    }
}

/// Resposta da criação de task
#[derive(Debug, serde::Deserialize)]
pub struct TaskResponse {
    pub id: String,
    pub custom_id: Option<String>,
    pub name: String,
    pub text_content: Option<String>,
    pub description: Option<String>,
    pub status: Value,
    pub orderindex: String,
    pub date_created: String,
    pub date_updated: String,
    pub date_closed: Option<String>,
    pub archived: bool,
    pub creator: Value,
    pub assignees: Vec<Value>,
    pub tags: Vec<Value>,
    pub parent: Option<String>,
    pub priority: Option<Value>,
    pub due_date: Option<String>,
    pub start_date: Option<String>,
    pub time_estimate: Option<i64>,
    pub custom_fields: Vec<Value>,
    pub url: String,
    pub list: Value,
    pub folder: Value,
    pub space: Value,
}

impl ClickUpClient {
    /// Cria um novo cliente da API do ClickUp
    pub fn new(token: String, base_url: String) -> Self {
        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&token).unwrap_or_else(|_| HeaderValue::from_static(""))
        );
        headers.insert(
            "Content-Type",
            HeaderValue::from_static("application/json")
        );

        let client = Client::builder()
            .default_headers(headers)
            .timeout(Duration::from_secs(30))
            .build()
            .unwrap_or_default();

        Self {
            client,
            token,
            base_url: base_url.trim_end_matches('/').to_string(),
            cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Constrói URL completa para um endpoint
    fn build_url(&self, endpoint: &str) -> String {
        let endpoint = endpoint.trim_start_matches('/');
        format!("{}/{}", self.base_url, endpoint)
    }

    /// Executa uma requisição GET
    async fn get(&self, endpoint: &str) -> AuthResult<Value> {
        let url = self.build_url(endpoint);

        log::debug!("GET {}", url);

        let response = self.client
            .get(&url)
            .send()
            .await
            .map_err(|e| AuthError::network_error(&format!("Falha na requisição GET: {}", e)))?;

        let status = response.status();
        let response_text = response
            .text()
            .await
            .map_err(|e| AuthError::network_error(&format!("Falha ao ler resposta: {}", e)))?;

        log::debug!("Response status: {}, body: {}", status, response_text);

        if !status.is_success() {
            return Err(self.handle_error_response(status.as_u16(), &response_text));
        }

        serde_json::from_str(&response_text)
            .map_err(|e| AuthError::parse_error(&format!("Falha ao parsear JSON: {}", e)))
    }

    /// Executa uma requisição POST
    async fn post<T: serde::Serialize>(&self, endpoint: &str, body: &T) -> AuthResult<Value> {
        let url = self.build_url(endpoint);
        let json_body = serde_json::to_string(body)
            .map_err(|e| AuthError::parse_error(&format!("Falha ao serializar body: {}", e)))?;

        log::debug!("POST {} with body: {}", url, json_body);

        let response = self.client
            .post(&url)
            .header("Content-Type", "application/json")
            .body(json_body)
            .send()
            .await
            .map_err(|e| AuthError::network_error(&format!("Falha na requisição POST: {}", e)))?;

        let status = response.status();
        let response_text = response
            .text()
            .await
            .map_err(|e| AuthError::network_error(&format!("Falha ao ler resposta: {}", e)))?;

        log::debug!("Response status: {}, body: {}", status, response_text);

        if !status.is_success() {
            return Err(self.handle_error_response(status.as_u16(), &response_text));
        }

        serde_json::from_str(&response_text)
            .map_err(|e| AuthError::parse_error(&format!("Falha ao parsear resposta JSON: {}", e)))
    }

    /// Trata respostas de erro da API
    fn handle_error_response(&self, status: u16, body: &str) -> AuthError {
        match status {
            401 => AuthError::token_error("Token de acesso inválido ou expirado"),
            403 => AuthError::token_error("Acesso negado - verifique as permissões"),
            404 => AuthError::api_error("Endpoint não encontrado"),
            429 => AuthError::api_error("Limite de requisições excedido"),
            500..=599 => AuthError::api_error("Erro interno do servidor ClickUp"),
            _ => AuthError::api_error(&format!("Erro na API ({}): {}", status, body))
        }
    }

    /// **ENDPOINT 1: Health Check** - Valida se as credenciais são válidas
    /// Faz uma requisição simples para verificar se o token está funcionando
    pub async fn health_check(&self) -> AuthResult<bool> {
        log::info!("🏥 Executando health check...");
        
        match self.get_authorized_user().await {
            Ok(_) => {
                log::info!("✅ Health check passou - credenciais válidas");
                Ok(true)
            },
            Err(e) => {
                log::warn!("❌ Health check falhou: {}", e);
                Ok(false)
            }
        }
    }

    /// **ENDPOINT 2: Get Authorized User** - Obtém informações do usuário autenticado
    /// GET /user
    pub async fn get_authorized_user(&self) -> AuthResult<Value> {
        log::info!("👤 Obtendo informações do usuário autenticado...");
        self.get("user").await
    }

    /// **ENDPOINT 3: Get Authorized Teams** - Obtém equipes autorizadas (workspaces)
    /// GET /team
    pub async fn get_authorized_teams(&self) -> AuthResult<Value> {
        log::info!("👥 Obtendo equipes autorizadas...");
        self.get("team").await
    }

    /// **MÉTODO AUXILIAR: Parse User** - Converte resposta JSON para struct tipada
    pub async fn get_user_info(&self) -> AuthResult<AuthorizedUser> {
        let response = self.get_authorized_user().await?;
        
        let user_data = response
            .get("user")
            .ok_or_else(|| AuthError::parse_error("Campo 'user' não encontrado na resposta"))?;

        serde_json::from_value(user_data.clone())
            .map_err(|e| AuthError::parse_error(&format!("Falha ao parsear dados do usuário: {}", e)))
    }

    /// **MÉTODO AUXILIAR: Parse Teams** - Converte resposta JSON para lista tipada
    pub async fn get_teams_info(&self) -> AuthResult<Vec<AuthorizedTeam>> {
        let response = self.get_authorized_teams().await?;
        
        let teams_data = response
            .get("teams")
            .and_then(|t| t.as_array())
            .ok_or_else(|| AuthError::parse_error("Campo 'teams' não encontrado ou não é um array"))?;

        teams_data.iter()
            .map(|team| serde_json::from_value(team.clone())
                .map_err(|e| AuthError::parse_error(&format!("Falha ao parsear equipe: {}", e))))
            .collect()
    }

    /// **MÉTODO AUXILIAR: Get Workspaces** - Alias para get_authorized_teams (são a mesma coisa)
    pub async fn get_workspaces(&self) -> AuthResult<Value> {
        log::info!("🏢 Obtendo workspaces (alias para teams)...");
        self.get_authorized_teams().await
    }

    /// **MÉTODO AUXILIAR: Get First Workspace ID** - Obtém o ID do primeiro workspace disponível
    pub async fn get_first_workspace_id(&self) -> AuthResult<String> {
        let teams = self.get_authorized_teams().await?;
        
        let teams_array = teams
            .get("teams")
            .and_then(|t| t.as_array())
            .ok_or_else(|| AuthError::parse_error("Nenhuma equipe encontrada"))?;

        if teams_array.is_empty() {
            return Err(AuthError::api_error("Usuário não possui acesso a nenhuma equipe"));
        }

        let first_team_id = teams_array[0]
            .get("id")
            .and_then(|id| id.as_str())
            .ok_or_else(|| AuthError::parse_error("ID da primeira equipe não encontrado"))?;

        log::info!("📋 Primeiro workspace ID: {}", first_team_id);
        Ok(first_team_id.to_string())
    }

    /// **MÉTODO DE DIAGNÓSTICO: Test Connection** - Testa a conexão completa
    pub async fn test_connection(&self) -> AuthResult<Value> {
        log::info!("🧪 Testando conexão completa com ClickUp...");
        
        // 1. Testa health check
        let health = self.health_check().await?;
        if !health {
            return Err(AuthError::api_error("Health check falhou"));
        }

        // 2. Obtém informações do usuário
        let user = self.get_authorized_user().await?;
        let username = user
            .get("user")
            .and_then(|u| u.get("username"))
            .and_then(|u| u.as_str())
            .unwrap_or("unknown");

        // 3. Obtém informações das equipes
        let teams = self.get_authorized_teams().await?;
        let teams_count = teams
            .get("teams")
            .and_then(|t| t.as_array())
            .map(|t| t.len())
            .unwrap_or(0);

        log::info!("✅ Conexão testada com sucesso:");
        log::info!("  👤 Usuário: {}", username);
        log::info!("  👥 Equipes: {}", teams_count);

        // Retorna resumo da conexão
        Ok(json!({
            "status": "success",
            "user": user,
            "teams": teams,
            "health": health,
            "timestamp": chrono::Utc::now().to_rfc3339()
        }))
    }

    /// **MÉTODO DE CONFIGURAÇÃO: Get API Info** - Retorna informações sobre a configuração da API
    pub fn get_api_info(&self) -> Value {
        json!({
            "base_url": self.base_url,
            "has_token": !self.token.is_empty(),
            "token_length": self.token.len(),
            "token_preview": if self.token.len() > 10 {
                format!("{}...{}", &self.token[..4], &self.token[self.token.len()-4..])
            } else {
                "***".to_string()
            }
        })
    }

    /// **MÉTODO DE VALIDAÇÃO: Validate Token Format** - Valida se o token tem formato correto
    pub fn validate_token_format(&self) -> bool {
        // Token do ClickUp normalmente tem formato específico
        // Por enquanto, validação básica
        !self.token.is_empty() && self.token.len() > 20
    }

    /// **MÉTODO DE UTILIDADE: Get Rate Limit Info** - Obtém informações sobre rate limiting
    /// Nota: ClickUp não expõe endpoint público para isso, mas podemos simular
    pub async fn get_rate_limit_info(&self) -> AuthResult<Value> {
        log::info!("📊 Verificando informações de rate limit...");
        
        // Faz uma requisição simples para capturar headers de rate limit
        let url = self.build_url("user");
        
        let response = self.client
            .get(&url)
            .send()
            .await
            .map_err(|e| AuthError::network_error(&format!("Falha na requisição: {}", e)))?;

        let headers = response.headers();
        let mut rate_limit_info = json!({
            "timestamp": chrono::Utc::now().to_rfc3339()
        });

        // Captura headers comuns de rate limiting
        if let Some(remaining) = headers.get("X-RateLimit-Remaining") {
            if let Ok(value) = remaining.to_str() {
                rate_limit_info["remaining"] = json!(value);
            }
        }

        if let Some(reset) = headers.get("X-RateLimit-Reset") {
            if let Ok(value) = reset.to_str() {
                rate_limit_info["reset"] = json!(value);
            }
        }

        if let Some(limit) = headers.get("X-RateLimit-Limit") {
            if let Ok(value) = limit.to_str() {
                rate_limit_info["limit"] = json!(value);
            }
        }

        Ok(rate_limit_info)
    }

    /// **MÉTODO DE PESQUISA: Search Entity** - Pesquisa entidades por nome com cache
    /// Busca spaces, folders, lists ou tasks por nome e retorna informações detalhadas
    /// Resultados são armazenados em cache por 3 horas
    pub async fn search_entity(
        &self,
        name: &str,
        entity_type: EntityType,
        team_id: Option<String>,
    ) -> AuthResult<SearchResult> {
        log::info!("🔍 Pesquisando {} com nome: {}",
            match entity_type {
                EntityType::Space => "space",
                EntityType::Folder => "folder",
                EntityType::List => "list",
                EntityType::Task => "task",
            },
            name
        );

        // Obtém o team_id do ambiente se não fornecido
        let team_id = match team_id {
            Some(id) => id,
            None => {
                // Tenta obter do .env primeiro
                if let Ok(env_id) = std::env::var("CLICKUP_TEAM_ID") {
                    env_id
                } else {
                    // Se não houver no .env, obtém o primeiro workspace
                    self.get_first_workspace_id().await?
                }
            }
        };

        // Cria a chave do cache
        let cache_key = CacheKey {
            name: name.to_lowercase(),
            entity_type: entity_type.clone(),
            team_id: team_id.clone(),
        };

        // Verifica o cache primeiro
        {
            let cache = self.cache.read().unwrap();
            if let Some(entry) = cache.get(&cache_key) {
                let now = chrono::Utc::now();
                let cache_age = now - entry.cached_at;
                
                // Cache válido por 3 horas
                if cache_age < chrono::Duration::hours(3) {
                    log::info!("✅ Resultado encontrado em cache (idade: {} minutos)",
                        cache_age.num_minutes()
                    );
                    let mut result = entry.result.clone();
                    result.cached_at = Some(entry.cached_at);
                    return Ok(result);
                }
            }
        }

        // Se não estiver em cache, busca na API
        log::info!("📡 Buscando na API do ClickUp...");
        
        let result = match entity_type {
            EntityType::Space => self.search_spaces(&team_id, name).await?,
            EntityType::Folder => self.search_folders(&team_id, name).await?,
            EntityType::List => self.search_lists(&team_id, name).await?,
            EntityType::Task => self.search_tasks(&team_id, name).await?,
        };

        // Armazena no cache se encontrou resultados
        if result.found {
            let mut cache = self.cache.write().unwrap();
            let now = chrono::Utc::now();
            
            cache.insert(cache_key, CacheEntry {
                result: result.clone(),
                cached_at: now,
            });
            
            log::info!("💾 Resultado armazenado em cache");
        }

        Ok(result)
    }

    /// Pesquisa spaces em um team
    async fn search_spaces(&self, team_id: &str, name: &str) -> AuthResult<SearchResult> {
        let endpoint = format!("team/{}/space", team_id);
        let response = self.get(&endpoint).await?;
        
        let spaces = response
            .get("spaces")
            .and_then(|s| s.as_array())
            .ok_or_else(|| AuthError::parse_error("Campo 'spaces' não encontrado"))?;

        let name_lower = name.to_lowercase();
        let mut items = Vec::new();

        for space in spaces {
            let space_name = space.get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("");
            
            if space_name.to_lowercase().contains(&name_lower) {
                let space_id = space.get("id")
                    .and_then(|i| i.as_str())
                    .unwrap_or("");
                
                items.push(EntityItem {
                    id: space_id.to_string(),
                    name: space_name.to_string(),
                    url: format!("https://app.clickup.com/{}/home", team_id),
                    entity_type: EntityType::Space,
                    parent_id: Some(team_id.to_string()),
                    parent_name: Some("Team".to_string()),
                });
            }
        }

        Ok(SearchResult {
            found: !items.is_empty(),
            entity_type: EntityType::Space,
            items,
            cached_at: None,
        })
    }

    /// Pesquisa folders em todos os spaces de um team
    async fn search_folders(&self, team_id: &str, name: &str) -> AuthResult<SearchResult> {
        // Primeiro obtém todos os spaces
        let spaces_endpoint = format!("team/{}/space", team_id);
        let spaces_response = self.get(&spaces_endpoint).await?;
        
        let spaces = spaces_response
            .get("spaces")
            .and_then(|s| s.as_array())
            .ok_or_else(|| AuthError::parse_error("Campo 'spaces' não encontrado"))?;

        let name_lower = name.to_lowercase();
        let mut items = Vec::new();

        // Para cada space, busca folders
        for space in spaces {
            let space_id = space.get("id")
                .and_then(|i| i.as_str())
                .unwrap_or("");
            let space_name = space.get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("");
            
            let folders_endpoint = format!("space/{}/folder", space_id);
            
            if let Ok(folders_response) = self.get(&folders_endpoint).await {
                if let Some(folders) = folders_response.get("folders").and_then(|f| f.as_array()) {
                    for folder in folders {
                        let folder_name = folder.get("name")
                            .and_then(|n| n.as_str())
                            .unwrap_or("");
                        
                        if folder_name.to_lowercase().contains(&name_lower) {
                            let folder_id = folder.get("id")
                                .and_then(|i| i.as_str())
                                .unwrap_or("");
                            
                            items.push(EntityItem {
                                id: folder_id.to_string(),
                                name: folder_name.to_string(),
                                url: format!("https://app.clickup.com/{}/{}/f/{}",
                                    team_id, space_id, folder_id
                                ),
                                entity_type: EntityType::Folder,
                                parent_id: Some(space_id.to_string()),
                                parent_name: Some(space_name.to_string()),
                            });
                        }
                    }
                }
            }
        }

        Ok(SearchResult {
            found: !items.is_empty(),
            entity_type: EntityType::Folder,
            items,
            cached_at: None,
        })
    }

    /// Pesquisa lists em todos os spaces e folders de um team
    async fn search_lists(&self, team_id: &str, name: &str) -> AuthResult<SearchResult> {
        // Primeiro obtém todos os spaces
        let spaces_endpoint = format!("team/{}/space", team_id);
        let spaces_response = self.get(&spaces_endpoint).await?;
        
        let spaces = spaces_response
            .get("spaces")
            .and_then(|s| s.as_array())
            .ok_or_else(|| AuthError::parse_error("Campo 'spaces' não encontrado"))?;

        let name_lower = name.to_lowercase();
        let mut items = Vec::new();

        for space in spaces {
            let space_id = space.get("id")
                .and_then(|i| i.as_str())
                .unwrap_or("");
            let space_name = space.get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("");
            
            // Busca lists diretamente no space
            let lists_endpoint = format!("space/{}/list", space_id);
            
            if let Ok(lists_response) = self.get(&lists_endpoint).await {
                if let Some(lists) = lists_response.get("lists").and_then(|l| l.as_array()) {
                    for list in lists {
                        let list_name = list.get("name")
                            .and_then(|n| n.as_str())
                            .unwrap_or("");
                        
                        if list_name.to_lowercase().contains(&name_lower) {
                            let list_id = list.get("id")
                                .and_then(|i| i.as_str())
                                .unwrap_or("");
                            
                            items.push(EntityItem {
                                id: list_id.to_string(),
                                name: list_name.to_string(),
                                url: format!("https://app.clickup.com/{}/{}/l/li/{}",
                                    team_id, space_id, list_id
                                ),
                                entity_type: EntityType::List,
                                parent_id: Some(space_id.to_string()),
                                parent_name: Some(space_name.to_string()),
                            });
                        }
                    }
                }
            }

            // Busca lists dentro de folders
            let folders_endpoint = format!("space/{}/folder", space_id);
            
            if let Ok(folders_response) = self.get(&folders_endpoint).await {
                if let Some(folders) = folders_response.get("folders").and_then(|f| f.as_array()) {
                    for folder in folders {
                        let folder_id = folder.get("id")
                            .and_then(|i| i.as_str())
                            .unwrap_or("");
                        let folder_name = folder.get("name")
                            .and_then(|n| n.as_str())
                            .unwrap_or("");
                        
                        let folder_lists_endpoint = format!("folder/{}/list", folder_id);
                        
                        if let Ok(folder_lists_response) = self.get(&folder_lists_endpoint).await {
                            if let Some(lists) = folder_lists_response.get("lists").and_then(|l| l.as_array()) {
                                for list in lists {
                                    let list_name = list.get("name")
                                        .and_then(|n| n.as_str())
                                        .unwrap_or("");
                                    
                                    if list_name.to_lowercase().contains(&name_lower) {
                                        let list_id = list.get("id")
                                            .and_then(|i| i.as_str())
                                            .unwrap_or("");
                                        
                                        items.push(EntityItem {
                                            id: list_id.to_string(),
                                            name: list_name.to_string(),
                                            url: format!("https://app.clickup.com/{}/{}/l/li/{}",
                                                team_id, space_id, list_id
                                            ),
                                            entity_type: EntityType::List,
                                            parent_id: Some(folder_id.to_string()),
                                            parent_name: Some(folder_name.to_string()),
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(SearchResult {
            found: !items.is_empty(),
            entity_type: EntityType::List,
            items,
            cached_at: None,
        })
    }

    /// Pesquisa tasks usando o endpoint de search
    async fn search_tasks(&self, team_id: &str, name: &str) -> AuthResult<SearchResult> {
        let endpoint = format!("team/{}/task", team_id);
        let mut url = self.build_url(&endpoint);
        url.push_str(&format!("?name={}", urlencoding::encode(name)));
        
        log::debug!("GET {}", url);
        
        let response = self.client
            .get(&url)
            .send()
            .await
            .map_err(|e| AuthError::network_error(&format!("Falha na requisição GET: {}", e)))?;

        let status = response.status();
        let response_text = response
            .text()
            .await
            .map_err(|e| AuthError::network_error(&format!("Falha ao ler resposta: {}", e)))?;

        log::debug!("Response status: {}, body: {}", status, response_text);

        if !status.is_success() {
            return Err(self.handle_error_response(status.as_u16(), &response_text));
        }

        let response_json: Value = serde_json::from_str(&response_text)
            .map_err(|e| AuthError::parse_error(&format!("Falha ao parsear JSON: {}", e)))?;
        
        let empty_vec = Vec::new();
        let tasks = response_json
            .get("tasks")
            .and_then(|t| t.as_array())
            .unwrap_or(&empty_vec);

        let mut items = Vec::new();

        for task in tasks {
            let task_name = task.get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("");
            let task_id = task.get("id")
                .and_then(|i| i.as_str())
                .unwrap_or("");
            
            // Obtém informações da lista para construir a URL
            let list_id = task.get("list")
                .and_then(|l| l.get("id"))
                .and_then(|i| i.as_str())
                .unwrap_or("");
            let list_name = task.get("list")
                .and_then(|l| l.get("name"))
                .and_then(|n| n.as_str())
                .unwrap_or("");
            
            items.push(EntityItem {
                id: task_id.to_string(),
                name: task_name.to_string(),
                url: format!("https://app.clickup.com/t/{}", task_id),
                entity_type: EntityType::Task,
                parent_id: Some(list_id.to_string()),
                parent_name: Some(list_name.to_string()),
            });
        }

        Ok(SearchResult {
            found: !items.is_empty(),
            entity_type: EntityType::Task,
            items,
            cached_at: None,
        })
    }

    /// Limpa o cache de pesquisas
    pub fn clear_search_cache(&self) {
        let mut cache = self.cache.write().unwrap();
        cache.clear();
        log::info!("🗑️ Cache de pesquisas limpo");
    }

    /// Obtém estatísticas do cache
    pub fn get_cache_stats(&self) -> Value {
        let cache = self.cache.read().unwrap();
        let now = chrono::Utc::now();
        
        let mut stats = json!({
            "total_entries": cache.len(),
            "entries": []
        });

        for (key, entry) in cache.iter() {
            let age = now - entry.cached_at;
            stats["entries"].as_array_mut().unwrap().push(json!({
                "name": key.name,
                "entity_type": format!("{:?}", key.entity_type),
                "team_id": key.team_id,
                "cached_at": entry.cached_at.to_rfc3339(),
                "age_minutes": age.num_minutes(),
                "items_count": entry.result.items.len()
            }));
        }

        stats
    }

    /// **MÉTODO DE CRIAÇÃO: Create Task** - Cria uma nova task em uma lista
    /// POST /list/{list_id}/task
    ///
    /// # Parâmetros
    /// * `list_id` - ID da lista onde criar a task
    /// * `request` - Dados da task a ser criada
    ///
    /// # Exemplo
    /// ```no_run
    /// let task = CreateTaskRequest::builder("Nova Task")
    ///     .description("Descrição da task")
    ///     .priority(TaskPriority::High)
    ///     .custom_field_text("field_uuid", "valor")
    ///     .custom_field_date("date_field_uuid", timestamp)
    ///     .build();
    ///
    /// let response = client.create_task("list_id", task).await?;
    /// ```
    pub async fn create_task(
        &self,
        list_id: &str,
        request: CreateTaskRequest,
    ) -> AuthResult<TaskResponse> {
        log::info!("📝 Criando nova task na lista: {}", list_id);
        log::debug!("Task data: {:?}", request);

        let endpoint = format!("list/{}/task", list_id);
        let response = self.post(&endpoint, &request).await?;

        // Parseia a resposta para TaskResponse
        serde_json::from_value(response)
            .map_err(|e| AuthError::parse_error(&format!("Falha ao parsear resposta da task: {}", e)))
    }

    /// **MÉTODO DE CRIAÇÃO SIMPLIFICADO: Create Simple Task** - Cria uma task básica
    pub async fn create_simple_task(
        &self,
        list_id: &str,
        name: &str,
        description: Option<&str>,
    ) -> AuthResult<TaskResponse> {
        let mut request = CreateTaskRequest::new(name);
        if let Some(desc) = description {
            request.content = Some(desc.to_string());
        }

        self.create_task(list_id, request).await
    }

    /// **MÉTODO AUXILIAR: Get Lists** - Obtém listas de um space ou folder
    /// GET /space/{space_id}/list ou GET /folder/{folder_id}/list
    pub async fn get_lists(&self, space_id: Option<&str>, folder_id: Option<&str>) -> AuthResult<Value> {
        let endpoint = if let Some(folder) = folder_id {
            log::info!("📋 Obtendo listas do folder: {}", folder);
            format!("folder/{}/list", folder)
        } else if let Some(space) = space_id {
            log::info!("📋 Obtendo listas do space: {}", space);
            format!("space/{}/list", space)
        } else {
            return Err(AuthError::api_error("Forneça space_id ou folder_id"));
        };

        self.get(&endpoint).await
    }

    /// **MÉTODO AUXILIAR: Get Custom Fields** - Obtém campos personalizados de uma lista
    /// GET /list/{list_id}/field
    pub async fn get_custom_fields(&self, list_id: &str) -> AuthResult<Value> {
        log::info!("🔧 Obtendo campos personalizados da lista: {}", list_id);
        let endpoint = format!("list/{}/field", list_id);
        self.get(&endpoint).await
    }

    /// **MÉTODO AUXILIAR: Get Spaces** - Obtém spaces de um team
    /// GET /team/{team_id}/space
    pub async fn get_spaces(&self, team_id: &str) -> AuthResult<Value> {
        log::info!("🏢 Obtendo spaces do team: {}", team_id);
        let endpoint = format!("team/{}/space", team_id);
        self.get(&endpoint).await
    }

    /// **MÉTODO AUXILIAR: Get Folders** - Obtém folders de um space
    /// GET /space/{space_id}/folder
    pub async fn get_folders(&self, space_id: &str) -> AuthResult<Value> {
        log::info!("📁 Obtendo folders do space: {}", space_id);
        let endpoint = format!("space/{}/folder", space_id);
        self.get(&endpoint).await
    }

    /// **MÉTODO AUXILIAR: Get Task** - Obtém uma task específica
    /// GET /task/{task_id}
    pub async fn get_task(&self, task_id: &str) -> AuthResult<Value> {
        log::info!("📌 Obtendo task: {}", task_id);
        let endpoint = format!("task/{}", task_id);
        self.get(&endpoint).await
    }

    /// **MÉTODO AUXILIAR: Update Task Custom Field** - Atualiza um campo personalizado
    /// POST /task/{task_id}/field/{field_id}
    ///
    /// Nota: ClickUp só permite atualizar um campo por vez
    pub async fn update_custom_field(
        &self,
        task_id: &str,
        field_id: &str,
        value: CustomFieldValue,
    ) -> AuthResult<Value> {
        log::info!("🔄 Atualizando campo {} da task {}", field_id, task_id);

        let endpoint = format!("task/{}/field/{}", task_id, field_id);
        let body = json!({ "value": value });

        self.post(&endpoint, &body).await
    }

    /// **MÉTODO DE BUSCA: Find List by Name** - Busca uma lista por nome
    /// Útil para encontrar o list_id necessário para criar tasks
    pub async fn find_list_by_name(
        &self,
        name: &str,
        team_id: Option<String>,
    ) -> AuthResult<Option<String>> {
        log::info!("🔍 Buscando lista com nome: {}", name);

        let result = self.search_entity(name, EntityType::List, team_id).await?;

        if result.found && !result.items.is_empty() {
            // Retorna o ID da primeira lista encontrada
            Ok(Some(result.items[0].id.clone()))
        } else {
            Ok(None)
        }
    }

    /// **MÉTODO HELPER: Create Task with Auto-Find List** - Cria task buscando a lista por nome
    /// Este método é útil quando você sabe o nome da lista mas não o ID
    pub async fn create_task_in_list_by_name(
        &self,
        list_name: &str,
        task: CreateTaskRequest,
        team_id: Option<String>,
    ) -> AuthResult<TaskResponse> {
        // Busca a lista por nome
        let list_id = self.find_list_by_name(list_name, team_id).await?
            .ok_or_else(|| AuthError::api_error(&format!("Lista '{}' não encontrada", list_name)))?;

        // Cria a task na lista encontrada
        self.create_task(&list_id, task).await
    }
}

#[cfg(test)]
mod search_tests {
    use super::*;
    use crate::config::EnvManager;

    #[tokio::test]
    async fn test_search_entity_with_cache() {
        // Só roda se houver token configurado
        if let Ok(env_manager) = EnvManager::load() {
            if let Some(token) = EnvManager::get_access_token() {
                let client = ClickUpClient::new(
                    token,
                    env_manager.api_base_url
                );
                
                // Obtém o team_id do ambiente ou usa o primeiro disponível
                let team_id = if let Ok(teams) = client.get_authorized_teams().await {
                    teams
                        .get("teams")
                        .and_then(|t| t.as_array())
                        .and_then(|arr| arr.first())
                        .and_then(|team| team.get("id"))
                        .and_then(|id| id.as_str())
                        .unwrap_or("test_team")
                        .to_string()
                } else {
                    "test_team".to_string()
                };
                
                println!("🧪 Testando pesquisa de entidades com team_id: {}", team_id);
                
                // Teste 1: Pesquisar por space
                println!("\n📍 Teste 1: Pesquisando por space...");
                match client.search_entity(
                    "Test Space",
                    EntityType::Space,
                    Some(team_id.clone())
                ).await {
                    Ok(result) => {
                        println!("  ✅ Pesquisa concluída");
                        println!("  📊 Encontrado: {}", result.found);
                        println!("  📦 Total de items: {}", result.items.len());
                        
                        for item in &result.items {
                            println!("{} (ID: {})", item.name, item.id);
                            println!("      URL: {}", item.url);
                        }
                        
                        if result.cached_at.is_some() {
                            println!("  ⚡ Resultado veio do cache!");
                        } else {
                            println!("  🔄 Resultado veio da API");
                        }
                    },
                    Err(e) => println!("  ⚠️ Erro na pesquisa: {}", e)
                }
                
                // Teste 2: Pesquisar por folder
                println!("\n📍 Teste 2: Pesquisando por folder...");
                match client.search_entity(
                    "Dev",
                    EntityType::Folder,
                    Some(team_id.clone())
                ).await {
                    Ok(result) => {
                        println!("  ✅ Pesquisa concluída");
                        println!("  📊 Encontrado: {}", result.found);
                        println!("  📦 Total de folders: {}", result.items.len());
                        
                        for item in &result.items {
                            println!("{} (ID: {})", item.name, item.id);
                            println!("      URL: {}", item.url);
                            if let Some(parent) = &item.parent_name {
                                println!("      Parent: {}", parent);
                            }
                        }
                    },
                    Err(e) => println!("  ⚠️ Erro na pesquisa: {}", e)
                }
                
                // Teste 3: Pesquisar por list
                println!("\n📍 Teste 3: Pesquisando por list...");
                match client.search_entity(
                    "TODO",
                    EntityType::List,
                    Some(team_id.clone())
                ).await {
                    Ok(result) => {
                        println!("  ✅ Pesquisa concluída");
                        println!("  📊 Encontrado: {}", result.found);
                        println!("  📦 Total de lists: {}", result.items.len());
                        
                        for item in &result.items {
                            println!("{} (ID: {})", item.name, item.id);
                            println!("      URL: {}", item.url);
                            if let Some(parent) = &item.parent_name {
                                println!("      Parent: {}", parent);
                            }
                        }
                    },
                    Err(e) => println!("  ⚠️ Erro na pesquisa: {}", e)
                }
                
                // Teste 4: Pesquisar por task
                println!("\n📍 Teste 4: Pesquisando por task...");
                match client.search_entity(
                    "Test Task",
                    EntityType::Task,
                    Some(team_id.clone())
                ).await {
                    Ok(result) => {
                        println!("  ✅ Pesquisa concluída");
                        println!("  📊 Encontrado: {}", result.found);
                        println!("  📦 Total de tasks: {}", result.items.len());
                        
                        for item in &result.items {
                            println!("{} (ID: {})", item.name, item.id);
                            println!("      URL: {}", item.url);
                            if let Some(parent) = &item.parent_name {
                                println!("      List: {}", parent);
                            }
                        }
                    },
                    Err(e) => println!("  ⚠️ Erro na pesquisa: {}", e)
                }
                
                // Teste 5: Segunda pesquisa do mesmo item (deve vir do cache)
                println!("\n📍 Teste 5: Testando cache (pesquisando novamente o mesmo space)...");
                let start = std::time::Instant::now();
                
                match client.search_entity(
                    "Test Space",
                    EntityType::Space,
                    Some(team_id.clone())
                ).await {
                    Ok(result) => {
                        let duration = start.elapsed();
                        println!("  ✅ Pesquisa concluída em {:?}", duration);
                        
                        if result.cached_at.is_some() {
                            println!("  ⚡ Resultado veio do cache (como esperado)!");
                            println!("  ⏱️ Cache criado em: {}",
                                result.cached_at.unwrap().to_rfc3339());
                        } else {
                            println!("  ⚠️ Resultado não veio do cache (inesperado)");
                        }
                    },
                    Err(e) => println!("  ⚠️ Erro na pesquisa: {}", e)
                }
                
                // Teste 6: Obter estatísticas do cache
                println!("\n📍 Teste 6: Estatísticas do cache...");
                let stats = client.get_cache_stats();
                println!("  📊 Cache stats: {}", serde_json::to_string_pretty(&stats).unwrap());
                
                // Teste 7: Limpar cache
                println!("\n📍 Teste 7: Limpando cache...");
                client.clear_search_cache();
                let stats_after = client.get_cache_stats();
                println!("  📊 Cache após limpeza: {} entradas",
                    stats_after["total_entries"].as_u64().unwrap_or(0));
            } else {
                println!("⚠️ Token não configurado - pulando testes de pesquisa");
            }
        } else {
            println!("⚠️ Configuração não encontrada - pulando testes de pesquisa");
        }
    }

    #[tokio::test]
    async fn test_search_entity_from_env() {
        // Teste usando team_id do .env
        if let Ok(env_manager) = EnvManager::load() {
            if let Some(token) = EnvManager::get_access_token() {
                let client = ClickUpClient::new(
                    token,
                    env_manager.api_base_url
                );
                
                // O team_id pode vir do .env ou ser None para usar o primeiro disponível
                println!("\n🧪 Testando pesquisa com team_id do ambiente");
                
                // Teste sem especificar team_id (usa o primeiro disponível)
                match client.search_entity(
                    "Test",
                    EntityType::Space,
                    None  // Não especifica team_id
                ).await {
                    Ok(result) => {
                        println!("✅ Pesquisa sem team_id funcionou");
                        println!("  Encontrado: {}", result.found);
                        println!("  Items: {}", result.items.len());
                    },
                    Err(e) => println!("⚠️ Erro: {}", e)
                }
            }
        }
    }
}

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

    #[test]
    fn test_client_creation() {
        // Carrega configurações reais do ambiente
        if let Ok(env_manager) = EnvManager::load() {
            let token = EnvManager::get_access_token().unwrap_or_else(|| "test_token".to_string());
            let client = ClickUpClient::new(
                token.clone(),
                env_manager.api_base_url.clone()
            );

            assert_eq!(client.token, token);
            assert_eq!(client.base_url, env_manager.api_base_url.trim_end_matches('/'));
        }
    }

    #[test]
    fn test_url_building() {
        // Usa URL da configuração ou padrão
        let base_url = std::env::var("CLICKUP_API_BASE_URL")
            .unwrap_or_else(|_| "https://api.clickup.com/api/v2".to_string());
        
        let client = ClickUpClient::new(
            "test_token".to_string(),
            format!("{}/", base_url) // Com barra no final para testar normalização
        );
        
        let url1 = client.build_url("user");
        let url2 = client.build_url("/team");
        
        assert_eq!(url1, format!("{}/user", base_url.trim_end_matches('/')));
        assert_eq!(url2, format!("{}/team", base_url.trim_end_matches('/')));
    }

    #[test]
    fn test_token_validation() {
        // Testa com token real se disponível
        if let Ok(env_manager) = EnvManager::load() {
            if let Some(token) = EnvManager::get_access_token() {
                let client = ClickUpClient::new(
                    token,
                    env_manager.api_base_url
                );

                // Token real deve passar na validação
                assert!(client.validate_token_format());
            }
        }

        // Testa token inválido
        let client_invalid = ClickUpClient::new(
            "short".to_string(),
            "https://api.clickup.com/api/v2".to_string()
        );

        assert!(!client_invalid.validate_token_format());
    }

    #[test]
    fn test_api_info() {
        // Usa configurações reais se disponíveis
        let env_manager = EnvManager::load().unwrap_or_else(|_| {
            // Mock para testes sem configuração
            EnvManager {
                client_id: "test".to_string(),
                client_secret: "test".to_string(),
                redirect_uri: "http://localhost:8888/callback".to_string(),
                api_base_url: "https://api.clickup.com/api/v2".to_string(),
                callback_port: 8888,
                environment: crate::config::env::Environment::Development,
            }
        });

        let token = EnvManager::get_access_token()
            .unwrap_or_else(|| "test_token_with_more_than_20_chars".to_string());

        let client = ClickUpClient::new(
            token.clone(),
            env_manager.api_base_url.clone()
        );

        let info = client.get_api_info();

        assert_eq!(info["base_url"], env_manager.api_base_url.trim_end_matches('/'));
        assert_eq!(info["has_token"], !token.is_empty());

        if !token.is_empty() {
            assert!(info["token_length"].as_u64().unwrap() > 0);
        }

        if token.len() > 10 {
            assert!(info["token_preview"].as_str().unwrap().contains("..."));
        }
    }
    
    #[tokio::test]
    async fn test_health_check_with_real_token() {
        // Só roda se houver token configurado
        if let Ok(env_manager) = EnvManager::load() {
            if let Some(token) = EnvManager::get_access_token() {
                let client = ClickUpClient::new(
                    token,
                    env_manager.api_base_url
                );

                // Tenta fazer health check real
                match client.health_check().await {
                    Ok(result) => {
                        // Se temos token, esperamos true ou false (não erro)
                        assert!(result == true || result == false);
                    },
                    Err(_) => {
                        // Erro de rede ou configuração é aceitável em testes
                    }
                }
            }
        }
    }
}