starfield 0.13.0

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

use serde::Deserialize;

/// API response signature present in all SBDB API responses
#[derive(Debug, Clone, Deserialize)]
pub struct Signature {
    pub source: String,
    pub version: String,
}

/// Orbit class of a small body
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OrbitClass {
    /// IEO - Atira (Interior Earth Object)
    Atira,
    /// ATE - Aten
    Aten,
    /// APO - Apollo
    Apollo,
    /// AMO - Amor
    Amor,
    /// MCA - Mars-crossing Asteroid
    MarsCrosser,
    /// MBA - Main Belt Asteroid
    MainBelt,
    /// JFC - Jupiter-family Comet
    JupiterFamilyComet,
    /// HTC - Halley-type Comet
    HalleyTypeComet,
    /// ETc - Encke-type Comet
    EnckeTypeComet,
    /// COM - Comet (general)
    Comet,
    /// TJN - Jupiter Trojan
    JupiterTrojan,
    /// CEN - Centaur
    Centaur,
    /// TNO - Trans-Neptunian Object
    TransNeptunian,
    /// AST - Asteroid (generic)
    Asteroid,
    /// PAA - Parabolic Asteroid
    ParabolicAsteroid,
    /// HYA - Hyperbolic Asteroid
    HyperbolicAsteroid,
    /// Unrecognized orbit class code
    Other(String),
}

impl OrbitClass {
    /// Parse an orbit class from the SBDB API code string
    pub fn from_code(code: &str) -> Self {
        match code {
            "IEO" => OrbitClass::Atira,
            "ATE" => OrbitClass::Aten,
            "APO" => OrbitClass::Apollo,
            "AMO" => OrbitClass::Amor,
            "MCA" => OrbitClass::MarsCrosser,
            "MBA" => OrbitClass::MainBelt,
            "JFC" | "JFc" => OrbitClass::JupiterFamilyComet,
            "HTC" => OrbitClass::HalleyTypeComet,
            "ETc" => OrbitClass::EnckeTypeComet,
            "COM" => OrbitClass::Comet,
            "TJN" => OrbitClass::JupiterTrojan,
            "CEN" => OrbitClass::Centaur,
            "TNO" => OrbitClass::TransNeptunian,
            "AST" => OrbitClass::Asteroid,
            "PAA" => OrbitClass::ParabolicAsteroid,
            "HYA" => OrbitClass::HyperbolicAsteroid,
            other => OrbitClass::Other(other.to_string()),
        }
    }

    /// Convert to the SBDB API code string
    pub fn as_code(&self) -> &str {
        match self {
            OrbitClass::Atira => "IEO",
            OrbitClass::Aten => "ATE",
            OrbitClass::Apollo => "APO",
            OrbitClass::Amor => "AMO",
            OrbitClass::MarsCrosser => "MCA",
            OrbitClass::MainBelt => "MBA",
            OrbitClass::JupiterFamilyComet => "JFC",
            OrbitClass::HalleyTypeComet => "HTC",
            OrbitClass::EnckeTypeComet => "ETc",
            OrbitClass::Comet => "COM",
            OrbitClass::JupiterTrojan => "TJN",
            OrbitClass::Centaur => "CEN",
            OrbitClass::TransNeptunian => "TNO",
            OrbitClass::Asteroid => "AST",
            OrbitClass::ParabolicAsteroid => "PAA",
            OrbitClass::HyperbolicAsteroid => "HYA",
            OrbitClass::Other(code) => code.as_str(),
        }
    }
}

/// Small body identification data from the SBDB `object` field
#[derive(Debug, Clone)]
pub struct SmallBodyObject {
    /// Primary designation (e.g., "433", "2015 TB145")
    pub designation: String,
    /// SPK-ID
    pub spkid: Option<String>,
    /// Full name (e.g., "433 Eros (A898 PA)")
    pub fullname: Option<String>,
    /// Short name (e.g., "433 Eros")
    pub shortname: Option<String>,
    /// Object kind code (an/au/cn/cu)
    pub kind: Option<String>,
    /// Is a Near-Earth Object
    pub neo: bool,
    /// Is a Potentially Hazardous Asteroid
    pub pha: bool,
    /// Orbit class
    pub orbit_class: Option<OrbitClass>,
}

/// Orbital elements for a small body
#[derive(Debug, Clone)]
pub struct SmallBodyOrbit {
    /// Orbit solution ID
    pub orbit_id: Option<String>,
    /// Epoch (Julian Date TDB)
    pub epoch_jd: Option<f64>,
    /// Eccentricity
    pub eccentricity: Option<f64>,
    /// Semi-major axis (AU)
    pub semi_major_axis: Option<f64>,
    /// Perihelion distance (AU)
    pub perihelion_dist: Option<f64>,
    /// Inclination (degrees)
    pub inclination: Option<f64>,
    /// Longitude of ascending node (degrees)
    pub long_asc_node: Option<f64>,
    /// Argument of perihelion (degrees)
    pub arg_perihelion: Option<f64>,
    /// Mean anomaly (degrees)
    pub mean_anomaly: Option<f64>,
    /// Time of perihelion passage (Julian Date TDB)
    pub time_perihelion: Option<f64>,
    /// Mean motion (degrees/day)
    pub mean_motion: Option<f64>,
    /// Orbital period (days)
    pub period: Option<f64>,
    /// Aphelion distance (AU)
    pub aphelion_dist: Option<f64>,
    /// Minimum orbit intersection distance with Earth (AU)
    pub moid_au: Option<f64>,
    /// First observation date
    pub first_obs: Option<String>,
    /// Last observation date
    pub last_obs: Option<String>,
    /// Number of observations used
    pub n_obs_used: Option<u32>,
    /// Data arc span (days)
    pub data_arc_days: Option<u32>,
    /// Orbit condition code (0-9, 0 is best)
    pub condition_code: Option<String>,
    /// RMS of weighted residuals
    pub rms: Option<f64>,
}

/// Physical parameters for a small body
#[derive(Debug, Clone)]
pub struct PhysicalParams {
    /// Absolute magnitude H
    pub abs_magnitude_h: Option<f64>,
    /// Magnitude slope parameter G
    pub magnitude_slope_g: Option<f64>,
    /// Diameter (km)
    pub diameter_km: Option<f64>,
    /// Geometric albedo
    pub albedo: Option<f64>,
    /// Rotation period (hours)
    pub rotation_period_h: Option<f64>,
    /// Spectral type
    pub spectral_type: Option<String>,
}

