dns-orchestrator-provider 0.1.2

DNS provider abstraction library for multiple cloud platforms (Cloudflare, Aliyun, DNSPod, Huaweicloud)
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
use serde::{Deserialize, Serialize};

// ============ Pagination ============

/// Pagination parameters for list operations.
///
/// All list endpoints accept these parameters to control page-based pagination.
/// Pages are 1-indexed.
///
/// # Default
///
/// The default is `page = 1, page_size = 20`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PaginationParams {
    /// Page number (1-indexed).
    pub page: u32,
    /// Number of items per page.
    pub page_size: u32,
}

impl Default for PaginationParams {
    fn default() -> Self {
        Self {
            page: 1,
            page_size: 20,
        }
    }
}

impl PaginationParams {
    /// Clamp pagination values to valid ranges.
    ///
    /// - `page` is clamped to `>= 1`
    /// - `page_size` is clamped to `1..=max_page_size`
    #[must_use]
    pub fn validated(&self, max_page_size: u32) -> Self {
        Self {
            page: self.page.max(1),
            page_size: self.page_size.clamp(1, max_page_size),
        }
    }
}

/// Query parameters for DNS record listing, with optional search and filtering.
///
/// Extends basic pagination with keyword search and record type filtering.
///
/// # Default
///
/// The default is `page = 1, page_size = 20`, with no keyword or type filter.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RecordQueryParams {
    /// Page number (1-indexed).
    pub page: u32,
    /// Number of items per page.
    pub page_size: u32,
    /// Optional keyword to match against record names or values.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub keyword: Option<String>,
    /// Optional record type filter.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_type: Option<DnsRecordType>,
}

impl Default for RecordQueryParams {
    fn default() -> Self {
        Self {
            page: 1,
            page_size: 20,
            keyword: None,
            record_type: None,
        }
    }
}

impl RecordQueryParams {
    /// Convert to basic [`PaginationParams`], discarding search/filter fields.
    pub fn to_pagination(&self) -> PaginationParams {
        PaginationParams {
            page: self.page,
            page_size: self.page_size,
        }
    }

    /// Clamp pagination values to valid ranges.
    ///
    /// - `page` is clamped to `>= 1`
    /// - `page_size` is clamped to `1..=max_page_size`
    /// - `keyword` and `record_type` are preserved as-is
    #[must_use]
    pub fn validated(&self, max_page_size: u32) -> Self {
        Self {
            page: self.page.max(1),
            page_size: self.page_size.clamp(1, max_page_size),
            keyword: self.keyword.clone(),
            record_type: self.record_type.clone(),
        }
    }
}

/// A paginated response wrapper.
///
/// Returned by all list operations. Contains the current page of items
/// along with pagination metadata.
///
/// # Type Parameters
///
/// * `T` — The item type (e.g., [`ProviderDomain`], [`DnsRecord`]).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PaginatedResponse<T> {
    /// Items in the current page.
    pub items: Vec<T>,
    /// Current page number.
    pub page: u32,
    /// Page size used for this request.
    pub page_size: u32,
    /// Total number of items across all pages.
    pub total_count: u32,
    /// Whether there are more pages after this one.
    pub has_more: bool,
}

impl<T> PaginatedResponse<T> {
    /// Create a new paginated response, automatically computing [`has_more`](Self::has_more).
    pub fn new(items: Vec<T>, page: u32, page_size: u32, total_count: u32) -> Self {
        let has_more = (page * page_size) < total_count;
        Self {
            items,
            page,
            page_size,
            total_count,
            has_more,
        }
    }
}

// ============ Provider Types ============

/// Identifies which DNS provider implementation to use.
///
/// Each variant is gated behind its corresponding feature flag.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ProviderType {
    /// Cloudflare DNS. Requires feature `cloudflare`.
    #[cfg(feature = "cloudflare")]
    Cloudflare,
    /// Aliyun (China) DNS. Requires feature `aliyun`.
    #[cfg(feature = "aliyun")]
    Aliyun,
    /// Tencent Cloud `DNSPod`. Requires feature `dnspod`.
    #[cfg(feature = "dnspod")]
    Dnspod,
    /// Huawei Cloud DNS. Requires feature `huaweicloud`.
    #[cfg(feature = "huaweicloud")]
    Huaweicloud,
}

