lago-types 0.1.25

Types definitions for Lago API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
use serde::{Deserialize, Serialize};

use crate::models::PaginationParams;

use crate::filters::{common::ListFilters, invoice::InvoiceFilters};

/// Request parameters for listing invoices.
///
/// This struct combines pagination parameters and invoice-specific filters
/// to build a comprehensive request for retrieving invoice lists.
#[derive(Debug, Clone)]
pub struct ListInvoicesRequest {
    pub pagination: PaginationParams,
    pub filters: InvoiceFilters,
    /// Search by id, number, customer name, external_id or email.
    pub search_term: Option<String>,
}

impl ListInvoicesRequest {
    /// Creates a new empty list invoices request.
    ///
    /// # Returns
    /// A new `ListInvoicesRequest` instance with default pagination and no filters.
    pub fn new() -> Self {
        Self {
            pagination: PaginationParams::default(),
            filters: InvoiceFilters::default(),
            search_term: None,
        }
    }

    /// Sets the pagination parameters for the request.
    ///
    /// # Arguments
    /// * `pagination` - The pagination parameters to use
    ///
    /// # Returns
    /// The modified request instance for method chaining.
    pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
        self.pagination = pagination;
        self
    }

    /// Sets the invoice filters for the request.
    ///
    /// # Arguments
    /// * `filters` - The invoice filters to apply
    ///
    /// # Returns
    /// The modified request instance for method chaining.
    pub fn with_filters(mut self, filters: InvoiceFilters) -> Self {
        self.filters = filters;
        self
    }

    /// Sets the search term for the request.
    ///
    /// Search by id, number, customer name, external_id or email.
    ///
    /// # Arguments
    /// * `term` - The search term
    ///
    /// # Returns
    /// The modified request instance for method chaining.
    pub fn with_search_term(mut self, term: String) -> Self {
        self.search_term = Some(term);
        self
    }

    /// Converts the request parameters into HTTP query parameters.
    ///
    /// # Returns
    /// A vector of query parameter tuples containing both pagination and filter criteria.
    pub fn to_query_params(&self) -> Vec<(&str, String)> {
        let mut params = self.pagination.to_query_params();
        params.extend(self.filters.to_query_params());

        if let Some(ref term) = self.search_term {
            params.push(("search_term", term.clone()));
        }

        params
    }
}

impl Default for ListInvoicesRequest {
    fn default() -> Self {
        Self::new()
    }
}

/// Request parameters for retrieving a specific invoice.
///
/// This struct contains the identifier needed to fetch a single invoice
/// from the API.
#[derive(Debug, Clone)]
pub struct GetInvoiceRequest {
    pub invoice_id: String,
}

impl GetInvoiceRequest {
    /// Creates a new get invoice request.
    ///
    /// # Arguments
    /// * `invoice_id` - The unique identifier of the invoice to retrieve
    ///
    /// # Returns
    /// A new `GetInvoiceRequest` instance with the specified invoice ID.
    pub fn new(invoice_id: String) -> Self {
        Self { invoice_id }
    }
}

/// Request parameters for deleting a draft invoice.
///
/// This struct contains the identifier needed to permanently delete a
/// `draft` invoice from the API.
#[derive(Debug, Clone)]
pub struct DeleteInvoiceRequest {
    pub invoice_id: String,
}

impl DeleteInvoiceRequest {
    /// Creates a new delete invoice request.
    ///
    /// # Arguments
    /// * `invoice_id` - The unique identifier of the draft invoice to delete
    ///
    /// # Returns
    /// A new `DeleteInvoiceRequest` instance with the specified invoice ID.
    pub fn new(invoice_id: String) -> Self {
        Self { invoice_id }
    }
}

/// Billing time determines when recurring billing cycles occur.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BillingTime {
    /// Billing cycle based on the specific date the subscription started (billed fully).
    Anniversary,
    /// Billing cycle at the first day of the week/month/year (billed with proration).
    Calendar,
}