/// A close approach record
#[derive(Debug, Clone)]
pub struct CloseApproachRecord {
    /// Object designation
    pub designation: String,
    /// Orbit solution ID
    pub orbit_id: Option<String>,
    /// Julian Date (TDB) of closest approach
    pub jd_tdb: Option<f64>,
    /// Calendar date/time of closest approach
    pub date: String,
    /// Nominal close approach distance (AU)
    pub dist_au: f64,
    /// Minimum possible distance (AU)
    pub dist_min_au: Option<f64>,
    /// Maximum possible distance (AU)
    pub dist_max_au: Option<f64>,
    /// Relative velocity at close approach (km/s)
    pub v_rel_km_s: Option<f64>,
    /// Velocity at infinity (km/s)
    pub v_inf_km_s: Option<f64>,
    /// Absolute magnitude H
    pub h_mag: Option<f64>,
    /// Estimated diameter (km)
    pub diameter_km: Option<f64>,
    /// Full name of the object
    pub fullname: Option<String>,
    /// Close approach body (e.g., "Earth", "Mars")
    pub body: String,
}

/// A fireball/bolide event record
#[derive(Debug, Clone)]
pub struct FireballRecord {
    /// Date/time of peak brightness
    pub date: String,
    /// Radiated energy (joules * 10^10)
    pub energy_joules_e10: Option<f64>,
    /// Estimated total impact energy (kilotons of TNT)
    pub impact_energy_kt: Option<f64>,
    /// Latitude (degrees, positive = N)
    pub latitude: Option<f64>,
    /// Latitude direction (N or S)
    pub lat_dir: Option<String>,
    /// Longitude (degrees, positive = E)
    pub longitude: Option<f64>,
    /// Longitude direction (E or W)
    pub lon_dir: Option<String>,
    /// Altitude (km)
    pub altitude_km: Option<f64>,
    /// Velocity (km/s)
    pub velocity_km_s: Option<f64>,
}

// ── Mission Design API Types ────────────────────────────────────────────────

/// Optimality criterion for mission accessible target search (Mode A)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MissionDesignCriterion {
    /// Minimize departure V-infinity
    MinDepartureVinf = 1,
    /// Minimize arrival V-infinity
    MinArrivalVinf = 2,
    /// Minimize total delta-v
    MinTotalDv = 3,
    /// Minimize TOF + minimize departure V-infinity
    MinTofMinDepVinf = 4,
    /// Minimize TOF + minimize arrival V-infinity
    MinTofMinArrVinf = 5,
    /// Minimize TOF + minimize total delta-v
    MinTofMinTotalDv = 6,
}

impl MissionDesignCriterion {
    /// Convert to the integer value used by the API
    pub fn as_api_value(&self) -> u32 {
        *self as u32
    }
}

/// Parameters for Mission Design accessible target search (Mode A)
#[derive(Debug, Clone)]
pub struct MissionAccessibleParams {
    /// Optimality criterion for ranking results
    pub crit: MissionDesignCriterion,
    /// Launch year(s) to search
    pub year: Vec<u32>,
    /// Maximum number of records to return
    pub lim: Option<u32>,
}

/// A single accessible target entry from the Mission Design API (Mode A)
#[derive(Debug, Clone)]
pub struct MissionAccessibleEntry {
    /// Object name
    pub name: String,
    /// Primary designation
    pub pdes: Option<String>,
    /// Departure date (calendar)
    pub date0: String,
    /// Departure date (Modified Julian Date)
    pub mjd0: f64,
    /// Arrival date (calendar)
    pub datef: String,
    /// Arrival date (Modified Julian Date)
    pub mjdf: f64,
    /// Departure C3 (km^2/s^2)
    pub c3_dep: f64,
    /// Departure V-infinity (km/s)
    pub vinf_dep: f64,
    /// Arrival V-infinity (km/s)
    pub vinf_arr: f64,
    /// Total delta-v (km/s)
    pub dv_tot: f64,
    /// Time of flight (days)
    pub tof: f64,
    /// Orbit class code
    pub class: Option<String>,
    /// Absolute magnitude H
    pub h_mag: Option<f64>,
    /// Orbit condition code
    pub condition_code: Option<String>,
    /// Near-Earth Object flag
    pub neo: bool,
    /// Potentially Hazardous Asteroid flag
    pub pha: bool,
}

/// Response from the Mission Design accessible target search (Mode A)
#[derive(Debug, Clone)]
pub struct MissionAccessibleResponse {
    /// Number of records returned
    pub count: u32,
    /// Accessible target entries
    pub data: Vec<MissionAccessibleEntry>,
}

/// Object info returned in a Mission Design query response (Mode Q)
#[derive(Debug, Clone)]
pub struct MissionQueryObject {
    /// Primary designation
    pub des: String,
    /// Full name
    pub fullname: Option<String>,
    /// SPK-ID
    pub spkid: Option<String>,
    /// Orbit class
    pub orbit_class: Option<String>,
    /// Orbit condition code
    pub condition_code: Option<String>,
    /// Data arc (days)
    pub data_arc: Option<String>,
    /// Orbit solution ID
    pub orbit_id: Option<String>,
    /// Mission design orbit ID
    pub md_orbit_id: Option<String>,
}

/// Response from the Mission Design query for a specific object (Mode Q)
#[derive(Debug, Clone)]
pub struct MissionQueryResponse {
    /// Object identification
    pub object: MissionQueryObject,
    /// Field names for the selected missions table
    pub fields: Vec<String>,
    /// Selected mission data rows (tabular, matches fields order)
    pub selected_missions: Vec<Vec<f64>>,
}

/// Parameters for Mission Design flyby/extension target search (Mode T)
#[derive(Debug, Clone)]
pub struct MissionFlybyParams {
    /// Eccentricity of reference orbit
    pub ec: f64,
    /// Perihelion distance (AU)
    pub qr: f64,
    /// Time of perihelion passage (Julian Date)
    pub tp: f64,
    /// Inclination (degrees)
    pub inc: f64,
    /// Longitude of ascending node (degrees)
    pub om: f64,
    /// Argument of periapsis (degrees)
    pub w: f64,
    /// Start of time span (Julian Date)
    pub jd0: f64,
    /// End of time span (Julian Date)
    pub jdf: f64,
    /// Maximum number of output records
    pub maxout: Option<u32>,
    /// Maximum close-approach distance (AU)
    pub maxdist: Option<f64>,
}