impl std::fmt::Display for ProviderType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            #[cfg(feature = "cloudflare")]
            Self::Cloudflare => write!(f, "cloudflare"),
            #[cfg(feature = "aliyun")]
            Self::Aliyun => write!(f, "aliyun"),
            #[cfg(feature = "dnspod")]
            Self::Dnspod => write!(f, "dnspod"),
            #[cfg(feature = "huaweicloud")]
            Self::Huaweicloud => write!(f, "huaweicloud"),
        }
    }
}

// ============ Domain Types ============

/// Status of a domain/zone within a DNS provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DomainStatus {
    /// Domain is active and resolving.
    Active,
    /// Domain is paused (not resolving).
    Paused,
    /// Domain is pending activation/verification.
    Pending,
    /// Domain is in an error state.
    Error,
    /// Status could not be determined.
    Unknown,
}

/// A domain (zone) managed by a DNS provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderDomain {
    /// Provider-specific domain/zone identifier.
    pub id: String,
    /// Domain name (e.g., `"example.com"`).
    pub name: String,
    /// Which provider manages this domain.
    pub provider: ProviderType,
    /// Current domain status.
    pub status: DomainStatus,
    /// Number of DNS records in this domain, if known.
    #[serde(rename = "recordCount", skip_serializing_if = "Option::is_none")]
    pub record_count: Option<u32>,
}

// ============ DNS Record Types ============

/// DNS record type identifier, used for query filtering.
///
/// Serialized as uppercase strings (`"A"`, `"AAAA"`, `"CNAME"`, etc.).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum DnsRecordType {
    /// IPv4 address record.
    A,
    /// IPv6 address record.
    Aaaa,
    /// Canonical name (alias) record.
    Cname,
    /// Mail exchange record.
    Mx,
    /// Text record.
    Txt,
    /// Name server record.
    Ns,
    /// Service locator record.
    Srv,
    /// Certificate Authority Authorization record.
    Caa,
}

/// Type-safe representation of DNS record data.
///
/// Each variant carries the fields specific to that record type.
/// Use [`record_type()`](Self::record_type) to get the [`DnsRecordType`] discriminant,
/// or [`display_value()`](Self::display_value) to get the primary value for display.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "content")]
pub enum RecordData {
    /// A record — maps a hostname to an IPv4 address.
    A {
        /// IPv4 address (e.g., `"1.2.3.4"`).
        address: String,
    },

    /// AAAA record — maps a hostname to an IPv6 address.
    AAAA {
        /// IPv6 address (e.g., `"2001:db8::1"`).
        address: String,
    },

    /// CNAME record — alias from one name to another.
    CNAME {
        /// Target hostname.
        target: String,
    },

    /// MX record — mail exchange server.
    MX {
        /// Priority (lower = preferred).
        priority: u16,
        /// Mail server hostname.
        exchange: String,
    },

    /// TXT record — arbitrary text data.
    TXT {
        /// Text content.
        text: String,
    },

    /// NS record — authoritative name server.
    NS {
        /// Name server hostname.
        nameserver: String,
    },

    /// SRV record — service locator.
    SRV {
        /// Priority (lower = preferred).
        priority: u16,
        /// Weight for load balancing among same-priority targets.
        weight: u16,
        /// TCP/UDP port number.
        port: u16,
        /// Target hostname providing the service.
        target: String,
    },

    /// CAA record — Certificate Authority Authorization.
    CAA {
        /// Issuer critical flag (0 or 128).
        flags: u8,
        /// Property tag (`"issue"`, `"issuewild"`, or `"iodef"`).
        tag: String,
        /// CA domain or reporting URI.
        value: String,
    },
}

