libfmp 1.3.1

Async, typed Rust client for the Financial Modeling Prep 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
//! TipRanks add-on endpoint and query contracts.
//!
//! Every route requires the provider's paid TipRanks add-on, so each descriptor
//! carries [`AccessRequirement::NamedAddOn`] with the name `TipRanks`. The
//! search and point-in-time routes also record the documented three-year
//! ratings-history window as a [`ConditionalPlanRequirement`] for the
//! Enterprise plan. Both facts are advisory metadata: the client neither gates
//! nor rejects a request locally. The Python binding exposes the same seven
//! methods under `client.tipranks`.

use crate::{
    Client, Result,
    endpoints::{
        EndpointSpec, QueryEncoder, QueryParameters,
        metadata::{
            AccessRequirement, ConditionalPlanRequirement, EndpointBounds, EndpointMetadata,
            PlanCondition,
        },
    },
    responses::tipranks::{
        TipRanksAnalystProfile, TipRanksAnalystSummary, TipRanksFirmSummary,
        TipRanksPointInTimeRating, TipRanksRatingSearchResult, TipRanksSymbolSummary,
    },
    types::{Date, Limit, Page, SearchTerm, Ticker, TipRanksExpertUid},
};

/// Optional filters and pagination for the TipRanks analyst ratings search.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TipRanksSearchQuery {
    expert_uid: Option<TipRanksExpertUid>,
    symbol: Option<Ticker>,
    from: Option<Date>,
    to: Option<Date>,
    limit: Option<Limit>,
    page: Option<Page>,
    nonadjusted: Option<bool>,
}

impl TipRanksSearchQuery {
    /// Creates a search without undocumented filters, pagination, or flag defaults.
    pub const fn new() -> Self {
        Self {
            expert_uid: None,
            symbol: None,
            from: None,
            to: None,
            limit: None,
            page: None,
            nonadjusted: None,
        }
    }

    /// Restricts results to one stable TipRanks analyst identifier.
    pub fn with_expert_uid(mut self, expert_uid: TipRanksExpertUid) -> Self {
        self.expert_uid = Some(expert_uid);
        self
    }

    /// Restricts results to one ticker.
    pub fn with_symbol(mut self, symbol: Ticker) -> Self {
        self.symbol = Some(symbol);
        self
    }

    /// Sets the optional independent recommendation start date.
    pub const fn with_from(mut self, from: Date) -> Self {
        self.from = Some(from);
        self
    }

    /// Sets the optional independent recommendation end date.
    pub const fn with_to(mut self, to: Date) -> Self {
        self.to = Some(to);
        self
    }

    /// Sets the optional provider result limit.
    ///
    /// The endpoint's documented inclusive maximum is exposed through its
    /// [`EndpointMetadata`] rather than rejected while building a request.
    pub const fn with_limit(mut self, limit: Limit) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Sets the optional provider page index.
    pub const fn with_page(mut self, page: Page) -> Self {
        self.page = Some(page);
        self
    }

    /// Sets native-currency price targets when true, preserving explicit false.
    pub const fn with_nonadjusted(mut self, nonadjusted: bool) -> Self {
        self.nonadjusted = Some(nonadjusted);
        self
    }

    /// Borrows the optional stable TipRanks analyst identifier.
    pub fn expert_uid(&self) -> Option<&TipRanksExpertUid> {
        self.expert_uid.as_ref()
    }

    /// Borrows the optional ticker restriction.
    pub fn symbol(&self) -> Option<&Ticker> {
        self.symbol.as_ref()
    }

    /// Returns the optional independent recommendation start date.
    pub const fn from(&self) -> Option<Date> {
        self.from
    }

    /// Returns the optional independent recommendation end date.
    pub const fn to(&self) -> Option<Date> {
        self.to
    }

    /// Returns the optional provider result limit.
    pub const fn limit(&self) -> Option<Limit> {
        self.limit
    }