/// A flyby/extension target entry from the Mission Design API (Mode T)
#[derive(Debug, Clone)]
pub struct MissionFlybyEntry {
    /// Full object name
    pub full_name: String,
    /// Primary designation
    pub pdes: Option<String>,
    /// SPK-ID
    pub spkid: Option<String>,
    /// Close approach date (calendar)
    pub date: String,
    /// Close approach date (Julian Date)
    pub jd: f64,
    /// Minimum distance (AU)
    pub min_dist_au: f64,
    /// Minimum distance (km)
    pub min_dist_km: Option<f64>,
    /// Relative velocity (km/s)
    pub rel_vel: f64,
    /// Orbit class code
    pub class: Option<String>,
    /// Absolute magnitude H
    pub h_mag: Option<f64>,
    /// Orbit condition code
    pub condition_code: Option<String>,
    /// Near-Earth Object flag
    pub neo: bool,
    /// Potentially Hazardous Asteroid flag
    pub pha: bool,
}

/// Response from the Mission Design flyby/extension target search (Mode T)
#[derive(Debug, Clone)]
pub struct MissionFlybyResponse {
    /// Number of records returned
    pub count: u32,
    /// Flyby target entries
    pub data: Vec<MissionFlybyEntry>,
}

/// A Sentry impact risk entry
#[derive(Debug, Clone)]
pub struct SentryEntry {
    /// Object designation
    pub designation: String,
    /// Full name
    pub fullname: Option<String>,
    /// Absolute magnitude H
    pub h_mag: Option<f64>,
    /// Estimated diameter (km)
    pub diameter_km: Option<f64>,
    /// Number of potential impacts
    pub n_imp: Option<u32>,
    /// Cumulative impact probability
    pub ip: Option<f64>,
    /// Cumulative Palermo Scale
    pub ps_cum: Option<f64>,
    /// Maximum Palermo Scale
    pub ps_max: Option<f64>,
    /// Maximum Torino Scale
    pub ts_max: Option<u32>,
    /// Last observation date
    pub last_obs: Option<String>,
    /// Range of potential impact years
    pub ip_range: Option<String>,
}

/// A summary entry from the Scout NEOCP analysis API (Mode S)
#[derive(Debug, Clone)]
pub struct ScoutSummaryEntry {
    /// NEOCP temporary designation
    pub object_name: String,
    /// Number of observations
    pub n_obs: Option<u32>,
    /// Observation arc (days)
    pub arc: Option<f64>,
    /// Normalized RMS residual
    pub rms_n: Option<f64>,
    /// Estimated absolute magnitude
    pub h_mag: Option<f64>,
    /// Interest rating (0-100, higher = more interesting)
    pub rating: Option<u32>,
    /// Minimum orbit intersection distance (AU)
    pub moid: Option<f64>,
    /// Close approach distance (AU)
    pub ca_dist: Option<f64>,
    /// Velocity at infinity (km/s)
    pub v_inf: Option<f64>,
    /// PHA likelihood score
    pub pha_score: Option<i32>,
    /// NEO likelihood score
    pub neo_score: Option<i32>,
    /// Geocentric orbit likelihood
    pub geocentric_score: Option<i32>,
    /// Interior Earth orbit likelihood
    pub ieo_score: Option<i32>,
    /// Tisserand parameter score (comet vs asteroid)
    pub tisserand_score: Option<i32>,
    /// Last analysis run time
    pub last_run: Option<String>,
    /// Right ascension
    pub ra: Option<String>,
    /// Declination
    pub dec: Option<String>,
    /// Solar elongation
    pub elong: Option<String>,
    /// Rate of motion
    pub rate: Option<f64>,
    /// Estimated visual magnitude
    pub v_mag: Option<f64>,
    /// Positional uncertainty (arcsec)
    pub unc: Option<f64>,
    /// Positional uncertainty at +1 day (arcsec)
    pub unc_p1: Option<f64>,
}

/// Detailed data for a single Scout NEOCP object (Mode O)
#[derive(Debug, Clone)]
pub struct ScoutObjectDetail {
    /// All summary-level fields
    pub summary: ScoutSummaryEntry,
    /// NEO 1km impact score
    pub neo1km_score: Option<String>,
    /// Ephemeris time
    pub t_ephem: Option<String>,
    /// Sampled orbit data (fields + rows)
    pub orbits: Option<ScoutOrbitData>,
}

/// Sampled orbit data from Scout object detail
#[derive(Debug, Clone)]
pub struct ScoutOrbitData {
    /// Number of sampled orbits
    pub count: u32,
    /// Field names for orbit columns
    pub fields: Vec<String>,
    /// Raw orbit data rows
    pub data: Vec<Vec<serde_json::Value>>,
}

/// Response from the Scout summary endpoint (Mode S)
#[derive(Debug, Clone)]
pub struct ScoutSummaryResponse {
    /// Total number of NEOCP objects
    pub count: u32,
    /// Summary entries for each object
    pub data: Vec<ScoutSummaryEntry>,
}

/// Response from the Scout object detail endpoint (Mode O)
#[derive(Debug, Clone)]
pub struct ScoutObjectResponse {
    /// Detailed object data
    pub detail: ScoutObjectDetail,
}

/// Parameters for SB Radar API queries
#[derive(Debug, Clone, Default)]
pub struct RadarParams {
    /// Select by SPK-ID
    pub spk: Option<i64>,
    /// Select by designation
    pub des: Option<String>,
    /// Object type: a, an, au, c, cn, cu, n, u
    pub kind: Option<String>,
    /// Reference point: P (peak/center) or C (center of mass)
    pub bp: Option<String>,
    /// Measurement type: R (range/delay) or P (Doppler)
    pub measurement_type: Option<String>,
    /// Include observer field
    pub observer: bool,
    /// Include notes field
    pub notes: bool,
    /// Include reference field
    pub ref_field: bool,
    /// Include full object name
    pub fullname: bool,
    /// Include modification date
    pub modified: bool,
    /// Include station geodetic coordinates
    pub coords: bool,
    /// Use cylindrical station coordinates instead of geodetic
    pub c_coords: bool,
}