impl RecordData {
    /// Returns the [`DnsRecordType`] discriminant for this record data.
    pub fn record_type(&self) -> DnsRecordType {
        match self {
            Self::A { .. } => DnsRecordType::A,
            Self::AAAA { .. } => DnsRecordType::Aaaa,
            Self::CNAME { .. } => DnsRecordType::Cname,
            Self::MX { .. } => DnsRecordType::Mx,
            Self::TXT { .. } => DnsRecordType::Txt,
            Self::NS { .. } => DnsRecordType::Ns,
            Self::SRV { .. } => DnsRecordType::Srv,
            Self::CAA { .. } => DnsRecordType::Caa,
        }
    }

    /// Returns the primary display value for this record (e.g., the IP address for A/AAAA,
    /// the target for CNAME/SRV, the exchange for MX).
    pub fn display_value(&self) -> &str {
        match self {
            Self::A { address } | Self::AAAA { address } => address,
            Self::CNAME { target } | Self::SRV { target, .. } => target,
            Self::MX { exchange, .. } => exchange,
            Self::TXT { text } => text,
            Self::NS { nameserver } => nameserver,
            Self::CAA { value, .. } => value,
        }
    }
}

/// A DNS record as returned by a provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DnsRecord {
    /// Provider-specific record identifier.
    pub id: String,
    /// Domain/zone identifier this record belongs to.
    pub domain_id: String,
    /// Record name (e.g., `"www"` or `"@"` for apex).
    pub name: String,
    /// Time to live in seconds.
    pub ttl: u32,
    /// Type-specific record data.
    pub data: RecordData,

    /// Whether Cloudflare CDN proxy is enabled (Cloudflare only).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proxied: Option<bool>,

    /// When the record was created, if known.
    #[serde(with = "crate::utils::datetime")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<chrono::DateTime<chrono::Utc>>,

    /// When the record was last updated, if known.
    #[serde(with = "crate::utils::datetime")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}

/// Request to create a new DNS record.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateDnsRecordRequest {
    /// Domain/zone identifier to create the record in.
    pub domain_id: String,
    /// Record name (e.g., `"www"`).
    pub name: String,
    /// Time to live in seconds.
    pub ttl: u32,
    /// Type-specific record data.
    pub data: RecordData,
    /// Enable Cloudflare CDN proxy (Cloudflare only, ignored by other providers).
    pub proxied: Option<bool>,
}

/// Request to update an existing DNS record.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateDnsRecordRequest {
    /// Domain/zone identifier the record belongs to.
    pub domain_id: String,
    /// New record name.
    pub name: String,
    /// New TTL in seconds.
    pub ttl: u32,
    /// New type-specific record data.
    pub data: RecordData,
    /// Enable Cloudflare CDN proxy (Cloudflare only, ignored by other providers).
    pub proxied: Option<bool>,
}

// ============ Batch Operation Types ============

/// Result of a batch create operation.
///
/// Contains both successfully created records and any per-record failures.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchCreateResult {
    /// Number of records successfully created.
    pub success_count: usize,
    /// Number of records that failed to create.
    pub failed_count: usize,
    /// Successfully created records.
    pub created_records: Vec<DnsRecord>,
    /// Details about each failed creation.
    pub failures: Vec<BatchCreateFailure>,
}

/// Information about a single failed record creation in a batch.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchCreateFailure {
    /// Index of the failed request in the original request slice.
    pub request_index: usize,
    /// Name of the record that failed.
    pub record_name: String,
    /// Human-readable reason for the failure.
    pub reason: String,
}

/// Result of a batch update operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchUpdateResult {
    /// Number of records successfully updated.
    pub success_count: usize,
    /// Number of records that failed to update.
    pub failed_count: usize,
    /// Successfully updated records.
    pub updated_records: Vec<DnsRecord>,
    /// Details about each failed update.
    pub failures: Vec<BatchUpdateFailure>,
}

/// Information about a single failed record update in a batch.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchUpdateFailure {
    /// ID of the record that failed to update.
    pub record_id: String,
    /// Human-readable reason for the failure.
    pub reason: String,
}