    /// Returns the optional provider page index.
    pub const fn page(&self) -> Option<Page> {
        self.page
    }

    /// Returns the optional native-currency flag.
    pub const fn nonadjusted(&self) -> Option<bool> {
        self.nonadjusted
    }
}

impl QueryParameters for TipRanksSearchQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.optional("expertUID", self.expert_uid.as_ref());
        encoder.optional("symbol", self.symbol.as_ref());
        encoder.optional("from", self.from);
        encoder.optional("to", self.to);
        encoder.optional("limit", self.limit);
        encoder.optional("page", self.page);
        encoder.optional("nonadjusted", self.nonadjusted);
    }
}

/// Required symbol and optional snapshot controls for point-in-time ratings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PointInTimeRatingsBySymbolQuery {
    symbol: Ticker,
    date: Option<Date>,
    limit: Option<Limit>,
    page: Option<Page>,
    nonadjusted: Option<bool>,
}

impl PointInTimeRatingsBySymbolQuery {
    /// Creates a current snapshot request for one ticker without injected defaults.
    pub const fn new(symbol: Ticker) -> Self {
        Self {
            symbol,
            date: None,
            limit: None,
            page: None,
            nonadjusted: None,
        }
    }

    /// Sets the optional historical snapshot date.
    pub const fn with_date(mut self, date: Date) -> Self {
        self.date = Some(date);
        self
    }

    /// Sets the optional provider result limit.
    pub const fn with_limit(mut self, limit: Limit) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Sets the optional provider page index.
    pub const fn with_page(mut self, page: Page) -> Self {
        self.page = Some(page);
        self
    }

    /// Sets native-currency price targets when true, preserving explicit false.
    pub const fn with_nonadjusted(mut self, nonadjusted: bool) -> Self {
        self.nonadjusted = Some(nonadjusted);
        self
    }

    /// Borrows the required ticker.
    pub const fn symbol(&self) -> &Ticker {
        &self.symbol
    }

    /// Returns the optional historical snapshot date.
    pub const fn date(&self) -> Option<Date> {
        self.date
    }

    /// Returns the optional provider result limit.
    pub const fn limit(&self) -> Option<Limit> {
        self.limit
    }

    /// Returns the optional provider page index.
    pub const fn page(&self) -> Option<Page> {
        self.page
    }

    /// Returns the optional native-currency flag.
    pub const fn nonadjusted(&self) -> Option<bool> {
        self.nonadjusted
    }
}

impl QueryParameters for PointInTimeRatingsBySymbolQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.required("symbol", &self.symbol);
        encoder.optional("date", self.date);
        encoder.optional("limit", self.limit);
        encoder.optional("page", self.page);
        encoder.optional("nonadjusted", self.nonadjusted);
    }
}

/// Optional analyst selectors and snapshot controls for point-in-time ratings.
///
/// The provider prose calls `expertUID` a supplied selector, while its parameter
/// table marks neither selector as required. This query therefore preserves the
/// documented wire surface without imposing a local selector requirement or XOR.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PointInTimeRatingsByAnalystQuery {
    expert_uid: Option<TipRanksExpertUid>,
    analyst_name: Option<SearchTerm>,
    date: Option<Date>,
    limit: Option<Limit>,
    page: Option<Page>,
    nonadjusted: Option<bool>,
}

impl PointInTimeRatingsByAnalystQuery {
    /// Creates a request with no selector, pagination, date, or flag defaults.
    pub const fn new() -> Self {
        Self {
            expert_uid: None,
            analyst_name: None,
            date: None,
            limit: None,
            page: None,
            nonadjusted: None,
        }
    }

    /// Selects a stable TipRanks analyst identifier.
    pub fn with_expert_uid(mut self, expert_uid: TipRanksExpertUid) -> Self {
        self.expert_uid = Some(expert_uid);
        self
    }