/// Customer information for invoice preview.
///
/// This struct contains customer data used when generating an invoice preview.
/// It can reference an existing customer via external_id or provide inline customer details.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct InvoicePreviewCustomer {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address_line1: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address_line2: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub city: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timezone: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tax_identification_number: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shipping_address: Option<InvoicePreviewAddress>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub integration_customers: Option<Vec<InvoicePreviewIntegrationCustomer>>,
}

impl InvoicePreviewCustomer {
    /// Creates a new invoice preview customer with an external ID.
    ///
    /// # Arguments
    /// * `external_id` - The unique external identifier for the customer
    ///
    /// # Returns
    /// A new `InvoicePreviewCustomer` instance
    pub fn with_external_id(external_id: String) -> Self {
        Self {
            external_id: Some(external_id),
            ..Default::default()
        }
    }

    /// Creates a new empty invoice preview customer for inline definition.
    ///
    /// # Returns
    /// A new empty `InvoicePreviewCustomer` instance
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }

    pub fn with_address(
        mut self,
        address_line1: String,
        address_line2: Option<String>,
        city: Option<String>,
        state: Option<String>,
        country: Option<String>,
    ) -> Self {
        self.address_line1 = Some(address_line1);
        self.address_line2 = address_line2;
        self.city = city;
        self.state = state;
        self.country = country;
        self
    }

    pub fn with_currency(mut self, currency: String) -> Self {
        self.currency = Some(currency);
        self
    }

    pub fn with_timezone(mut self, timezone: String) -> Self {
        self.timezone = Some(timezone);
        self
    }

    pub fn with_tax_identification_number(mut self, tax_id: String) -> Self {
        self.tax_identification_number = Some(tax_id);
        self
    }

    pub fn with_shipping_address(mut self, address: InvoicePreviewAddress) -> Self {
        self.shipping_address = Some(address);
        self
    }

    pub fn with_integration_customers(
        mut self,
        integrations: Vec<InvoicePreviewIntegrationCustomer>,
    ) -> Self {
        self.integration_customers = Some(integrations);
        self
    }
}

/// Address information for invoice preview.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct InvoicePreviewAddress {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address_line1: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address_line2: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub city: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub zipcode: Option<String>,
}

/// Integration customer for tax providers like Anrok.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvoicePreviewIntegrationCustomer {
    pub integration_type: String,
    pub integration_code: String,
}

/// Coupon information for invoice preview.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvoicePreviewCoupon {
    pub code: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coupon_type: Option<InvoicePreviewCouponType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount_cents: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount_currency: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub percentage_rate: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency_duration: Option<i32>,
}

impl InvoicePreviewCoupon {
    /// Creates a new coupon with the required code.
    ///
    /// # Arguments
    /// * `code` - The unique code for the coupon
    ///
    /// # Returns
    /// A new `InvoicePreviewCoupon` instance
    pub fn new(code: String) -> Self {
        Self {
            code,
            name: None,
            coupon_type: None,
            amount_cents: None,
            amount_currency: None,
            percentage_rate: None,
            frequency_duration: None,
        }
    }

    pub fn with_name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }

    pub fn with_fixed_amount(mut self, amount_cents: i64, currency: String) -> Self {
        self.coupon_type = Some(InvoicePreviewCouponType::FixedAmount);
        self.amount_cents = Some(amount_cents);
        self.amount_currency = Some(currency);
        self
    }

    pub fn with_percentage(mut self, percentage_rate: String) -> Self {
        self.coupon_type = Some(InvoicePreviewCouponType::Percentage);
        self.percentage_rate = Some(percentage_rate);
        self
    }

    pub fn with_frequency_duration(mut self, duration: i32) -> Self {
        self.frequency_duration = Some(duration);
        self
    }
}

/// Type of coupon discount.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InvoicePreviewCouponType {
    FixedAmount,
    Percentage,
}

/// Subscription information for invoice preview.
///
/// Used to specify existing subscriptions to include in the preview,
/// with optional plan changes or termination.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvoicePreviewSubscriptions {
    pub external_ids: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan_code: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub terminated_at: Option<String>,
}