/// A single item in a batch update request.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchUpdateItem {
    /// ID of the record to update.
    pub record_id: String,
    /// The update payload.
    pub request: UpdateDnsRecordRequest,
}

/// Result of a batch delete operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchDeleteResult {
    /// Number of records successfully deleted.
    pub success_count: usize,
    /// Number of records that failed to delete.
    pub failed_count: usize,
    /// Details about each failed deletion.
    pub failures: Vec<BatchDeleteFailure>,
}

/// Information about a single failed record deletion in a batch.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchDeleteFailure {
    /// ID of the record that failed to delete.
    pub record_id: String,
    /// Human-readable reason for the failure.
    pub reason: String,
}

// ============ Provider Metadata Types ============

/// The input type of a credential field (affects UI rendering).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum FieldType {
    /// Plain text input.
    Text,
    /// Masked/password input.
    Password,
}

/// Definition of a single credential field required by a provider.
///
/// Used to dynamically build credential forms in UIs.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProviderCredentialField {
    /// Machine-readable field key (e.g., `"apiToken"`).
    pub key: String,
    /// Human-readable label (e.g., `"API Token"`).
    pub label: String,
    /// Input type for UI rendering.
    #[serde(rename = "type")]
    pub field_type: FieldType,
    /// Optional placeholder text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub placeholder: Option<String>,
    /// Optional help/description text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub help_text: Option<String>,
}

/// Provider-specific feature support flags.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ProviderFeatures {
    /// Whether the provider supports CDN proxy (e.g., Cloudflare's orange-cloud proxy).
    pub proxy: bool,
}

/// Provider-specific pagination limits.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProviderLimits {
    /// Maximum page size for domain list requests.
    pub max_page_size_domains: u32,
    /// Maximum page size for DNS record list requests.
    pub max_page_size_records: u32,
}

/// Static metadata describing a DNS provider.
///
/// Contains the provider's identity, required credential fields, supported features,
/// and API limits. Useful for building dynamic UIs or validating configuration.
///
/// Obtain via [`DnsProvider::metadata()`](crate::DnsProvider::metadata) or
/// [`get_all_provider_metadata()`](crate::get_all_provider_metadata).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProviderMetadata {
    /// Provider type identifier.
    pub id: ProviderType,
    /// Human-readable provider name.
    pub name: String,
    /// Short description of the provider.
    pub description: String,
    /// Credential fields required to authenticate with this provider.
    pub required_fields: Vec<ProviderCredentialField>,
    /// Feature flags for this provider.
    pub features: ProviderFeatures,
    /// API pagination limits for this provider.
    pub limits: ProviderLimits,
}

// ============ Credential Types ============

/// Validation error for provider credentials.
///
/// Returned when credential fields are missing, empty, or have an invalid format.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum CredentialValidationError {
    /// A required credential field is missing entirely.
    MissingField {
        /// Which provider the error relates to.
        provider: ProviderType,
        /// Machine-readable field key.
        field: String,
        /// Human-readable field label.
        label: String,
    },
    /// A credential field is present but empty/whitespace-only.
    EmptyField {
        /// Which provider the error relates to.
        provider: ProviderType,
        /// Machine-readable field key.
        field: String,
        /// Human-readable field label.
        label: String,
    },
    /// A credential field has an invalid format.
    InvalidFormat {
        /// Which provider the error relates to.
        provider: ProviderType,
        /// Machine-readable field key.
        field: String,
        /// Human-readable field label.
        label: String,
        /// Description of what's wrong with the format.
        reason: String,
    },
}

impl std::fmt::Display for CredentialValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MissingField { label, .. } => write!(f, "Missing required field: {label}"),
            Self::EmptyField { label, .. } => write!(f, "Field must not be empty: {label}"),
            Self::InvalidFormat { label, reason, .. } => write!(f, "{label}: {reason}"),
        }
    }
}

impl std::error::Error for CredentialValidationError {}