impl RadarParams {
    /// Convert parameters to query string pairs
    pub fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();
        if let Some(v) = self.spk {
            params.push(("spk".into(), v.to_string()));
        }
        if let Some(ref v) = self.des {
            params.push(("des".into(), v.clone()));
        }
        if let Some(ref v) = self.kind {
            params.push(("kind".into(), v.clone()));
        }
        if let Some(ref v) = self.bp {
            params.push(("bp".into(), v.clone()));
        }
        if let Some(ref v) = self.measurement_type {
            params.push(("type".into(), v.clone()));
        }
        if self.observer {
            params.push(("observer".into(), "true".into()));
        }
        if self.notes {
            params.push(("notes".into(), "true".into()));
        }
        if self.ref_field {
            params.push(("ref".into(), "true".into()));
        }
        if self.fullname {
            params.push(("fullname".into(), "true".into()));
        }
        if self.modified {
            params.push(("modified".into(), "true".into()));
        }
        if self.coords {
            params.push(("coords".into(), "true".into()));
        }
        if self.c_coords {
            params.push(("c-coords".into(), "true".into()));
        }
        params
    }
}

/// Observer location for the SB Identification API
#[derive(Debug, Clone)]
pub enum SbIdentObserver {
    /// MPC observatory code (e.g., "F51" for Pan-STARRS)
    MpcCode(String),
    /// Geodetic coordinates (lat degrees north-positive, lon degrees east-positive, alt km)
    Geodetic { lat: f64, lon: f64, alt: f64 },
    /// Geocentric state vector: position (km) and optional velocity (km/s), J2000 equatorial
    Geocentric(String),
    /// Heliocentric state vector: position (AU) and optional velocity (AU/d), J2000 equatorial
    Heliocentric(String),
}

/// Field of view specification for the SB Identification API
#[derive(Debug, Clone)]
pub enum SbIdentFov {
    /// FOV defined by RA/Dec edge limits (sexagesimal strings)
    Edges {
        /// RA edges: comma-separated `hh-mm-ss[.ss]` values
        ra_lim: String,
        /// Dec edges: comma-separated `dd-mm-ss[.ss]` values
        dec_lim: String,
    },
    /// FOV defined by center point and half-widths
    Center {
        /// Center RA: `hh-mm-ss[.ss]`
        ra_center: String,
        /// Center Dec: `dd-mm-ss[.ss]`
        dec_center: String,
        /// Half-width in RA (degrees, default 0.5)
        ra_hwidth: Option<f64>,
        /// Half-width in Dec (degrees, default 0.5)
        dec_hwidth: Option<f64>,
    },
}

/// Parameters for the SB Identification API query
#[derive(Debug, Clone)]
pub struct SbIdentParams {
    /// Observer location
    pub observer: SbIdentObserver,
    /// Field of view specification
    pub fov: SbIdentFov,
    /// Observation time: `YYYY-MM-DD[_hh:mm:ss]` or Julian Date
    pub obs_time: String,
    /// Visual magnitude limit threshold
    pub vmag_lim: Option<f64>,
    /// Enable second pass with high-fidelity numerical integration
    pub two_pass: bool,
    /// Require magnitude parameters for returned objects
    pub mag_required: Option<bool>,
    /// Object type filter: `a` (asteroids only)
    pub sb_kind: Option<String>,
    /// Group filter: e.g., `neo`
    pub sb_group: Option<String>,
    /// Include osculating orbital elements in the response
    pub req_elem: bool,
}

impl SbIdentParams {
    /// Convert parameters to query string key-value pairs for the HTTP request
    pub fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        match &self.observer {
            SbIdentObserver::MpcCode(code) => {
                params.push(("mpc-code".into(), code.clone()));
            }
            SbIdentObserver::Geodetic { lat, lon, alt } => {
                params.push(("lat".into(), lat.to_string()));
                params.push(("lon".into(), lon.to_string()));
                params.push(("alt".into(), alt.to_string()));
            }
            SbIdentObserver::Geocentric(state) => {
                params.push(("xobs".into(), state.clone()));
            }
            SbIdentObserver::Heliocentric(state) => {
                params.push(("xobs-hel".into(), state.clone()));
            }
        }

        match &self.fov {
            SbIdentFov::Edges { ra_lim, dec_lim } => {
                params.push(("fov-ra-lim".into(), ra_lim.clone()));
                params.push(("fov-dec-lim".into(), dec_lim.clone()));
            }
            SbIdentFov::Center {
                ra_center,
                dec_center,
                ra_hwidth,
                dec_hwidth,
            } => {
                params.push(("fov-ra-center".into(), ra_center.clone()));
                params.push(("fov-dec-center".into(), dec_center.clone()));
                if let Some(w) = ra_hwidth {
                    params.push(("fov-ra-hwidth".into(), w.to_string()));
                }
                if let Some(w) = dec_hwidth {
                    params.push(("fov-dec-hwidth".into(), w.to_string()));
                }
            }
        }

        params.push(("obs-time".into(), self.obs_time.clone()));

        if let Some(v) = self.vmag_lim {
            params.push(("vmag-lim".into(), v.to_string()));
        }
        if self.two_pass {
            params.push(("two-pass".into(), "true".into()));
            params.push(("suppress-first-pass".into(), "false".into()));
        }
        if let Some(v) = self.mag_required {
            params.push(("mag-required".into(), v.to_string()));
        }
        if let Some(ref v) = self.sb_kind {
            params.push(("sb-kind".into(), v.clone()));
        }
        if let Some(ref v) = self.sb_group {
            params.push(("sb-group".into(), v.clone()));
        }
        if self.req_elem {
            params.push(("req-elem".into(), "true".into()));
        }

        params
    }
}