    /// Selects an analyst by the provider's open name text.
    pub fn with_analyst_name(mut self, analyst_name: SearchTerm) -> Self {
        self.analyst_name = Some(analyst_name);
        self
    }

    /// Sets the optional historical snapshot date.
    pub const fn with_date(mut self, date: Date) -> Self {
        self.date = Some(date);
        self
    }

    /// Sets the optional provider result limit.
    pub const fn with_limit(mut self, limit: Limit) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Sets the optional provider page index.
    pub const fn with_page(mut self, page: Page) -> Self {
        self.page = Some(page);
        self
    }

    /// Sets native-currency price targets when true, preserving explicit false.
    pub const fn with_nonadjusted(mut self, nonadjusted: bool) -> Self {
        self.nonadjusted = Some(nonadjusted);
        self
    }

    /// Borrows the optional stable TipRanks analyst identifier.
    pub fn expert_uid(&self) -> Option<&TipRanksExpertUid> {
        self.expert_uid.as_ref()
    }

    /// Borrows the optional analyst name selector.
    pub fn analyst_name(&self) -> Option<&SearchTerm> {
        self.analyst_name.as_ref()
    }

    /// Returns the optional historical snapshot date.
    pub const fn date(&self) -> Option<Date> {
        self.date
    }

    /// Returns the optional provider result limit.
    pub const fn limit(&self) -> Option<Limit> {
        self.limit
    }

    /// Returns the optional provider page index.
    pub const fn page(&self) -> Option<Page> {
        self.page
    }

    /// Returns the optional native-currency flag.
    pub const fn nonadjusted(&self) -> Option<bool> {
        self.nonadjusted
    }
}

impl QueryParameters for PointInTimeRatingsByAnalystQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.optional("expertUID", self.expert_uid.as_ref());
        encoder.optional("analystName", self.analyst_name.as_ref());
        encoder.optional("date", self.date);
        encoder.optional("limit", self.limit);
        encoder.optional("page", self.page);
        encoder.optional("nonadjusted", self.nonadjusted);
    }
}

/// Required ticker and optional independent date bounds for a ratings summary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TipRanksSymbolSummaryQuery {
    symbol: Ticker,
    from: Option<Date>,
    to: Option<Date>,
}

impl TipRanksSymbolSummaryQuery {
    /// Creates a summary request without injecting the provider's date defaults.
    pub const fn new(symbol: Ticker) -> Self {
        Self {
            symbol,
            from: None,
            to: None,
        }
    }

    /// Sets the optional independent recommendation start date.
    pub const fn with_from(mut self, from: Date) -> Self {
        self.from = Some(from);
        self
    }

    /// Sets the optional independent recommendation end date.
    pub const fn with_to(mut self, to: Date) -> Self {
        self.to = Some(to);
        self
    }

    /// Borrows the required ticker.
    pub const fn symbol(&self) -> &Ticker {
        &self.symbol
    }

    /// Returns the optional independent recommendation start date.
    pub const fn from(&self) -> Option<Date> {
        self.from
    }

    /// Returns the optional independent recommendation end date.
    pub const fn to(&self) -> Option<Date> {
        self.to
    }
}

impl QueryParameters for TipRanksSymbolSummaryQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.required("symbol", &self.symbol);
        encoder.optional("from", self.from);
        encoder.optional("to", self.to);
    }
}

/// Required analyst identifier and optional independent date bounds for a summary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TipRanksAnalystSummaryQuery {
    expert_uid: TipRanksExpertUid,
    from: Option<Date>,
    to: Option<Date>,
}

impl TipRanksAnalystSummaryQuery {
    /// Creates a summary request without injecting the provider's date defaults.
    pub const fn new(expert_uid: TipRanksExpertUid) -> Self {
        Self {
            expert_uid,
            from: None,
            to: None,
        }
    }

    /// Sets the optional independent recommendation start date.
    pub const fn with_from(mut self, from: Date) -> Self {
        self.from = Some(from);
        self
    }