/// Type-safe credential container for all supported DNS providers.
///
/// Each variant holds the authentication fields required by that provider.
/// Pass this to [`create_provider()`](crate::create_provider) to instantiate a provider.
///
/// # Serialization
///
/// Serialized as a tagged enum with `"provider"` as the tag and `"credentials"` as the content:
///
/// ```json
/// { "provider": "cloudflare", "credentials": { "api_token": "..." } }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "provider", content = "credentials")]
pub enum ProviderCredentials {
    /// Cloudflare credentials. Requires feature `cloudflare`.
    #[cfg(feature = "cloudflare")]
    #[serde(rename = "cloudflare")]
    Cloudflare {
        /// Cloudflare API token.
        api_token: String,
    },

    /// Aliyun DNS credentials. Requires feature `aliyun`.
    #[cfg(feature = "aliyun")]
    #[serde(rename = "aliyun")]
    Aliyun {
        /// Aliyun Access Key ID.
        access_key_id: String,
        /// Aliyun Access Key Secret.
        access_key_secret: String,
    },

    /// Tencent Cloud `DNSPod` credentials. Requires feature `dnspod`.
    #[cfg(feature = "dnspod")]
    #[serde(rename = "dnspod")]
    Dnspod {
        /// Tencent Cloud Secret ID.
        secret_id: String,
        /// Tencent Cloud Secret Key.
        secret_key: String,
    },

    /// Huawei Cloud DNS credentials. Requires feature `huaweicloud`.
    #[cfg(feature = "huaweicloud")]
    #[serde(rename = "huaweicloud")]
    Huaweicloud {
        /// Huawei Cloud Access Key ID.
        access_key_id: String,
        /// Huawei Cloud Secret Access Key.
        secret_access_key: String,
    },
}

impl ProviderCredentials {
    /// Construct credentials from a `HashMap`, validating required fields.
    ///
    /// Useful for deserializing credentials stored in a flat key-value format.
    ///
    /// # Errors
    ///
    /// Returns [`CredentialValidationError`] if a required field is missing or empty.
    pub fn from_map(
        provider: &ProviderType,
        map: &std::collections::HashMap<String, String>,
    ) -> Result<Self, CredentialValidationError> {
        match provider {
            #[cfg(feature = "cloudflare")]
            ProviderType::Cloudflare => Ok(Self::Cloudflare {
                api_token: Self::get_required_field(provider, map, "apiToken", "API Token")?,
            }),
            #[cfg(feature = "aliyun")]
            ProviderType::Aliyun => Ok(Self::Aliyun {
                access_key_id: Self::get_required_field(
                    provider,
                    map,
                    "accessKeyId",
                    "Access Key ID",
                )?,
                access_key_secret: Self::get_required_field(
                    provider,
                    map,
                    "accessKeySecret",
                    "Access Key Secret",
                )?,
            }),
            #[cfg(feature = "dnspod")]
            ProviderType::Dnspod => Ok(Self::Dnspod {
                secret_id: Self::get_required_field(provider, map, "secretId", "Secret ID")?,
                secret_key: Self::get_required_field(provider, map, "secretKey", "Secret Key")?,
            }),
            #[cfg(feature = "huaweicloud")]
            ProviderType::Huaweicloud => Ok(Self::Huaweicloud {
                access_key_id: Self::get_required_field(
                    provider,
                    map,
                    "accessKeyId",
                    "Access Key ID",
                )?,
                secret_access_key: Self::get_required_field(
                    provider,
                    map,
                    "secretAccessKey",
                    "Secret Access Key",
                )?,
            }),
            #[allow(unreachable_patterns)]
            _ => Err(CredentialValidationError::InvalidFormat {
                provider: provider.clone(),
                field: "provider".to_string(),
                label: "Provider".to_string(),
                reason: format!(
                    "Provider '{provider}' is not supported or its feature is not enabled."
                ),
            }),
        }
    }