/// A radar astrometry measurement record for a small body
#[derive(Debug, Clone)]
pub struct RadarRecord {
    /// Object designation
    pub designation: String,
    /// Observation epoch
    pub epoch: String,
    /// Measured value (delay in microseconds or Doppler shift in Hz)
    pub value: Option<f64>,
    /// Measurement uncertainty (1-sigma)
    pub sigma: Option<f64>,
    /// Units of measurement: "us" (microseconds) or "Hz"
    pub units: Option<String>,
    /// Transmission frequency (MHz)
    pub freq: Option<f64>,
    /// Receiving station DSN code
    pub rcvr: Option<String>,
    /// Transmitting station DSN code
    pub xmit: Option<String>,
    /// Reference point: P (peak/center) or C (center of mass)
    pub bp: Option<String>,
    /// Observer information
    pub observer: Option<String>,
    /// Notes
    pub notes: Option<String>,
    /// Literature reference
    pub reference: Option<String>,
    /// Full object name
    pub fullname: Option<String>,
    /// Last modification date
    pub modified: Option<String>,
    /// Station longitude (degrees)
    pub longitude: Option<f64>,
    /// Station latitude (degrees)
    pub latitude: Option<f64>,
    /// Station altitude or cylindrical d_xy
    pub altitude: Option<f64>,
}

/// Response from the SB Radar API
#[derive(Debug, Clone)]
pub struct RadarResponse {
    /// Total number of records
    pub count: u32,
    /// Radar measurement records
    pub records: Vec<RadarRecord>,
}

/// A single identified small body entry from the SB Identification API
#[derive(Debug, Clone)]
pub struct SbIdentEntry {
    /// Object name/designation
    pub name: String,
    /// Astrometric right ascension (sexagesimal or degrees)
    pub ra: Option<String>,
    /// Astrometric declination (sexagesimal or degrees)
    pub dec: Option<String>,
    /// RA offset from FOV center (arcsec)
    pub ra_offset: Option<f64>,
    /// Dec offset from FOV center (arcsec)
    pub dec_offset: Option<f64>,
    /// Total offset from FOV center (arcsec)
    pub total_offset: Option<f64>,
    /// Visual magnitude
    pub vmag: Option<f64>,
    /// RA rate of motion (deg/s)
    pub ra_rate: Option<f64>,
    /// Dec rate of motion (deg/s)
    pub dec_rate: Option<f64>,
    /// RA error estimate (arcsec, first-pass only)
    pub ra_err: Option<f64>,
    /// Dec error estimate (arcsec, first-pass only)
    pub dec_err: Option<f64>,
}

/// Orbital elements for an identified small body (when `req_elem` is true)
#[derive(Debug, Clone)]
pub struct SbIdentOrbitalElements {
    /// Object name/designation
    pub name: String,
    /// Absolute magnitude H
    pub h: Option<f64>,
    /// Magnitude slope parameter G
    pub g: Option<f64>,
    /// Eccentricity
    pub e: Option<f64>,
    /// Perihelion distance (AU)
    pub q: Option<f64>,
    /// Time of perihelion passage (JD)
    pub tp: Option<f64>,
    /// Longitude of ascending node (degrees)
    pub om: Option<f64>,
    /// Argument of perihelion (degrees)
    pub w: Option<f64>,
    /// Inclination (degrees)
    pub i: Option<f64>,
    /// Epoch (JD)
    pub epoch: Option<f64>,
}

/// Observer information returned in the SB Identification response
#[derive(Debug, Clone)]
pub struct SbIdentObserverInfo {
    /// Observation date/time
    pub obs_date: Option<String>,
    /// Observer location description
    pub location: Option<String>,
    /// FOV center coordinate
    pub fov_center: Option<String>,
    /// FOV offset description
    pub fov_offset: Option<String>,
    /// Reference frame (typically "J2000")
    pub frame: Option<String>,
}

/// Response from the SB Identification API
#[derive(Debug, Clone)]
pub struct SbIdentResponse {
    /// Observer information
    pub observer: SbIdentObserverInfo,
    /// Number of objects found in first pass
    pub n_first_pass: u32,
    /// Number of objects found in second pass (when two-pass enabled)
    pub n_second_pass: u32,
    /// Identified objects from first-pass search
    pub data_first_pass: Vec<SbIdentEntry>,
    /// Identified objects from second-pass search (when two-pass enabled)
    pub data_second_pass: Vec<SbIdentEntry>,
    /// Orbital elements from first-pass (when req_elem is true)
    pub elem_first_pass: Vec<SbIdentOrbitalElements>,
    /// Orbital elements from second-pass (when req_elem is true)
    pub elem_second_pass: Vec<SbIdentOrbitalElements>,
}

/// Observer location for the SB Observability API
#[derive(Debug, Clone)]
pub enum ObservabilityObserver {
    /// MPC observatory code (e.g., "F51" for Pan-STARRS 1)
    MpcCode(String),
    /// Geodetic coordinates
    Geodetic {
        /// Latitude in degrees, north-positive [-90, 90]
        lat: f64,
        /// Longitude in degrees, east-positive [-180, 180]
        lon: f64,
        /// Altitude above WGS-84 ellipsoid in km
        alt: f64,
    },
}

/// Parameters for the SB Observability API query
#[derive(Debug, Clone)]
pub struct ObservabilityParams {
    /// Observer location (required)
    pub observer: ObservabilityObserver,
    /// Observation date: YYYY-MM-DD or YYYY-MM-DD_hh:mm:ss (required)
    pub obs_time: String,
    /// End observation time
    pub obs_end: Option<String>,
    /// Require sun below horizon (default: true)
    pub optical: Option<bool>,
    /// Minimum solar elongation (deg), required if optical=false
    pub elong_min: Option<f64>,
    /// Maximum solar elongation (deg)
    pub elong_max: Option<f64>,
    /// Minimum galactic latitude (deg)
    pub glat_min: Option<f64>,
    /// Maximum galactic latitude (deg)
    pub glat_max: Option<f64>,
    /// Minimum elevation above horizon (deg, default: 30)
    pub elev_min: Option<f64>,
    /// Minimum observable time (minutes, default: 0)
    pub time_min: Option<u32>,
    /// Minimum visual magnitude
    pub vmag_min: Option<f64>,
    /// Maximum visual magnitude
    pub vmag_max: Option<f64>,
    /// Require magnitude data
    pub mag_required: Option<bool>,
    /// Minimum heliocentric distance (AU)
    pub helio_min: Option<f64>,
    /// Maximum heliocentric distance (AU)
    pub helio_max: Option<f64>,
    /// Minimum topocentric distance (AU)
    pub dist_min: Option<f64>,
    /// Maximum topocentric distance (AU)
    pub dist_max: Option<f64>,
    /// Maximum number of output records
    pub maxoutput: Option<u32>,
    /// Sort field for output
    pub output_sort: Option<String>,
    /// Sort in descending order
    pub output_sort_r: Option<bool>,
    /// Small body kind filter (a=asteroid, c=comet)
    pub sb_kind: Option<String>,
    /// Small body group filter (e.g., "neo", "pha")
    pub sb_group: Option<String>,
}