    /// Sets the optional independent recommendation end date.
    pub const fn with_to(mut self, to: Date) -> Self {
        self.to = Some(to);
        self
    }

    /// Borrows the required stable TipRanks analyst identifier.
    pub const fn expert_uid(&self) -> &TipRanksExpertUid {
        &self.expert_uid
    }

    /// Returns the optional independent recommendation start date.
    pub const fn from(&self) -> Option<Date> {
        self.from
    }

    /// Returns the optional independent recommendation end date.
    pub const fn to(&self) -> Option<Date> {
        self.to
    }
}

impl QueryParameters for TipRanksAnalystSummaryQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.required("expertUID", &self.expert_uid);
        encoder.optional("from", self.from);
        encoder.optional("to", self.to);
    }
}

/// Required exact firm text and optional independent date bounds for a summary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TipRanksFirmSummaryQuery {
    firm_name: SearchTerm,
    from: Option<Date>,
    to: Option<Date>,
}

impl TipRanksFirmSummaryQuery {
    /// Creates a summary request without injecting the provider's date defaults.
    pub const fn new(firm_name: SearchTerm) -> Self {
        Self {
            firm_name,
            from: None,
            to: None,
        }
    }

    /// Sets the optional independent recommendation start date.
    pub const fn with_from(mut self, from: Date) -> Self {
        self.from = Some(from);
        self
    }

    /// Sets the optional independent recommendation end date.
    pub const fn with_to(mut self, to: Date) -> Self {
        self.to = Some(to);
        self
    }

    /// Borrows the required exact firm-name text.
    pub const fn firm_name(&self) -> &SearchTerm {
        &self.firm_name
    }

    /// Returns the optional independent recommendation start date.
    pub const fn from(&self) -> Option<Date> {
        self.from
    }

    /// Returns the optional independent recommendation end date.
    pub const fn to(&self) -> Option<Date> {
        self.to
    }
}

impl QueryParameters for TipRanksFirmSummaryQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.required("firmName", &self.firm_name);
        encoder.optional("from", self.from);
        encoder.optional("to", self.to);
    }
}

/// Optional pagination and name filters for the TipRanks analyst directory.
///
/// The provider's parameter table lists `page`, `limit`, and `firmName`, while
/// its prose repeatedly documents an exact `analystName` lookup. This query
/// preserves both documented surfaces without requiring or coupling selectors.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TipRanksAnalystsQuery {
    page: Option<Page>,
    limit: Option<Limit>,
    firm_name: Option<SearchTerm>,
    analyst_name: Option<SearchTerm>,
}

impl TipRanksAnalystsQuery {
    /// Creates a directory request without filters or pagination defaults.
    pub const fn new() -> Self {
        Self {
            page: None,
            limit: None,
            firm_name: None,
            analyst_name: None,
        }
    }

    /// Sets the optional provider page index.
    pub const fn with_page(mut self, page: Page) -> Self {
        self.page = Some(page);
        self
    }

    /// Sets the optional provider result limit.
    pub const fn with_limit(mut self, limit: Limit) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Filters the directory by the provider's open firm-name text.
    pub fn with_firm_name(mut self, firm_name: SearchTerm) -> Self {
        self.firm_name = Some(firm_name);
        self
    }

    /// Looks up an analyst by the exact published name documented in prose.
    pub fn with_analyst_name(mut self, analyst_name: SearchTerm) -> Self {
        self.analyst_name = Some(analyst_name);
        self
    }

    /// Returns the optional provider page index.
    pub const fn page(&self) -> Option<Page> {
        self.page
    }

    /// Returns the optional provider result limit.
    pub const fn limit(&self) -> Option<Limit> {
        self.limit
    }

    /// Borrows the optional firm-name filter.
    pub fn firm_name(&self) -> Option<&SearchTerm> {
        self.firm_name.as_ref()
    }