impl InvoicePreviewSubscriptions {
    /// Creates a new subscriptions object with the required external IDs.
    ///
    /// # Arguments
    /// * `external_ids` - The external identifiers of the subscriptions
    ///
    /// # Returns
    /// A new `InvoicePreviewSubscriptions` instance
    pub fn new(external_ids: Vec<String>) -> Self {
        Self {
            external_ids,
            plan_code: None,
            terminated_at: None,
        }
    }

    pub fn with_plan_code(mut self, plan_code: String) -> Self {
        self.plan_code = Some(plan_code);
        self
    }

    pub fn with_terminated_at(mut self, terminated_at: String) -> Self {
        self.terminated_at = Some(terminated_at);
        self
    }
}

/// Input parameters for previewing an invoice.
///
/// This struct contains all the information needed to generate an invoice preview
/// without actually creating the invoice.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct InvoicePreviewInput {
    pub customer: InvoicePreviewCustomer,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan_code: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subscription_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub billing_time: Option<BillingTime>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coupons: Option<Vec<InvoicePreviewCoupon>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subscriptions: Option<InvoicePreviewSubscriptions>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub billing_entity_code: Option<String>,
}

impl InvoicePreviewInput {
    /// Creates a new invoice preview input with a customer reference.
    ///
    /// # Arguments
    /// * `customer` - The customer information for the preview
    ///
    /// # Returns
    /// A new `InvoicePreviewInput` instance
    pub fn new(customer: InvoicePreviewCustomer) -> Self {
        Self {
            customer,
            ..Default::default()
        }
    }

    /// Creates an invoice preview for an existing customer.
    ///
    /// # Arguments
    /// * `external_id` - The external ID of the existing customer
    ///
    /// # Returns
    /// A new `InvoicePreviewInput` instance
    pub fn for_customer(external_id: String) -> Self {
        Self::new(InvoicePreviewCustomer::with_external_id(external_id))
    }

    pub fn with_plan_code(mut self, plan_code: String) -> Self {
        self.plan_code = Some(plan_code);
        self
    }

    pub fn with_subscription_at(mut self, subscription_at: String) -> Self {
        self.subscription_at = Some(subscription_at);
        self
    }

    pub fn with_billing_time(mut self, billing_time: BillingTime) -> Self {
        self.billing_time = Some(billing_time);
        self
    }

    pub fn with_coupons(mut self, coupons: Vec<InvoicePreviewCoupon>) -> Self {
        self.coupons = Some(coupons);
        self
    }

    pub fn with_subscriptions(mut self, subscriptions: InvoicePreviewSubscriptions) -> Self {
        self.subscriptions = Some(subscriptions);
        self
    }

    pub fn with_billing_entity_code(mut self, code: String) -> Self {
        self.billing_entity_code = Some(code);
        self
    }
}

/// Request parameters for previewing an invoice.
///
/// This struct wraps the invoice preview input in the expected API format.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvoicePreviewRequest {
    #[serde(flatten)]
    pub input: InvoicePreviewInput,
}

impl InvoicePreviewRequest {
    /// Creates a new preview invoice request.
    ///
    /// # Arguments
    /// * `input` - The invoice preview input data
    ///
    /// # Returns
    /// A new `InvoicePreviewRequest` instance
    pub fn new(input: InvoicePreviewInput) -> Self {
        Self { input }
    }
}

/// Fee input for creating a one-off invoice.
///
/// This struct contains the details for a single fee line item
/// to be included in a one-off invoice.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateInvoiceFeeInput {
    /// The code of the add-on to charge.
    pub add_on_code: String,
    /// The number of units to charge.
    pub units: f64,
    /// The price per unit in cents (optional, uses add-on default if not specified).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unit_amount_cents: Option<i64>,
    /// Optional description for the fee.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional tax codes to apply to this fee.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tax_codes: Option<Vec<String>>,
    /// Lower boundary of the service period covered by this fee.
    /// ISO 8601 datetime expressed in UTC, e.g. `2022-08-08T00:00:00Z`.
    /// Must be set together with `to_datetime` — the API rejects one without the other.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_datetime: Option<String>,
    /// Upper boundary of the service period covered by this fee.
    /// ISO 8601 datetime expressed in UTC, e.g. `2022-08-31T23:59:59Z`.
    /// Must be set together with `from_datetime` — the API rejects one without the other.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to_datetime: Option<String>,
}