impl ObservabilityParams {
    /// Convert parameters to query string key-value pairs
    pub fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        match &self.observer {
            ObservabilityObserver::MpcCode(code) => {
                params.push(("mpc-code".into(), code.clone()));
            }
            ObservabilityObserver::Geodetic { lat, lon, alt } => {
                params.push(("lat".into(), lat.to_string()));
                params.push(("lon".into(), lon.to_string()));
                params.push(("alt".into(), alt.to_string()));
            }
        }

        params.push(("obs-time".into(), self.obs_time.clone()));

        if let Some(ref v) = self.obs_end {
            params.push(("obs-end".into(), v.clone()));
        }
        if let Some(v) = self.optical {
            params.push(("optical".into(), v.to_string()));
        }
        if let Some(v) = self.elong_min {
            params.push(("elong-min".into(), v.to_string()));
        }
        if let Some(v) = self.elong_max {
            params.push(("elong-max".into(), v.to_string()));
        }
        if let Some(v) = self.glat_min {
            params.push(("glat-min".into(), v.to_string()));
        }
        if let Some(v) = self.glat_max {
            params.push(("glat-max".into(), v.to_string()));
        }
        if let Some(v) = self.elev_min {
            params.push(("elev-min".into(), v.to_string()));
        }
        if let Some(v) = self.time_min {
            params.push(("time-min".into(), v.to_string()));
        }
        if let Some(v) = self.vmag_min {
            params.push(("vmag-min".into(), v.to_string()));
        }
        if let Some(v) = self.vmag_max {
            params.push(("vmag-max".into(), v.to_string()));
        }
        if let Some(v) = self.mag_required {
            params.push(("mag-required".into(), v.to_string()));
        }
        if let Some(v) = self.helio_min {
            params.push(("helio-min".into(), v.to_string()));
        }
        if let Some(v) = self.helio_max {
            params.push(("helio-max".into(), v.to_string()));
        }
        if let Some(v) = self.dist_min {
            params.push(("dist-min".into(), v.to_string()));
        }
        if let Some(v) = self.dist_max {
            params.push(("dist-max".into(), v.to_string()));
        }
        if let Some(v) = self.maxoutput {
            params.push(("maxoutput".into(), v.to_string()));
        }
        if let Some(ref v) = self.output_sort {
            params.push(("output-sort".into(), v.clone()));
        }
        if let Some(v) = self.output_sort_r {
            params.push(("output-sort-r".into(), v.to_string()));
        }
        if let Some(ref v) = self.sb_kind {
            params.push(("sb-kind".into(), v.clone()));
        }
        if let Some(ref v) = self.sb_group {
            params.push(("sb-group".into(), v.clone()));
        }

        params
    }
}

/// Night information from the SB Observability API response
#[derive(Debug, Clone)]
pub struct ObservabilityNightInfo {
    /// Sunset time (UT)
    pub sun_set: Option<String>,
    /// Sunrise time (UT)
    pub sun_rise: Option<String>,
    /// Sunset azimuth (deg)
    pub sun_set_az: Option<String>,
    /// Sunrise azimuth (deg)
    pub sun_rise_az: Option<String>,
    /// Begin astronomical twilight (UT)
    pub begin_astronomical: Option<String>,
    /// End astronomical twilight (UT)
    pub end_astronomical: Option<String>,
    /// Begin civil twilight (UT)
    pub begin_civil: Option<String>,
    /// End civil twilight (UT)
    pub end_civil: Option<String>,
    /// Begin nautical twilight (UT)
    pub begin_nautical: Option<String>,
    /// End nautical twilight (UT)
    pub end_nautical: Option<String>,
    /// Moon rise time (UT)
    pub moon_rise: Option<String>,
    /// Moon rise phase
    pub moon_rise_phase: Option<String>,
    /// Moon set time (UT)
    pub moon_set: Option<String>,
    /// Moon set phase
    pub moon_set_phase: Option<String>,
    /// Moon transit time (UT)
    pub transit: Option<String>,
    /// Moon transit phase
    pub transit_phase: Option<String>,
    /// Begin dark time (UT)
    pub begin_dark: Option<String>,
    /// Mid dark time (UT)
    pub mid_dark: Option<String>,
    /// End dark time (UT)
    pub end_dark: Option<String>,
    /// Total dark time (hours)
    pub dark_time: Option<String>,
}

/// A single observable object from the SB Observability API
#[derive(Debug, Clone)]
pub struct ObservableObject {
    /// Object designation
    pub des: String,
    /// Full name
    pub fullname: Option<String>,
    /// Rise time (UT)
    pub rise: Option<String>,
    /// Transit time (UT)
    pub transit: Option<String>,
    /// Set time (UT)
    pub set: Option<String>,
    /// Maximum observable time
    pub max_time: Option<String>,
    /// Right ascension
    pub ra: Option<String>,
    /// Declination
    pub dec: Option<String>,
    /// Visual magnitude
    pub vmag: Option<f64>,
    /// Heliocentric range (AU)
    pub helio_range_au: Option<f64>,
    /// Topocentric range (AU)
    pub topo_range_au: Option<f64>,
    /// Object-Observer-Sun angle (deg)
    pub sun_angle: Option<f64>,
    /// Object-Observer-Moon angle (deg)
    pub moon_angle: Option<f64>,
    /// Galactic latitude (deg)
    pub galactic_lat: Option<f64>,
}

/// Response from the SB Observability API
#[derive(Debug, Clone)]
pub struct ObservabilityResponse {
    /// Night information (sunset/sunrise, twilight, moon data)
    pub night_info: ObservabilityNightInfo,
    /// Total number of observable objects
    pub count: u32,
    /// Observable objects
    pub objects: Vec<ObservableObject>,
}