    /// Obtain required fields from `HashMap` and verify that it is not empty
    fn get_required_field(
        provider: &ProviderType,
        map: &std::collections::HashMap<String, String>,
        key: &str,
        label: &str,
    ) -> Result<String, CredentialValidationError> {
        match map.get(key) {
            None => Err(CredentialValidationError::MissingField {
                provider: provider.clone(),
                field: key.to_string(),
                label: label.to_string(),
            }),
            Some(v) if v.trim().is_empty() => Err(CredentialValidationError::EmptyField {
                provider: provider.clone(),
                field: key.to_string(),
                label: label.to_string(),
            }),
            Some(v) => Ok(v.clone()),
        }
    }

    /// Convert credentials to a `HashMap` for flat key-value storage.
    pub fn to_map(&self) -> std::collections::HashMap<String, String> {
        match self {
            Self::Cloudflare { api_token } => [("apiToken".to_string(), api_token.clone())].into(),
            Self::Aliyun {
                access_key_id,
                access_key_secret,
            } => [
                ("accessKeyId".to_string(), access_key_id.clone()),
                ("accessKeySecret".to_string(), access_key_secret.clone()),
            ]
            .into(),
            Self::Dnspod {
                secret_id,
                secret_key,
            } => [
                ("secretId".to_string(), secret_id.clone()),
                ("secretKey".to_string(), secret_key.clone()),
            ]
            .into(),
            Self::Huaweicloud {
                access_key_id,
                secret_access_key,
            } => [
                ("accessKeyId".to_string(), access_key_id.clone()),
                ("secretAccessKey".to_string(), secret_access_key.clone()),
            ]
            .into(),
        }
    }