impl CreateInvoiceFeeInput {
    /// Creates a new fee input with the required fields.
    ///
    /// # Arguments
    /// * `add_on_code` - The code of the add-on to charge
    /// * `units` - The number of units to charge
    ///
    /// # Returns
    /// A new `CreateInvoiceFeeInput` instance
    pub fn new(add_on_code: String, units: f64) -> Self {
        Self {
            add_on_code,
            units,
            unit_amount_cents: None,
            description: None,
            tax_codes: None,
            from_datetime: None,
            to_datetime: None,
        }
    }

    /// Sets a custom unit amount in cents.
    pub fn with_unit_amount_cents(mut self, unit_amount_cents: i64) -> Self {
        self.unit_amount_cents = Some(unit_amount_cents);
        self
    }

    /// Sets a description for the fee.
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }

    /// Sets the tax codes to apply to this fee.
    pub fn with_tax_codes(mut self, tax_codes: Vec<String>) -> Self {
        self.tax_codes = Some(tax_codes);
        self
    }

    /// Sets the service period covered by this fee.
    ///
    /// The API requires both boundaries together, so they are set as a pair.
    /// Both expect an ISO 8601 datetime expressed in UTC.
    ///
    /// # Arguments
    /// * `from_datetime` - Lower boundary, e.g. `2022-08-08T00:00:00Z`
    /// * `to_datetime` - Upper boundary, e.g. `2022-08-31T23:59:59Z`
    pub fn with_service_period(mut self, from_datetime: String, to_datetime: String) -> Self {
        self.from_datetime = Some(from_datetime);
        self.to_datetime = Some(to_datetime);
        self
    }
}

/// Input for creating a one-off invoice.
///
/// This struct contains all the information needed to create a one-off invoice
/// for a customer with add-on charges.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateInvoiceInput {
    /// The external customer ID to create the invoice for.
    pub external_customer_id: String,
    /// The currency for the invoice (ISO 4217 code).
    pub currency: String,
    /// The list of fees to include in the invoice.
    pub fees: Vec<CreateInvoiceFeeInput>,
    /// Optional purchase order number to associate with the invoice.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub purchase_order_number: Option<String>,
}

impl CreateInvoiceInput {
    /// Creates a new invoice input with the required fields.
    ///
    /// # Arguments
    /// * `external_customer_id` - The external ID of the customer
    /// * `currency` - The currency code (e.g., "USD")
    /// * `fees` - The list of fees to include
    ///
    /// # Returns
    /// A new `CreateInvoiceInput` instance
    pub fn new(
        external_customer_id: String,
        currency: String,
        fees: Vec<CreateInvoiceFeeInput>,
    ) -> Self {
        Self {
            external_customer_id,
            currency,
            fees,
            purchase_order_number: None,
        }
    }

    /// Sets the purchase order number to associate with the invoice.
    pub fn with_purchase_order_number(mut self, purchase_order_number: String) -> Self {
        self.purchase_order_number = Some(purchase_order_number);
        self
    }
}

/// Request for creating a one-off invoice.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateInvoiceRequest {
    /// The invoice input data.
    pub invoice: CreateInvoiceInput,
}

impl CreateInvoiceRequest {
    /// Creates a new create invoice request.
    ///
    /// # Arguments
    /// * `input` - The invoice input data
    ///
    /// # Returns
    /// A new `CreateInvoiceRequest` instance
    pub fn new(input: CreateInvoiceInput) -> Self {
        Self { invoice: input }
    }
}

/// Metadata input for updating an invoice.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateInvoiceMetadataInput {
    /// The ID of an existing metadata entry to update (optional for new entries).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The metadata key.
    pub key: String,
    /// The metadata value.
    pub value: String,
}