/// Parameters for NHATS (Near-Earth Asteroid Human Space Flight Accessible Targets Survey) queries
#[derive(Debug, Clone, Default)]
pub struct NhatsParams {
    /// Maximum total mission delta-v in km/s (4-12, default 12)
    pub dv: Option<u32>,
    /// Maximum total mission duration in days (60-450 in steps of 30, default 450)
    pub dur: Option<u32>,
    /// Minimum stay time at asteroid in days (8, 16, 24, or 32; default 8)
    pub stay: Option<u32>,
    /// Launch window (e.g., "2020-2045", "2025-2030")
    pub launch: Option<String>,
    /// Maximum absolute magnitude H (16-30, Mode S only)
    pub h: Option<u32>,
    /// Maximum orbit condition code (0-8, Mode S only)
    pub occ: Option<u32>,
}

impl NhatsParams {
    /// Convert parameters to query string pairs for the HTTP request
    pub fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();
        if let Some(v) = self.dv {
            params.push(("dv".into(), v.to_string()));
        }
        if let Some(v) = self.dur {
            params.push(("dur".into(), v.to_string()));
        }
        if let Some(v) = self.stay {
            params.push(("stay".into(), v.to_string()));
        }
        if let Some(ref v) = self.launch {
            params.push(("launch".into(), v.clone()));
        }
        if let Some(v) = self.h {
            params.push(("h".into(), v.to_string()));
        }
        if let Some(v) = self.occ {
            params.push(("occ".into(), v.to_string()));
        }
        params
    }
}

/// A delta-v / duration pair used in NHATS summary entries
#[derive(Debug, Clone)]
pub struct NhatsDvDur {
    /// Delta-v (km/s)
    pub dv: Option<f64>,
    /// Duration (days)
    pub dur: Option<f64>,
}

/// A trajectory record from the NHATS API (Mode O detail)
#[derive(Debug, Clone)]
pub struct NhatsTrajectory {
    /// Trajectory ID
    pub tid: Option<String>,
    /// Total mission delta-v (km/s)
    pub dv_total: Option<f64>,
    /// Total mission duration (days)
    pub dur_total: Option<f64>,
    /// Outbound duration (days)
    pub dur_out: Option<f64>,
    /// Stay duration at asteroid (days)
    pub dur_at: Option<f64>,
    /// Return duration (days)
    pub dur_ret: Option<f64>,
    /// Launch date (YYYY-MM-DD)
    pub launch: Option<String>,
    /// Launch C3 energy (km^2/s^2)
    pub c3: Option<f64>,
    /// Departure velocity from Earth (km/s)
    pub v_dep_earth: Option<f64>,
    /// Delta-v to depart parking orbit (km/s)
    pub dv_dep_park: Option<f64>,
    /// Relative arrival velocity at NEO (km/s)
    pub vrel_arr_neo: Option<f64>,
    /// Relative departure velocity from NEO (km/s)
    pub vrel_dep_neo: Option<f64>,
    /// Relative arrival velocity at Earth (km/s)
    pub vrel_arr_earth: Option<f64>,
    /// Arrival velocity at Earth (km/s)
    pub v_arr_earth: Option<f64>,
    /// Departure declination (degrees)
    pub dec_dep: Option<f64>,
    /// Arrival declination (degrees)
    pub dec_arr: Option<f64>,
}

/// A summary entry from the NHATS API (Mode S)
#[derive(Debug, Clone)]
pub struct NhatsSummaryEntry {
    /// Object designation
    pub des: String,
    /// Full name
    pub fullname: Option<String>,
    /// Orbit solution ID
    pub orbit_id: Option<String>,
    /// Absolute magnitude H
    pub h: Option<f64>,
    /// Minimum estimated size (meters)
    pub min_size: Option<f64>,
    /// Maximum estimated size (meters)
    pub max_size: Option<f64>,
    /// Measured size (meters), if available
    pub size: Option<f64>,
    /// Orbit condition code
    pub occ: Option<u32>,
    /// Minimum delta-v trajectory summary (dv + dur)
    pub min_dv: Option<NhatsDvDur>,
    /// Minimum duration trajectory summary (dv + dur)
    pub min_dur: Option<NhatsDvDur>,
    /// Number of viable trajectories
    pub n_via_traj: Option<u32>,
    /// Observation window start date
    pub obs_start: Option<String>,
    /// Observation window end date
    pub obs_end: Option<String>,
}

/// Response from the NHATS API in summary mode (Mode S)
#[derive(Debug, Clone)]
pub struct NhatsSummaryResponse {
    /// Total number of accessible objects
    pub count: u32,
    /// List of accessible NEA entries
    pub data: Vec<NhatsSummaryEntry>,
}