    /// Returns the [`ProviderType`] corresponding to this credential variant.
    pub fn provider_type(&self) -> ProviderType {
        match self {
            Self::Cloudflare { .. } => ProviderType::Cloudflare,
            Self::Aliyun { .. } => ProviderType::Aliyun,
            Self::Dnspod { .. } => ProviderType::Dnspod,
            Self::Huaweicloud { .. } => ProviderType::Huaweicloud,
        }
    }
}

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

    // ============ ProviderCredentials Round Trip Test ============

    #[test]
    fn credentials_cloudflare_roundtrip() {
        let map: HashMap<String, String> =
            [("apiToken".to_string(), "my-token".to_string())].into();
        let res = ProviderCredentials::from_map(&ProviderType::Cloudflare, &map);
        assert!(res.is_ok(), "expected Ok(..), got {res:?}");
        let Ok(cred) = res else {
            return;
        };
        let back = cred.to_map();
        assert_eq!(back.get("apiToken").map(String::as_str), Some("my-token"));
        assert_eq!(cred.provider_type(), ProviderType::Cloudflare);
    }

    #[test]
    fn credentials_aliyun_roundtrip() {
        let map: HashMap<String, String> = [
            ("accessKeyId".to_string(), "id123".to_string()),
            ("accessKeySecret".to_string(), "secret456".to_string()),
        ]
        .into();
        let res = ProviderCredentials::from_map(&ProviderType::Aliyun, &map);
        assert!(res.is_ok(), "expected Ok(..), got {res:?}");
        let Ok(cred) = res else {
            return;
        };
        let back = cred.to_map();
        assert_eq!(back.get("accessKeyId").map(String::as_str), Some("id123"));
        assert_eq!(
            back.get("accessKeySecret").map(String::as_str),
            Some("secret456")
        );
    }

    #[test]
    fn credentials_dnspod_roundtrip() {
        let map: HashMap<String, String> = [
            ("secretId".to_string(), "sid".to_string()),
            ("secretKey".to_string(), "skey".to_string()),
        ]
        .into();
        let res = ProviderCredentials::from_map(&ProviderType::Dnspod, &map);
        assert!(res.is_ok(), "expected Ok(..), got {res:?}");
        let Ok(cred) = res else {
            return;
        };
        let back = cred.to_map();
        assert_eq!(back.get("secretId").map(String::as_str), Some("sid"));
        assert_eq!(back.get("secretKey").map(String::as_str), Some("skey"));
    }

    #[test]
    fn credentials_huaweicloud_roundtrip() {
        let map: HashMap<String, String> = [
            ("accessKeyId".to_string(), "ak".to_string()),
            ("secretAccessKey".to_string(), "sk".to_string()),
        ]
        .into();
        let res = ProviderCredentials::from_map(&ProviderType::Huaweicloud, &map);
        assert!(res.is_ok(), "expected Ok(..), got {res:?}");
        let Ok(cred) = res else {
            return;
        };
        let back = cred.to_map();
        assert_eq!(back.get("accessKeyId").map(String::as_str), Some("ak"));
        assert_eq!(back.get("secretAccessKey").map(String::as_str), Some("sk"));
    }

    #[test]
    fn credentials_missing_field() {
        let map: HashMap<String, String> = HashMap::new();
        let res = ProviderCredentials::from_map(&ProviderType::Cloudflare, &map);
        assert!(
            matches!(&res, Err(CredentialValidationError::MissingField { .. })),
            "unexpected result: {res:?}"
        );
    }

    #[test]
    fn credentials_empty_field() {
        let map: HashMap<String, String> = [("apiToken".to_string(), "  ".to_string())].into();
        let res = ProviderCredentials::from_map(&ProviderType::Cloudflare, &map);
        assert!(
            matches!(&res, Err(CredentialValidationError::EmptyField { .. })),
            "unexpected result: {res:?}"
        );
    }

    // ============ PaginatedResponse paging calculation test ============

    #[test]
    fn paginated_response_has_more() {
        let resp = PaginatedResponse::new(vec![1, 2, 3], 1, 3, 10);
        assert!(resp.has_more);
        assert_eq!(resp.total_count, 10);
    }

    #[test]
    fn paginated_response_no_more() {
        let resp = PaginatedResponse::new(vec![1, 2], 2, 3, 5);
        assert!(!resp.has_more); // page 2 * page_size 3 = 6 >= 5
    }

    #[test]
    fn paginated_response_exact_boundary() {
        let resp = PaginatedResponse::new(vec![1, 2, 3], 1, 3, 3);
        assert!(!resp.has_more); // 1 * 3 = 3, not < 3
    }

    #[test]
    fn paginated_response_empty() {
        let resp: PaginatedResponse<i32> = PaginatedResponse::new(vec![], 1, 20, 0);
        assert!(!resp.has_more);
        assert_eq!(resp.items.len(), 0);
    }

    // ============ DnsRecordType serde test ============

    #[test]
    fn dns_record_type_serialize() {
        let a = DnsRecordType::A;
        let json_res = serde_json::to_string(&a);
        assert!(
            json_res.is_ok(),
            "serde_json::to_string failed: {json_res:?}"
        );
        let Ok(json) = json_res else {
            return;
        };
        assert_eq!(json, "\"A\"");
    }

    #[test]
    fn dns_record_type_deserialize() {
        let a_res: serde_json::Result<DnsRecordType> = serde_json::from_str("\"AAAA\"");
        assert!(a_res.is_ok(), "serde_json::from_str failed: {a_res:?}");
        let Ok(a) = a_res else {
            return;
        };
        assert_eq!(a, DnsRecordType::Aaaa);
    }

    #[test]
    fn dns_record_type_roundtrip_all() {
        let types = vec![
            DnsRecordType::A,
            DnsRecordType::Aaaa,
            DnsRecordType::Cname,
            DnsRecordType::Mx,
            DnsRecordType::Txt,
            DnsRecordType::Ns,
            DnsRecordType::Srv,
            DnsRecordType::Caa,
        ];
        for t in types {
            let json_res = serde_json::to_string(&t);
            assert!(
                json_res.is_ok(),
                "serde_json::to_string failed: {json_res:?}"
            );
            let Ok(json) = json_res else {
                return;
            };

            let back_res: serde_json::Result<DnsRecordType> = serde_json::from_str(&json);
            assert!(
                back_res.is_ok(),
                "serde_json::from_str failed: {back_res:?}"
            );
            let Ok(back) = back_res else {
                return;
            };
            assert_eq!(back, t);
        }
    }

    // ============ RecordData serde test ============

    #[test]
    fn record_data_srv_serde_roundtrip() {
        let data = RecordData::SRV {
            priority: 10,
            weight: 20,
            port: 443,
            target: "example.com".to_string(),
        };
        let json_res = serde_json::to_string(&data);
        assert!(
            json_res.is_ok(),
            "serde_json::to_string failed: {json_res:?}"
        );
        let Ok(json) = json_res else {
            return;
        };

        let back_res: serde_json::Result<RecordData> = serde_json::from_str(&json);
        assert!(
            back_res.is_ok(),
            "serde_json::from_str failed: {back_res:?}"
        );
        let Ok(back) = back_res else {
            return;
        };
        assert_eq!(back, data);
    }

    #[test]
    fn record_data_caa_serde_roundtrip() {
        let data = RecordData::CAA {
            flags: 0,
            tag: "issue".to_string(),
            value: "letsencrypt.org".to_string(),
        };
        let json_res = serde_json::to_string(&data);
        assert!(
            json_res.is_ok(),
            "serde_json::to_string failed: {json_res:?}"
        );
        let Ok(json) = json_res else {
            return;
        };

        let back_res: serde_json::Result<RecordData> = serde_json::from_str(&json);
        assert!(
            back_res.is_ok(),
            "serde_json::from_str failed: {back_res:?}"
        );
        let Ok(back) = back_res else {
            return;
        };
        assert_eq!(back, data);
    }

    #[test]
    fn record_data_record_type() {
        assert_eq!(
            RecordData::A {
                address: "1.2.3.4".into()
            }
            .record_type(),
            DnsRecordType::A
        );
        assert_eq!(
            RecordData::SRV {
                priority: 0,
                weight: 0,
                port: 0,
                target: ".".into()
            }
            .record_type(),
            DnsRecordType::Srv
        );
    }

    #[test]
    fn record_data_display_value() {
        assert_eq!(
            RecordData::A {
                address: "1.2.3.4".into()
            }
            .display_value(),
            "1.2.3.4"
        );
        assert_eq!(
            RecordData::MX {
                priority: 10,
                exchange: "mail.x.com".into()
            }
            .display_value(),
            "mail.x.com"
        );
        assert_eq!(
            RecordData::CAA {
                flags: 0,
                tag: "issue".into(),
                value: "le.org".into()
            }
            .display_value(),
            "le.org"
        );
    }

    // ============ PaginationParams::validated Test ============

    #[test]
    fn pagination_validated_clamps_page_zero() {
        let p = PaginationParams {
            page: 0,
            page_size: 20,
        };
        let v = p.validated(100);
        assert_eq!(v.page, 1);
        assert_eq!(v.page_size, 20);
    }

    #[test]
    fn pagination_validated_clamps_page_size_over_max() {
        let p = PaginationParams {
            page: 1,
            page_size: 9999,
        };
        let v = p.validated(100);
        assert_eq!(v.page_size, 100);
    }

    #[test]
    fn pagination_validated_clamps_page_size_zero() {
        let p = PaginationParams {
            page: 1,
            page_size: 0,
        };
        let v = p.validated(100);
        assert_eq!(v.page_size, 1);
    }

    #[test]
    fn pagination_validated_normal_values_unchanged() {
        let p = PaginationParams {
            page: 3,
            page_size: 50,
        };
        let v = p.validated(100);
        assert_eq!(v.page, 3);
        assert_eq!(v.page_size, 50);
    }

    #[test]
    fn record_query_validated_preserves_filters() {
        let p = RecordQueryParams {
            page: 0,
            page_size: 9999,
            keyword: Some("test".to_string()),
            record_type: Some(DnsRecordType::A),
        };
        let v = p.validated(100);
        assert_eq!(v.page, 1);
        assert_eq!(v.page_size, 100);
        assert_eq!(v.keyword.as_deref(), Some("test"));
        assert_eq!(v.record_type, Some(DnsRecordType::A));
    }
}