impl UpdateInvoiceMetadataInput {
    /// Creates a new metadata input.
    ///
    /// # Arguments
    /// * `key` - The metadata key
    /// * `value` - The metadata value
    ///
    /// # Returns
    /// A new `UpdateInvoiceMetadataInput` instance
    pub fn new(key: String, value: String) -> Self {
        Self {
            id: None,
            key,
            value,
        }
    }

    /// Creates a metadata input for updating an existing entry.
    ///
    /// # Arguments
    /// * `id` - The ID of the existing metadata entry
    /// * `key` - The metadata key
    /// * `value` - The metadata value
    ///
    /// # Returns
    /// A new `UpdateInvoiceMetadataInput` instance
    pub fn with_id(id: String, key: String, value: String) -> Self {
        Self {
            id: Some(id),
            key,
            value,
        }
    }
}

/// Input for updating an invoice.
///
/// This struct contains the fields that can be updated on an existing invoice.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UpdateInvoiceInput {
    /// The payment status of the invoice.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payment_status: Option<String>,
    /// Custom metadata for the invoice.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Vec<UpdateInvoiceMetadataInput>>,
}

impl UpdateInvoiceInput {
    /// Creates a new empty update invoice input.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the payment status.
    ///
    /// # Arguments
    /// * `payment_status` - The payment status (e.g., "succeeded", "failed", "pending")
    pub fn with_payment_status(mut self, payment_status: String) -> Self {
        self.payment_status = Some(payment_status);
        self
    }

    /// Sets the metadata entries.
    pub fn with_metadata(mut self, metadata: Vec<UpdateInvoiceMetadataInput>) -> Self {
        self.metadata = Some(metadata);
        self
    }
}

/// Request for updating an invoice.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateInvoiceRequest {
    /// The Lago ID of the invoice to update.
    #[serde(skip)]
    pub lago_id: String,
    /// The invoice update data.
    pub invoice: UpdateInvoiceInput,
}

impl UpdateInvoiceRequest {
    /// Creates a new update invoice request.
    ///
    /// # Arguments
    /// * `lago_id` - The Lago ID of the invoice to update
    /// * `input` - The update input data
    ///
    /// # Returns
    /// A new `UpdateInvoiceRequest` instance
    pub fn new(lago_id: String, input: UpdateInvoiceInput) -> Self {
        Self {
            lago_id,
            invoice: input,
        }
    }
}

/// Request parameters for listing a customer's invoices.
///
/// This struct combines the customer identifier with pagination and filters
/// to retrieve invoices for a specific customer.
#[derive(Debug, Clone)]
pub struct ListCustomerInvoicesRequest {
    /// The external customer ID.
    pub external_customer_id: String,
    /// Pagination parameters.
    pub pagination: PaginationParams,
    /// Invoice filters.
    pub filters: InvoiceFilters,
    /// Search by id, number, customer name, external_id or email.
    pub search_term: Option<String>,
}

impl ListCustomerInvoicesRequest {
    /// Creates a new list customer invoices request.
    ///
    /// # Arguments
    /// * `external_customer_id` - The external customer ID
    ///
    /// # Returns
    /// A new `ListCustomerInvoicesRequest` instance
    pub fn new(external_customer_id: String) -> Self {
        Self {
            external_customer_id,
            pagination: PaginationParams::default(),
            filters: InvoiceFilters::default(),
            search_term: None,
        }
    }

    /// Sets the pagination parameters.
    pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
        self.pagination = pagination;
        self
    }

    /// Sets the invoice filters.
    pub fn with_filters(mut self, filters: InvoiceFilters) -> Self {
        self.filters = filters;
        self
    }

    /// Sets the search term for the request.
    ///
    /// Search by id, number, customer name, external_id or email.
    pub fn with_search_term(mut self, term: String) -> Self {
        self.search_term = Some(term);
        self
    }

    /// Converts the request parameters into HTTP query parameters.
    pub fn to_query_params(&self) -> Vec<(&str, String)> {
        let mut params = self.pagination.to_query_params();
        params.extend(self.filters.to_query_params());

        if let Some(ref term) = self.search_term {
            params.push(("search_term", term.clone()));
        }

        params
    }
}