    /// Borrows the optional exact analyst-name lookup.
    pub fn analyst_name(&self) -> Option<&SearchTerm> {
        self.analyst_name.as_ref()
    }
}

impl QueryParameters for TipRanksAnalystsQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.optional("page", self.page);
        encoder.optional("limit", self.limit);
        encoder.optional("firmName", self.firm_name.as_ref());
        encoder.optional("analystName", self.analyst_name.as_ref());
    }
}

const TIPRANKS_SEARCH_METADATA: EndpointMetadata = EndpointMetadata::new()
    .with_access(AccessRequirement::NamedAddOn("TipRanks"))
    .with_conditional_plan(ConditionalPlanRequirement::new(
        "Enterprise",
        PlanCondition::HistoryOlderThanYears(3),
    ))
    .with_bounds(
        EndpointBounds::new()
            .with_limit(5_000)
            .with_response_rows(5_000),
    );

const TIPRANKS_SUMMARY_METADATA: EndpointMetadata = EndpointMetadata::new()
    .with_access(AccessRequirement::NamedAddOn("TipRanks"))
    .with_bounds(EndpointBounds::new().with_response_rows(1));

const TIPRANKS_DIRECTORY_METADATA: EndpointMetadata =
    EndpointMetadata::new().with_access(AccessRequirement::NamedAddOn("TipRanks"));

/// Describes `GET tipranks-search` without binding a transport.
pub fn search_tipranks_ratings(
    query: TipRanksSearchQuery,
) -> EndpointSpec<TipRanksSearchQuery, Vec<TipRanksRatingSearchResult>> {
    EndpointSpec::get("tipranks-search", "tipranks-search", query)
        .with_metadata(TIPRANKS_SEARCH_METADATA)
}

/// Describes `GET tipranks-pit-symbol` without binding a transport.
pub fn tipranks_point_in_time_ratings_by_symbol(
    query: PointInTimeRatingsBySymbolQuery,
) -> EndpointSpec<PointInTimeRatingsBySymbolQuery, Vec<TipRanksPointInTimeRating>> {
    EndpointSpec::get("tipranks-pit-symbol", "tipranks-pit-symbol", query)
        .with_metadata(TIPRANKS_SEARCH_METADATA)
}

/// Describes `GET tipranks-pit-analyst` without binding a transport.
pub fn tipranks_point_in_time_ratings_by_analyst(
    query: PointInTimeRatingsByAnalystQuery,
) -> EndpointSpec<PointInTimeRatingsByAnalystQuery, Vec<TipRanksPointInTimeRating>> {
    EndpointSpec::get("tipranks-pit-analyst", "tipranks-pit-analyst", query)
        .with_metadata(TIPRANKS_SEARCH_METADATA)
}

/// Describes `GET tipranks-symbol-summary` without binding a transport.
pub fn tipranks_symbol_summary(
    query: TipRanksSymbolSummaryQuery,
) -> EndpointSpec<TipRanksSymbolSummaryQuery, Vec<TipRanksSymbolSummary>> {
    EndpointSpec::get("tipranks-symbol-summary", "tipranks-symbol-summary", query)
        .with_metadata(TIPRANKS_SUMMARY_METADATA)
}

/// Describes `GET tipranks-analyst-summary` without binding a transport.
pub fn tipranks_analyst_summary(
    query: TipRanksAnalystSummaryQuery,
) -> EndpointSpec<TipRanksAnalystSummaryQuery, Vec<TipRanksAnalystSummary>> {
    EndpointSpec::get(
        "tipranks-analyst-summary",
        "tipranks-analyst-summary",
        query,
    )
    .with_metadata(TIPRANKS_SUMMARY_METADATA)
}