/// Response from the NHATS API in object detail mode (Mode O)
#[derive(Debug, Clone)]
pub struct NhatsObjectResponse {
    /// Object designation
    pub des: String,
    /// Full name
    pub fullname: Option<String>,
    /// Orbit solution ID
    pub orbit_id: Option<String>,
    /// Absolute magnitude H
    pub h: Option<f64>,
    /// Minimum estimated size (meters)
    pub min_size: Option<f64>,
    /// Maximum estimated size (meters)
    pub max_size: Option<f64>,
    /// Measured size (meters), if available
    pub size: Option<f64>,
    /// Orbit condition code
    pub occ: Option<u32>,
    /// Number of viable trajectories
    pub n_via_traj: Option<u32>,
    /// Minimum delta-v trajectory detail
    pub min_dv_traj: Option<NhatsTrajectory>,
    /// Minimum duration trajectory detail
    pub min_dur_traj: Option<NhatsTrajectory>,
}

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

    #[test]
    fn test_orbit_class_from_code() {
        assert_eq!(OrbitClass::from_code("APO"), OrbitClass::Apollo);
        assert_eq!(OrbitClass::from_code("AMO"), OrbitClass::Amor);
        assert_eq!(OrbitClass::from_code("MBA"), OrbitClass::MainBelt);
        assert_eq!(OrbitClass::from_code("TNO"), OrbitClass::TransNeptunian);
        assert_eq!(
            OrbitClass::from_code("XYZ"),
            OrbitClass::Other("XYZ".to_string())
        );
    }

    #[test]
    fn test_mission_design_criterion_values() {
        assert_eq!(MissionDesignCriterion::MinDepartureVinf.as_api_value(), 1);
        assert_eq!(MissionDesignCriterion::MinArrivalVinf.as_api_value(), 2);
        assert_eq!(MissionDesignCriterion::MinTotalDv.as_api_value(), 3);
        assert_eq!(MissionDesignCriterion::MinTofMinDepVinf.as_api_value(), 4);
        assert_eq!(MissionDesignCriterion::MinTofMinArrVinf.as_api_value(), 5);
        assert_eq!(MissionDesignCriterion::MinTofMinTotalDv.as_api_value(), 6);
    }

    #[test]
    fn test_orbit_class_roundtrip() {
        let classes = [
            OrbitClass::Atira,
            OrbitClass::Aten,
            OrbitClass::Apollo,
            OrbitClass::Amor,
            OrbitClass::MainBelt,
            OrbitClass::Centaur,
        ];
        for class in &classes {
            assert_eq!(&OrbitClass::from_code(class.as_code()), class);
        }
    }

    #[test]
    fn test_sb_ident_params_mpc_code() {
        let params = SbIdentParams {
            observer: SbIdentObserver::MpcCode("F51".into()),
            fov: SbIdentFov::Center {
                ra_center: "05-00-00".into(),
                dec_center: "20-00-00".into(),
                ra_hwidth: Some(1.0),
                dec_hwidth: Some(1.0),
            },
            obs_time: "2024-01-01".into(),
            vmag_lim: Some(20.0),
            two_pass: false,
            mag_required: None,
            sb_kind: None,
            sb_group: None,
            req_elem: false,
        };
        let query = params.to_query_params();
        let map: std::collections::HashMap<String, String> = query.into_iter().collect();

        assert_eq!(map.get("mpc-code").unwrap(), "F51");
        assert_eq!(map.get("fov-ra-center").unwrap(), "05-00-00");
        assert_eq!(map.get("fov-dec-center").unwrap(), "20-00-00");
        assert_eq!(map.get("fov-ra-hwidth").unwrap(), "1");
        assert_eq!(map.get("obs-time").unwrap(), "2024-01-01");
        assert_eq!(map.get("vmag-lim").unwrap(), "20");
        assert!(map.get("two-pass").is_none());
    }

    #[test]
    fn test_sb_ident_params_geodetic_edges() {
        let params = SbIdentParams {
            observer: SbIdentObserver::Geodetic {
                lat: 34.05,
                lon: -118.25,
                alt: 0.0,
            },
            fov: SbIdentFov::Edges {
                ra_lim: "05-00-00,06-00-00".into(),
                dec_lim: "19-00-00,21-00-00".into(),
            },
            obs_time: "2024-06-15_12:00:00".into(),
            vmag_lim: None,
            two_pass: true,
            mag_required: Some(true),
            sb_kind: Some("a".into()),
            sb_group: Some("neo".into()),
            req_elem: true,
        };
        let query = params.to_query_params();
        let map: std::collections::HashMap<String, String> = query.into_iter().collect();

        assert_eq!(map.get("lat").unwrap(), "34.05");
        assert_eq!(map.get("lon").unwrap(), "-118.25");
        assert_eq!(map.get("alt").unwrap(), "0");
        assert_eq!(map.get("fov-ra-lim").unwrap(), "05-00-00,06-00-00");
        assert_eq!(map.get("fov-dec-lim").unwrap(), "19-00-00,21-00-00");
        assert_eq!(map.get("two-pass").unwrap(), "true");
        assert_eq!(map.get("suppress-first-pass").unwrap(), "false");
        assert_eq!(map.get("mag-required").unwrap(), "true");
        assert_eq!(map.get("sb-kind").unwrap(), "a");
        assert_eq!(map.get("sb-group").unwrap(), "neo");
        assert_eq!(map.get("req-elem").unwrap(), "true");
    }

    #[test]
    fn test_observability_params_mpc_code() {
        let params = ObservabilityParams {
            observer: ObservabilityObserver::MpcCode("F51".into()),
            obs_time: "2026-03-01".into(),
            obs_end: None,
            optical: None,
            elong_min: None,
            elong_max: None,
            glat_min: None,
            glat_max: None,
            elev_min: None,
            time_min: None,
            vmag_min: None,
            vmag_max: Some(18.0),
            mag_required: None,
            helio_min: None,
            helio_max: None,
            dist_min: None,
            dist_max: None,
            maxoutput: Some(10),
            output_sort: Some("vmag".into()),
            output_sort_r: None,
            sb_kind: None,
            sb_group: None,
        };
        let query = params.to_query_params();
        let map: std::collections::HashMap<String, String> = query.into_iter().collect();

        assert_eq!(map.get("mpc-code").unwrap(), "F51");
        assert_eq!(map.get("obs-time").unwrap(), "2026-03-01");
        assert_eq!(map.get("vmag-max").unwrap(), "18");
        assert_eq!(map.get("maxoutput").unwrap(), "10");
        assert_eq!(map.get("output-sort").unwrap(), "vmag");
        assert!(!map.contains_key("lat"));
    }

    #[test]
    fn test_observability_params_geodetic() {
        let params = ObservabilityParams {
            observer: ObservabilityObserver::Geodetic {
                lat: 34.05,
                lon: -118.25,
                alt: 0.1,
            },
            obs_time: "2026-06-15".into(),
            obs_end: None,
            optical: Some(false),
            elong_min: Some(30.0),
            elong_max: None,
            glat_min: None,
            glat_max: None,
            elev_min: Some(20.0),
            time_min: None,
            vmag_min: None,
            vmag_max: None,
            mag_required: None,
            helio_min: None,
            helio_max: None,
            dist_min: None,
            dist_max: None,
            maxoutput: None,
            output_sort: None,
            output_sort_r: None,
            sb_kind: Some("a".into()),
            sb_group: Some("neo".into()),
        };
        let query = params.to_query_params();
        let map: std::collections::HashMap<String, String> = query.into_iter().collect();

        assert_eq!(map.get("lat").unwrap(), "34.05");
        assert_eq!(map.get("lon").unwrap(), "-118.25");
        assert_eq!(map.get("alt").unwrap(), "0.1");
        assert_eq!(map.get("optical").unwrap(), "false");
        assert_eq!(map.get("elong-min").unwrap(), "30");
        assert_eq!(map.get("elev-min").unwrap(), "20");
        assert_eq!(map.get("sb-kind").unwrap(), "a");
        assert_eq!(map.get("sb-group").unwrap(), "neo");
        assert!(!map.contains_key("mpc-code"));
    }
}