/// Request for refreshing a draft invoice.
///
/// Refreshing re-fetches the customer information and recomputes the taxes.
#[derive(Debug, Clone)]
pub struct RefreshInvoiceRequest {
    /// The Lago ID of the invoice to refresh.
    pub lago_id: String,
}

impl RefreshInvoiceRequest {
    /// Creates a new refresh invoice request.
    ///
    /// # Arguments
    /// * `lago_id` - The Lago ID of the invoice to refresh
    ///
    /// # Returns
    /// A new `RefreshInvoiceRequest` instance
    pub fn new(lago_id: String) -> Self {
        Self { lago_id }
    }
}

/// Request for downloading an invoice PDF.
///
/// This triggers the generation of the invoice PDF if not already generated.
#[derive(Debug, Clone)]
pub struct DownloadInvoiceRequest {
    /// The Lago ID of the invoice to download.
    pub lago_id: String,
}

impl DownloadInvoiceRequest {
    /// Creates a new download invoice request.
    ///
    /// # Arguments
    /// * `lago_id` - The Lago ID of the invoice to download
    ///
    /// # Returns
    /// A new `DownloadInvoiceRequest` instance
    pub fn new(lago_id: String) -> Self {
        Self { lago_id }
    }
}

/// Request for retrying a failed invoice finalization.
///
/// This retries the finalization process for invoices that failed during generation.
#[derive(Debug, Clone)]
pub struct RetryInvoiceRequest {
    /// The Lago ID of the invoice to retry.
    pub lago_id: String,
}

impl RetryInvoiceRequest {
    /// Creates a new retry invoice request.
    ///
    /// # Arguments
    /// * `lago_id` - The Lago ID of the invoice to retry
    ///
    /// # Returns
    /// A new `RetryInvoiceRequest` instance
    pub fn new(lago_id: String) -> Self {
        Self { lago_id }
    }
}

/// Request for retrying a failed invoice payment.
///
/// This resends the invoice for collection and retries the payment with the payment provider.
#[derive(Debug, Clone)]
pub struct RetryInvoicePaymentRequest {
    /// The Lago ID of the invoice to retry payment for.
    pub lago_id: String,
}

impl RetryInvoicePaymentRequest {
    /// Creates a new retry invoice payment request.
    ///
    /// # Arguments
    /// * `lago_id` - The Lago ID of the invoice to retry payment for
    ///
    /// # Returns
    /// A new `RetryInvoicePaymentRequest` instance
    pub fn new(lago_id: String) -> Self {
        Self { lago_id }
    }
}

/// Request for voiding a finalized invoice.
///
/// This voids a finalized invoice, changing its status to "voided".
/// Only finalized invoices can be voided.
#[derive(Debug, Clone)]
pub struct VoidInvoiceRequest {
    /// The Lago ID of the invoice to void.
    pub lago_id: String,
}

impl VoidInvoiceRequest {
    /// Creates a new void invoice request.
    ///
    /// # Arguments
    /// * `lago_id` - The Lago ID of the invoice to void
    ///
    /// # Returns
    /// A new `VoidInvoiceRequest` instance
    pub fn new(lago_id: String) -> Self {
        Self { lago_id }
    }
}

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

    #[test]
    fn service_period_is_omitted_when_unset() {
        let fee = CreateInvoiceFeeInput::new("setup_fee".to_string(), 1.0);
        let json = serde_json::to_value(&fee).unwrap();

        assert!(json.get("from_datetime").is_none());
        assert!(json.get("to_datetime").is_none());
    }

    #[test]
    fn service_period_is_serialized_when_set() {
        let fee = CreateInvoiceFeeInput::new("setup_fee".to_string(), 1.0).with_service_period(
            "2022-08-08T00:00:00Z".to_string(),
            "2022-08-31T23:59:59Z".to_string(),
        );
        let json = serde_json::to_value(&fee).unwrap();

        assert_eq!(json["from_datetime"], "2022-08-08T00:00:00Z");
        assert_eq!(json["to_datetime"], "2022-08-31T23:59:59Z");
    }
}