/// Describes `GET tipranks-firm-summary` without binding a transport.
pub fn tipranks_firm_summary(
    query: TipRanksFirmSummaryQuery,
) -> EndpointSpec<TipRanksFirmSummaryQuery, Vec<TipRanksFirmSummary>> {
    EndpointSpec::get("tipranks-firm-summary", "tipranks-firm-summary", query)
        .with_metadata(TIPRANKS_SUMMARY_METADATA)
}

/// Describes `GET tipranks-analysts` without binding a transport.
pub fn tipranks_analysts(
    query: TipRanksAnalystsQuery,
) -> EndpointSpec<TipRanksAnalystsQuery, Vec<TipRanksAnalystProfile>> {
    EndpointSpec::get("tipranks-analysts", "tipranks-analysts", query)
        .with_metadata(TIPRANKS_DIRECTORY_METADATA)
}

impl Client {
    /// Retrieves individual analyst ratings from the TipRanks add-on.
    pub async fn search_tipranks_ratings(
        &self,
        query: TipRanksSearchQuery,
    ) -> Result<Vec<TipRanksRatingSearchResult>> {
        self.execute(&search_tipranks_ratings(query)).await
    }

    /// Retrieves a ticker's analyst ratings as of an optional snapshot date.
    pub async fn tipranks_point_in_time_ratings_by_symbol(
        &self,
        query: PointInTimeRatingsBySymbolQuery,
    ) -> Result<Vec<TipRanksPointInTimeRating>> {
        self.execute(&tipranks_point_in_time_ratings_by_symbol(query))
            .await
    }

    /// Retrieves an analyst's active coverage as of an optional snapshot date.
    pub async fn tipranks_point_in_time_ratings_by_analyst(
        &self,
        query: PointInTimeRatingsByAnalystQuery,
    ) -> Result<Vec<TipRanksPointInTimeRating>> {
        self.execute(&tipranks_point_in_time_ratings_by_analyst(query))
            .await
    }

    /// Retrieves a ticker's aggregate TipRanks ratings summary.
    pub async fn tipranks_symbol_summary(
        &self,
        query: TipRanksSymbolSummaryQuery,
    ) -> Result<Vec<TipRanksSymbolSummary>> {
        self.execute(&tipranks_symbol_summary(query)).await
    }

    /// Retrieves an analyst's aggregate TipRanks ratings summary.
    pub async fn tipranks_analyst_summary(
        &self,
        query: TipRanksAnalystSummaryQuery,
    ) -> Result<Vec<TipRanksAnalystSummary>> {
        self.execute(&tipranks_analyst_summary(query)).await
    }

    /// Retrieves a firm's aggregate TipRanks ratings summary.
    pub async fn tipranks_firm_summary(
        &self,
        query: TipRanksFirmSummaryQuery,
    ) -> Result<Vec<TipRanksFirmSummary>> {
        self.execute(&tipranks_firm_summary(query)).await
    }

    /// Retrieves analyst profiles from the TipRanks directory.
    pub async fn tipranks_analysts(
        &self,
        query: TipRanksAnalystsQuery,
    ) -> Result<Vec<TipRanksAnalystProfile>> {
        self.execute(&tipranks_analysts(query)).await
    }
}

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

    fn encoded(query: &impl QueryParameters) -> Vec<(String, String)> {
        let mut pairs = Vec::new();
        let mut visitor = |name: &str, value: &str| {
            pairs.push((name.to_owned(), value.to_owned()));
        };
        query.encode(&mut QueryEncoder::new(&mut visitor));
        pairs
    }

    #[test]
    fn query_omits_every_absent_value_and_preserves_exact_full_order() {
        assert!(encoded(&TipRanksSearchQuery::new()).is_empty());

        let from = Date::parse("2025-06-10").unwrap();
        let to = Date::parse("2026-06-10").unwrap();
        let query = TipRanksSearchQuery::new()
            .with_expert_uid(TipRanksExpertUid::new("expert / one").unwrap())
            .with_symbol(Ticker::new("AAPL").unwrap())
            .with_from(from)
            .with_to(to)
            .with_limit(Limit(5_000))
            .with_page(Page(0))
            .with_nonadjusted(false);

        assert_eq!(query.expert_uid().unwrap().as_str(), "expert / one");
        assert_eq!(query.symbol().unwrap().as_str(), "AAPL");
        assert_eq!(query.from(), Some(from));
        assert_eq!(query.to(), Some(to));
        assert_eq!(query.limit(), Some(Limit(5_000)));
        assert_eq!(query.page(), Some(Page(0)));
        assert_eq!(query.nonadjusted(), Some(false));
        assert_eq!(
            encoded(&query),
            [
                ("expertUID".into(), "expert / one".into()),
                ("symbol".into(), "AAPL".into()),
                ("from".into(), "2025-06-10".into()),
                ("to".into(), "2026-06-10".into()),
                ("limit".into(), "5000".into()),
                ("page".into(), "0".into()),
                ("nonadjusted".into(), "false".into()),
            ]
        );
    }

    #[test]
    fn dates_are_independent_and_flags_preserve_both_boolean_states() {
        let from = Date::parse("2025-06-10").unwrap();
        let to = Date::parse("2026-06-10").unwrap();

        assert_eq!(
            encoded(&TipRanksSearchQuery::new().with_from(from)),
            [("from".into(), "2025-06-10".into())]
        );
        assert_eq!(
            encoded(
                &TipRanksSearchQuery::new()
                    .with_to(to)
                    .with_limit(Limit(0))
                    .with_page(Page(0))
                    .with_nonadjusted(true)
            ),
            [
                ("to".into(), "2026-06-10".into()),
                ("limit".into(), "0".into()),
                ("page".into(), "0".into()),
                ("nonadjusted".into(), "true".into())
            ]
        );
    }

    #[test]
    fn point_in_time_symbol_query_preserves_required_first_and_optional_order() {
        let query = PointInTimeRatingsBySymbolQuery::new(Ticker::new("BRK.B").unwrap())
            .with_date(Date::parse("2026-06-10").unwrap())
            .with_limit(Limit(5_000))
            .with_page(Page(0))
            .with_nonadjusted(false);

        assert_eq!(query.symbol().as_str(), "BRK.B");
        assert_eq!(query.date(), Some(Date::parse("2026-06-10").unwrap()));
        assert_eq!(query.limit(), Some(Limit(5_000)));
        assert_eq!(query.page(), Some(Page(0)));
        assert_eq!(query.nonadjusted(), Some(false));
        assert_eq!(
            encoded(&query),
            [
                ("symbol".into(), "BRK.B".into()),
                ("date".into(), "2026-06-10".into()),
                ("limit".into(), "5000".into()),
                ("page".into(), "0".into()),
                ("nonadjusted".into(), "false".into()),
            ]
        );
        assert_eq!(
            encoded(&PointInTimeRatingsBySymbolQuery::new(
                Ticker::new("AAPL").unwrap()
            )),
            [("symbol".into(), "AAPL".into())]
        );
    }

    #[test]
    fn point_in_time_analyst_query_omits_defaults_and_allows_both_selectors() {
        assert!(encoded(&PointInTimeRatingsByAnalystQuery::new()).is_empty());
        assert!(encoded(&PointInTimeRatingsByAnalystQuery::default()).is_empty());

        let query = PointInTimeRatingsByAnalystQuery::new()
            .with_expert_uid(TipRanksExpertUid::new("0001").unwrap())
            .with_analyst_name(SearchTerm::new("Analyst / Name").unwrap())
            .with_date(Date::parse("2026-06-10").unwrap())
            .with_limit(Limit(5_000))
            .with_page(Page(0))
            .with_nonadjusted(false);

        assert_eq!(query.expert_uid().unwrap().as_str(), "0001");
        assert_eq!(query.analyst_name().unwrap().as_str(), "Analyst / Name");
        assert_eq!(query.date(), Some(Date::parse("2026-06-10").unwrap()));
        assert_eq!(query.limit(), Some(Limit(5_000)));
        assert_eq!(query.page(), Some(Page(0)));
        assert_eq!(query.nonadjusted(), Some(false));
        assert_eq!(
            encoded(&query),
            [
                ("expertUID".into(), "0001".into()),
                ("analystName".into(), "Analyst / Name".into()),
                ("date".into(), "2026-06-10".into()),
                ("limit".into(), "5000".into()),
                ("page".into(), "0".into()),
                ("nonadjusted".into(), "false".into()),
            ]
        );
    }

    #[test]
    fn summary_queries_emit_required_identity_then_independent_optional_dates() {
        let from = Date::parse("2025-06-10").unwrap();
        let to = Date::parse("2026-06-10").unwrap();

        let symbol = TipRanksSymbolSummaryQuery::new(Ticker::new("BRK.B").unwrap())
            .with_from(from)
            .with_to(to);
        assert_eq!(symbol.symbol().as_str(), "BRK.B");
        assert_eq!(symbol.from(), Some(from));
        assert_eq!(symbol.to(), Some(to));
        assert_eq!(
            encoded(&symbol),
            [
                ("symbol".into(), "BRK.B".into()),
                ("from".into(), "2025-06-10".into()),
                ("to".into(), "2026-06-10".into()),
            ]
        );

        let analyst =
            TipRanksAnalystSummaryQuery::new(TipRanksExpertUid::new("expert / one").unwrap())
                .with_to(to);
        assert_eq!(analyst.expert_uid().as_str(), "expert / one");
        assert_eq!(analyst.from(), None);
        assert_eq!(analyst.to(), Some(to));
        assert_eq!(
            encoded(&analyst),
            [
                ("expertUID".into(), "expert / one".into()),
                ("to".into(), "2026-06-10".into()),
            ]
        );

        let firm = TipRanksFirmSummaryQuery::new(SearchTerm::new("Morgan Stanley / Asia").unwrap())
            .with_from(from);
        assert_eq!(firm.firm_name().as_str(), "Morgan Stanley / Asia");
        assert_eq!(firm.from(), Some(from));
        assert_eq!(firm.to(), None);
        assert_eq!(
            encoded(&firm),
            [
                ("firmName".into(), "Morgan Stanley / Asia".into()),
                ("from".into(), "2025-06-10".into()),
            ]
        );

        assert_eq!(
            encoded(&TipRanksSymbolSummaryQuery::new(
                Ticker::new("AAPL").unwrap()
            )),
            [("symbol".into(), "AAPL".into())]
        );
    }

    #[test]
    fn analyst_directory_query_omits_defaults_and_preserves_reconciled_wire_order() {
        assert!(encoded(&TipRanksAnalystsQuery::new()).is_empty());
        assert!(encoded(&TipRanksAnalystsQuery::default()).is_empty());

        let query = TipRanksAnalystsQuery::new()
            .with_page(Page(0))
            .with_limit(Limit(1_000))
            .with_firm_name(SearchTerm::new("Morgan Stanley / Asia").unwrap())
            .with_analyst_name(SearchTerm::new("Andrew Marok / Exact").unwrap());

        assert_eq!(query.page(), Some(Page(0)));
        assert_eq!(query.limit(), Some(Limit(1_000)));
        assert_eq!(query.firm_name().unwrap().as_str(), "Morgan Stanley / Asia");
        assert_eq!(
            query.analyst_name().unwrap().as_str(),
            "Andrew Marok / Exact"
        );
        assert_eq!(
            encoded(&query),
            [
                ("page".into(), "0".into()),
                ("limit".into(), "1000".into()),
                ("firmName".into(), "Morgan Stanley / Asia".into()),
                ("analystName".into(), "Andrew Marok / Exact".into()),
            ]
        );
    }
}