khanij 1.1.0

Khanij — geology and mineralogy engine for crystal structures, rock cycles, soil, and mineral properties
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
use serde::{Deserialize, Serialize};

/// Soil texture classification (USDA).
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let comp = SoilComposition::new(0.40, 0.40, 0.20).unwrap();
/// assert_eq!(comp.texture(), SoilTexture::Loam);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SoilTexture {
    Sand,
    LoamySand,
    SandyLoam,
    Loam,
    SiltLoam,
    Silt,
    SandyClayLoam,
    ClayLoam,
    SiltyClayLoam,
    SandyClay,
    SiltyClay,
    Clay,
}

/// Soil composition by particle size fractions (sum to 1.0).
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let comp = SoilComposition::new(0.40, 0.40, 0.20).unwrap();
/// assert_eq!(comp.texture(), SoilTexture::Loam);
/// ```
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SoilComposition {
    pub sand: f64, // > 0.05mm
    pub silt: f64, // 0.002-0.05mm
    pub clay: f64, // < 0.002mm
}

impl SoilComposition {
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// assert!(SoilComposition::new(0.40, 0.40, 0.20).is_some());
    /// assert!(SoilComposition::new(0.5, 0.5, 0.5).is_none()); // sum != 1
    /// ```
    #[must_use]
    pub fn new(sand: f64, silt: f64, clay: f64) -> Option<Self> {
        let sum = sand + silt + clay;
        if (sum - 1.0).abs() > 0.01 || sand < 0.0 || silt < 0.0 || clay < 0.0 {
            return None;
        }
        Some(Self { sand, silt, clay })
    }

    /// Classify soil texture from composition using the USDA soil texture triangle.
    ///
    /// Boundaries follow the standard USDA classification with all 12 texture
    /// classes reachable. Percentages are expressed as fractions (0.0-1.0).
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let comp = SoilComposition::new(0.90, 0.05, 0.05).unwrap();
    /// assert_eq!(comp.texture(), SoilTexture::Sand);
    /// ```
    #[must_use]
    pub fn texture(&self) -> SoilTexture {
        let sand = self.sand;
        let silt = self.silt;
        let clay = self.clay;

        // Heavy clay region
        if clay >= 0.40 {
            if sand >= 0.45 {
                SoilTexture::SandyClay
            } else if silt >= 0.40 {
                SoilTexture::SiltyClay
            } else {
                SoilTexture::Clay
            }
        }
        // Sandy clay loam band (20-35% clay, high sand)
        else if (0.20..0.35).contains(&clay) && sand >= 0.45 {
            SoilTexture::SandyClayLoam
        }
        // Clay loam region (27-40% clay)
        else if (0.27..0.40).contains(&clay) {
            if silt >= 0.40 {
                SoilTexture::SiltyClayLoam
            } else {
                SoilTexture::ClayLoam
            }
        }
        // Sandy clay loam (remaining 35-40% clay, high sand)
        else if clay >= 0.35 && sand >= 0.45 {
            SoilTexture::SandyClayLoam
        }
        // Sand (>=85% sand, <=10% clay)
        else if sand >= 0.85 && clay < 0.10 {
            SoilTexture::Sand
        }
        // Loamy sand (70-90% sand, <=15% clay)
        else if (0.70..0.90).contains(&sand) && clay < 0.15 {
            SoilTexture::LoamySand
        }
        // Sandy loam (high sand, moderate fines)
        else if sand >= 0.43 && clay < 0.20 && silt < 0.50 {
            SoilTexture::SandyLoam
        }
        // Silt (>=80% silt, <=12% clay)
        else if silt >= 0.80 && clay < 0.12 {
            SoilTexture::Silt
        }
        // Silt loam (>=50% silt, <=27% clay)
        else if silt >= 0.50 && clay < 0.27 {
            SoilTexture::SiltLoam
        }
        // Loam (the middle ground)
        else {
            SoilTexture::Loam
        }
    }
}

// ---------------------------------------------------------------------------
// USDA Soil Taxonomy — Orders
// ---------------------------------------------------------------------------

/// USDA Soil Taxonomy order -- the 12 top-level soil classifications.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert_eq!(SoilOrder::ALL.len(), 12);
/// assert_eq!(SoilOrder::Mollisol.fertility(), SoilFertility::High);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SoilOrder {
    /// Soils with subsurface clay accumulation and moderate-high base saturation.
    Alfisol,
    /// Soils formed in volcanic ash with high water-holding capacity.
    Andisol,
    /// Dry soils of arid regions with accumulations of salts or carbonates.
    Aridisol,
    /// Young soils with little profile development (recently deposited).
    Entisol,
    /// Soils with permafrost within 2m of the surface.
    Gelisol,
    /// Organic soils (peat/muck) with >20% organic matter.
    Histosol,
    /// Young soils with beginning horizon development.
    Inceptisol,
    /// Dark, fertile prairie/grassland soils with thick organic-rich A horizon.
    Mollisol,
    /// Highly weathered tropical soils dominated by iron/aluminium oxides.
    Oxisol,
    /// Acidic forest soils with a subsurface accumulation of humus and Al/Fe.
    Spodosol,
    /// Highly weathered soils with low base saturation and clay accumulation.
    Ultisol,
    /// Clay-rich soils that shrink and swell with moisture changes.
    Vertisol,
}

impl SoilOrder {
    /// All 12 soil orders.
    pub const ALL: &'static [SoilOrder] = &[
        Self::Alfisol,
        Self::Andisol,
        Self::Aridisol,
        Self::Entisol,
        Self::Gelisol,
        Self::Histosol,
        Self::Inceptisol,
        Self::Mollisol,
        Self::Oxisol,
        Self::Spodosol,
        Self::Ultisol,
        Self::Vertisol,
    ];

    /// Typical climate/environment for this soil order.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let env = SoilOrder::Mollisol.typical_environment();
    /// assert_eq!(env, "grasslands/prairies");
    /// ```
    #[must_use]
    pub fn typical_environment(&self) -> &'static str {
        match self {
            Self::Alfisol => "temperate deciduous forest",
            Self::Andisol => "volcanic regions",
            Self::Aridisol => "arid/semi-arid deserts",
            Self::Entisol => "recent deposits (floodplains, dunes)",
            Self::Gelisol => "permafrost regions (tundra)",
            Self::Histosol => "wetlands (bogs, marshes)",
            Self::Inceptisol => "young landscapes (mountains, river terraces)",
            Self::Mollisol => "grasslands/prairies",
            Self::Oxisol => "tropical rainforest",
            Self::Spodosol => "coniferous forest (boreal/cool humid)",
            Self::Ultisol => "subtropical humid forest",
            Self::Vertisol => "seasonal wet-dry climates",
        }
    }

    /// Typical fertility level.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// assert_eq!(SoilOrder::Oxisol.fertility(), SoilFertility::VeryLow);
    /// ```
    #[must_use]
    pub fn fertility(&self) -> SoilFertility {
        match self {
            Self::Mollisol | Self::Alfisol | Self::Andisol => SoilFertility::High,
            Self::Vertisol | Self::Inceptisol | Self::Entisol => SoilFertility::Moderate,
            Self::Histosol | Self::Aridisol | Self::Gelisol => SoilFertility::Low,
            Self::Ultisol | Self::Spodosol | Self::Oxisol => SoilFertility::VeryLow,
        }
    }
}

/// Soil fertility classification.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert!(SoilFertility::High > SoilFertility::VeryLow);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum SoilFertility {
    VeryLow,
    Low,
    Moderate,
    High,
}

// ---------------------------------------------------------------------------
// Soil Horizons
// ---------------------------------------------------------------------------

/// Master soil horizon designation.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let ht = HorizonType::A;
/// assert_eq!(ht, HorizonType::A);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum HorizonType {
    /// Organic layer (decomposing plant material on surface).
    O,
    /// Topsoil — mineral horizon with organic matter accumulation.
    A,
    /// Eluviation zone — leached of clay, iron, and organic matter.
    E,
    /// Subsoil — zone of accumulation (illuviation) of clay, iron, carbonates.
    B,
    /// Parent material — partially weathered rock.
    C,
    /// Bedrock — unweathered rock.
    R,
}

/// A single soil horizon with properties.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let h = SoilHorizon {
///     horizon_type: HorizonType::A,
///     depth_top_cm: 0.0,
///     depth_bottom_cm: 30.0,
///     organic_matter: 0.04,
///     ph: 6.5,
///     texture: SoilTexture::Loam,
///     color: "10YR 3/2".into(),
/// };
/// assert!((h.thickness_cm() - 30.0).abs() < 0.01);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SoilHorizon {
    /// Horizon type (O, A, E, B, C, R).
    pub horizon_type: HorizonType,
    /// Depth to top of horizon in cm.
    pub depth_top_cm: f64,
    /// Depth to bottom of horizon in cm.
    pub depth_bottom_cm: f64,
    /// Organic matter content as fraction (0.0-1.0).
    pub organic_matter: f64,
    /// pH value (0-14).
    pub ph: f64,
    /// Texture of this horizon.
    pub texture: SoilTexture,
    /// Color (Munsell notation or description).
    pub color: String,
}

impl SoilHorizon {
    /// Thickness of this horizon in cm.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let h = SoilHorizon {
    ///     horizon_type: HorizonType::B,
    ///     depth_top_cm: 30.0,
    ///     depth_bottom_cm: 80.0,
    ///     organic_matter: 0.01,
    ///     ph: 6.0,
    ///     texture: SoilTexture::ClayLoam,
    ///     color: "7.5YR 4/4".into(),
    /// };
    /// assert!((h.thickness_cm() - 50.0).abs() < 0.01);
    /// ```
    #[must_use]
    pub fn thickness_cm(&self) -> f64 {
        self.depth_bottom_cm - self.depth_top_cm
    }
}

/// A complete soil profile -- a vertical sequence of horizons.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let profile = SoilProfile {
///     horizons: vec![SoilHorizon {
///         horizon_type: HorizonType::A,
///         depth_top_cm: 0.0,
///         depth_bottom_cm: 25.0,
///         organic_matter: 0.03,
///         ph: 6.0,
///         texture: SoilTexture::SandyLoam,
///         color: "10YR 3/2".into(),
///     }],
///     location: "field site".into(),
/// };
/// assert!((profile.total_depth_cm() - 25.0).abs() < 0.01);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SoilProfile {
    pub horizons: Vec<SoilHorizon>,
    pub location: String,
}

impl SoilProfile {
    /// Total depth of the profile in cm.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let profile = SoilProfile {
    ///     horizons: vec![SoilHorizon {
    ///         horizon_type: HorizonType::A,
    ///         depth_top_cm: 0.0,
    ///         depth_bottom_cm: 40.0,
    ///         organic_matter: 0.04,
    ///         ph: 6.5,
    ///         texture: SoilTexture::Loam,
    ///         color: "dark".into(),
    ///     }],
    ///     location: "site".into(),
    /// };
    /// assert!((profile.total_depth_cm() - 40.0).abs() < 0.01);
    /// ```
    #[must_use]
    pub fn total_depth_cm(&self) -> f64 {
        self.horizons
            .iter()
            .map(|h| h.depth_bottom_cm)
            .fold(0.0_f64, f64::max)
    }

    /// Check if the profile has a specific horizon type.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let profile = SoilProfile {
    ///     horizons: vec![SoilHorizon {
    ///         horizon_type: HorizonType::A,
    ///         depth_top_cm: 0.0,
    ///         depth_bottom_cm: 30.0,
    ///         organic_matter: 0.03,
    ///         ph: 6.0,
    ///         texture: SoilTexture::Loam,
    ///         color: "dark".into(),
    ///     }],
    ///     location: "site".into(),
    /// };
    /// assert!(profile.has_horizon(HorizonType::A));
    /// assert!(!profile.has_horizon(HorizonType::B));
    /// ```
    #[must_use]
    pub fn has_horizon(&self, ht: HorizonType) -> bool {
        self.horizons.iter().any(|h| h.horizon_type == ht)
    }

    /// Get the A horizon (topsoil) if present.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let profile = SoilProfile {
    ///     horizons: vec![SoilHorizon {
    ///         horizon_type: HorizonType::A,
    ///         depth_top_cm: 0.0,
    ///         depth_bottom_cm: 30.0,
    ///         organic_matter: 0.04,
    ///         ph: 6.5,
    ///         texture: SoilTexture::Loam,
    ///         color: "dark".into(),
    ///     }],
    ///     location: "site".into(),
    /// };
    /// assert!(profile.a_horizon().is_some());
    /// ```
    #[must_use]
    pub fn a_horizon(&self) -> Option<&SoilHorizon> {
        self.horizons
            .iter()
            .find(|h| h.horizon_type == HorizonType::A)
    }

    /// Get the B horizon (subsoil) if present.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let profile = SoilProfile {
    ///     horizons: vec![SoilHorizon {
    ///         horizon_type: HorizonType::A,
    ///         depth_top_cm: 0.0,
    ///         depth_bottom_cm: 30.0,
    ///         organic_matter: 0.03,
    ///         ph: 6.0,
    ///         texture: SoilTexture::Loam,
    ///         color: "dark".into(),
    ///     }],
    ///     location: "site".into(),
    /// };
    /// assert!(profile.b_horizon().is_none());
    /// ```
    #[must_use]
    pub fn b_horizon(&self) -> Option<&SoilHorizon> {
        self.horizons
            .iter()
            .find(|h| h.horizon_type == HorizonType::B)
    }

    /// Average organic matter content of the A horizon.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let profile = SoilProfile {
    ///     horizons: vec![SoilHorizon {
    ///         horizon_type: HorizonType::A,
    ///         depth_top_cm: 0.0,
    ///         depth_bottom_cm: 30.0,
    ///         organic_matter: 0.05,
    ///         ph: 6.5,
    ///         texture: SoilTexture::Loam,
    ///         color: "dark".into(),
    ///     }],
    ///     location: "site".into(),
    /// };
    /// let om = profile.topsoil_organic_matter().unwrap();
    /// assert!((om - 0.05).abs() < 0.001);
    /// ```
    #[must_use]
    pub fn topsoil_organic_matter(&self) -> Option<f64> {
        self.a_horizon().map(|h| h.organic_matter)
    }

    /// Simplified soil order classification from profile properties.
    ///
    /// This is a simplified key -- real taxonomy uses many more diagnostic criteria.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let profile = SoilProfile {
    ///     horizons: vec![SoilHorizon {
    ///         horizon_type: HorizonType::A,
    ///         depth_top_cm: 0.0,
    ///         depth_bottom_cm: 20.0,
    ///         organic_matter: 0.02,
    ///         ph: 6.0,
    ///         texture: SoilTexture::Sand,
    ///         color: "10YR 5/3".into(),
    ///     }],
    ///     location: "floodplain".into(),
    /// };
    /// assert_eq!(profile.classify_order(), SoilOrder::Entisol);
    /// ```
    #[must_use]
    pub fn classify_order(&self) -> SoilOrder {
        let a = self.a_horizon();
        let b = self.b_horizon();

        // Histosol: very high organic matter
        if a.is_some_and(|h| h.organic_matter > 0.20) {
            return SoilOrder::Histosol;
        }

        // Vertisol: very high clay content
        if a.is_some_and(|h| h.texture == SoilTexture::Clay)
            && b.is_some_and(|h| h.texture == SoilTexture::Clay)
        {
            return SoilOrder::Vertisol;
        }

        // Mollisol: thick, dark, organic-rich A horizon
        if a.is_some_and(|h| h.organic_matter > 0.03 && h.thickness_cm() > 25.0 && h.ph > 5.5) {
            return SoilOrder::Mollisol;
        }

        // Spodosol: E horizon above B with acidic pH
        if self.has_horizon(HorizonType::E) && a.is_some_and(|h| h.ph < 5.0) {
            return SoilOrder::Spodosol;
        }

        // Oxisol: very thick, clay-rich B, low pH
        if b.is_some_and(|h| h.thickness_cm() > 100.0 && h.ph < 5.5) {
            return SoilOrder::Oxisol;
        }

        // Aridisol: very low organic matter, high pH
        if a.is_some_and(|h| h.organic_matter < 0.01 && h.ph > 7.5) {
            return SoilOrder::Aridisol;
        }

        // Ultisol: clay-rich B, acidic
        if b.is_some_and(|h| {
            matches!(
                h.texture,
                SoilTexture::Clay | SoilTexture::SandyClay | SoilTexture::SiltyClay
            ) && h.ph < 5.5
        }) {
            return SoilOrder::Ultisol;
        }

        // Alfisol: clay-rich B, moderate-high pH
        if b.is_some_and(|h| {
            matches!(
                h.texture,
                SoilTexture::Clay
                    | SoilTexture::ClayLoam
                    | SoilTexture::SandyClayLoam
                    | SoilTexture::SiltyClayLoam
            ) && h.ph >= 5.5
        }) {
            return SoilOrder::Alfisol;
        }

        // Entisol: no B horizon at all
        if !self.has_horizon(HorizonType::B) {
            return SoilOrder::Entisol;
        }

        // Inceptisol: weak B horizon development (default for young soils)
        SoilOrder::Inceptisol
    }
}

/// Soil pH classification.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert_eq!(classify_ph(6.8), SoilPhClass::Neutral);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SoilPhClass {
    UltraAcid,
    ExtremelyAcid,
    VeryStronglyAcid,
    StronglyAcid,
    ModeratelyAcid,
    SlightlyAcid,
    Neutral,
    SlightlyAlkaline,
    ModeratelyAlkaline,
    StronglyAlkaline,
    VeryStronglyAlkaline,
}

/// Classify soil pH into USDA categories.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert_eq!(classify_ph(4.0), SoilPhClass::ExtremelyAcid);
/// assert_eq!(classify_ph(7.0), SoilPhClass::Neutral);
/// assert_eq!(classify_ph(8.5), SoilPhClass::StronglyAlkaline);
/// ```
#[must_use]
pub fn classify_ph(ph: f64) -> SoilPhClass {
    if ph < 3.5 {
        SoilPhClass::UltraAcid
    } else if ph < 4.5 {
        SoilPhClass::ExtremelyAcid
    } else if ph < 5.0 {
        SoilPhClass::VeryStronglyAcid
    } else if ph < 5.5 {
        SoilPhClass::StronglyAcid
    } else if ph < 6.0 {
        SoilPhClass::ModeratelyAcid
    } else if ph < 6.5 {
        SoilPhClass::SlightlyAcid
    } else if ph < 7.3 {
        SoilPhClass::Neutral
    } else if ph < 7.8 {
        SoilPhClass::SlightlyAlkaline
    } else if ph < 8.4 {
        SoilPhClass::ModeratelyAlkaline
    } else if ph < 9.0 {
        SoilPhClass::StronglyAlkaline
    } else {
        SoilPhClass::VeryStronglyAlkaline
    }
}

/// Cation exchange capacity estimate from clay content and organic matter.
///
/// CEC = 0.5 x clay% + 2.0 x OM%
///
/// - `clay_fraction`: clay content as fraction (0.0-1.0)
/// - `organic_matter_fraction`: OM as fraction (0.0-1.0)
///
/// Returns CEC in meq/100g (milliequivalents per 100 grams).
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let cec = cation_exchange_capacity(0.30, 0.05);
/// assert!((cec - 25.0).abs() < 0.01);
/// ```
#[must_use]
pub fn cation_exchange_capacity(clay_fraction: f64, organic_matter_fraction: f64) -> f64 {
    0.5 * (clay_fraction * 100.0) + 2.0 * (organic_matter_fraction * 100.0)
}

/// Soil water holding capacity estimate.
///
/// Returns available water capacity in mm/m depth.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let awc = available_water_capacity(SoilTexture::Loam);
/// assert!((awc - 170.0).abs() < 0.01);
/// ```
#[must_use]
pub fn available_water_capacity(texture: SoilTexture) -> f64 {
    match texture {
        SoilTexture::Sand => 60.0,
        SoilTexture::LoamySand => 80.0,
        SoilTexture::SandyLoam => 120.0,
        SoilTexture::Loam => 170.0,
        SoilTexture::SiltLoam => 200.0,
        SoilTexture::Silt => 180.0,
        SoilTexture::SandyClayLoam => 140.0,
        SoilTexture::ClayLoam => 170.0,
        SoilTexture::SiltyClayLoam => 190.0,
        SoilTexture::SandyClay => 130.0,
        SoilTexture::SiltyClay => 160.0,
        SoilTexture::Clay => 150.0,
    }
}

/// Saturated hydraulic conductivity estimate from texture in mm/hr.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let k = hydraulic_conductivity_mm_hr(SoilTexture::Sand);
/// assert!((k - 210.0).abs() < 0.01);
/// ```
#[must_use]
pub fn hydraulic_conductivity_mm_hr(texture: SoilTexture) -> f64 {
    match texture {
        SoilTexture::Sand => 210.0,
        SoilTexture::LoamySand => 61.0,
        SoilTexture::SandyLoam => 26.0,
        SoilTexture::Loam => 13.0,
        SoilTexture::SiltLoam => 7.0,
        SoilTexture::Silt => 7.0,
        SoilTexture::SandyClayLoam => 4.0,
        SoilTexture::ClayLoam => 2.0,
        SoilTexture::SiltyClayLoam => 1.5,
        SoilTexture::SandyClay => 1.2,
        SoilTexture::SiltyClay => 0.9,
        SoilTexture::Clay => 0.6,
    }
}

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

    #[test]
    fn valid_composition() {
        assert!(SoilComposition::new(0.4, 0.4, 0.2).is_some());
    }

    #[test]
    fn invalid_composition_sum() {
        assert!(SoilComposition::new(0.5, 0.5, 0.5).is_none());
    }

    #[test]
    fn negative_fraction_rejected() {
        assert!(SoilComposition::new(-0.1, 0.6, 0.5).is_none());
    }

    // --- All 12 texture classes ---

    #[test]
    fn texture_sand() {
        let s = SoilComposition::new(0.90, 0.05, 0.05).unwrap();
        assert_eq!(s.texture(), SoilTexture::Sand);
    }

    #[test]
    fn texture_loamy_sand() {
        let s = SoilComposition::new(0.80, 0.10, 0.10).unwrap();
        assert_eq!(s.texture(), SoilTexture::LoamySand);
    }

    #[test]
    fn texture_sandy_loam() {
        let s = SoilComposition::new(0.60, 0.25, 0.15).unwrap();
        assert_eq!(s.texture(), SoilTexture::SandyLoam);
    }

    #[test]
    fn texture_loam() {
        let s = SoilComposition::new(0.40, 0.40, 0.20).unwrap();
        assert_eq!(s.texture(), SoilTexture::Loam);
    }

    #[test]
    fn texture_silt_loam() {
        let s = SoilComposition::new(0.20, 0.65, 0.15).unwrap();
        assert_eq!(s.texture(), SoilTexture::SiltLoam);
    }

    #[test]
    fn texture_silt() {
        let s = SoilComposition::new(0.05, 0.88, 0.07).unwrap();
        assert_eq!(s.texture(), SoilTexture::Silt);
    }

    #[test]
    fn texture_sandy_clay_loam() {
        let s = SoilComposition::new(0.55, 0.15, 0.30).unwrap();
        assert_eq!(s.texture(), SoilTexture::SandyClayLoam);
    }

    #[test]
    fn texture_clay_loam() {
        let s = SoilComposition::new(0.30, 0.35, 0.35).unwrap();
        assert_eq!(s.texture(), SoilTexture::ClayLoam);
    }

    #[test]
    fn texture_silty_clay_loam() {
        let s = SoilComposition::new(0.10, 0.55, 0.35).unwrap();
        assert_eq!(s.texture(), SoilTexture::SiltyClayLoam);
    }

    #[test]
    fn texture_sandy_clay() {
        let s = SoilComposition::new(0.50, 0.05, 0.45).unwrap();
        assert_eq!(s.texture(), SoilTexture::SandyClay);
    }

    #[test]
    fn texture_silty_clay() {
        let s = SoilComposition::new(0.05, 0.50, 0.45).unwrap();
        assert_eq!(s.texture(), SoilTexture::SiltyClay);
    }

    #[test]
    fn texture_clay() {
        let s = SoilComposition::new(0.20, 0.30, 0.50).unwrap();
        assert_eq!(s.texture(), SoilTexture::Clay);
    }

    // --- Soil taxonomy tests ---

    #[test]
    fn twelve_soil_orders() {
        assert_eq!(SoilOrder::ALL.len(), 12);
    }

    #[test]
    fn mollisol_high_fertility() {
        assert_eq!(SoilOrder::Mollisol.fertility(), SoilFertility::High);
    }

    #[test]
    fn oxisol_very_low_fertility() {
        assert_eq!(SoilOrder::Oxisol.fertility(), SoilFertility::VeryLow);
    }

    #[test]
    fn fertility_ordering() {
        assert!(SoilFertility::High > SoilFertility::VeryLow);
        assert!(SoilFertility::Moderate > SoilFertility::Low);
    }

    fn make_mollisol_profile() -> SoilProfile {
        SoilProfile {
            horizons: vec![
                SoilHorizon {
                    horizon_type: HorizonType::A,
                    depth_top_cm: 0.0,
                    depth_bottom_cm: 40.0,
                    organic_matter: 0.05,
                    ph: 6.5,
                    texture: SoilTexture::Loam,
                    color: "10YR 2/1".into(),
                },
                SoilHorizon {
                    horizon_type: HorizonType::B,
                    depth_top_cm: 40.0,
                    depth_bottom_cm: 100.0,
                    organic_matter: 0.01,
                    ph: 6.8,
                    texture: SoilTexture::ClayLoam,
                    color: "10YR 4/3".into(),
                },
                SoilHorizon {
                    horizon_type: HorizonType::C,
                    depth_top_cm: 100.0,
                    depth_bottom_cm: 150.0,
                    organic_matter: 0.002,
                    ph: 7.0,
                    texture: SoilTexture::SiltLoam,
                    color: "10YR 5/4".into(),
                },
            ],
            location: "Kansas prairie".into(),
        }
    }

    #[test]
    fn classify_mollisol() {
        let profile = make_mollisol_profile();
        assert_eq!(profile.classify_order(), SoilOrder::Mollisol);
    }

    #[test]
    fn classify_entisol_no_b_horizon() {
        let profile = SoilProfile {
            horizons: vec![SoilHorizon {
                horizon_type: HorizonType::A,
                depth_top_cm: 0.0,
                depth_bottom_cm: 20.0,
                organic_matter: 0.02,
                ph: 6.0,
                texture: SoilTexture::Sand,
                color: "10YR 5/3".into(),
            }],
            location: "River floodplain".into(),
        };
        assert_eq!(profile.classify_order(), SoilOrder::Entisol);
    }

    #[test]
    fn classify_histosol_high_om() {
        let profile = SoilProfile {
            horizons: vec![
                SoilHorizon {
                    horizon_type: HorizonType::O,
                    depth_top_cm: 0.0,
                    depth_bottom_cm: 10.0,
                    organic_matter: 0.50,
                    ph: 4.5,
                    texture: SoilTexture::Loam,
                    color: "5YR 2/1".into(),
                },
                SoilHorizon {
                    horizon_type: HorizonType::A,
                    depth_top_cm: 10.0,
                    depth_bottom_cm: 50.0,
                    organic_matter: 0.30,
                    ph: 4.5,
                    texture: SoilTexture::SiltLoam,
                    color: "10YR 2/1".into(),
                },
            ],
            location: "Bog".into(),
        };
        assert_eq!(profile.classify_order(), SoilOrder::Histosol);
    }

    #[test]
    fn profile_total_depth() {
        let profile = make_mollisol_profile();
        assert!((profile.total_depth_cm() - 150.0).abs() < 0.01);
    }

    #[test]
    fn horizon_thickness() {
        let h = SoilHorizon {
            horizon_type: HorizonType::A,
            depth_top_cm: 0.0,
            depth_bottom_cm: 30.0,
            organic_matter: 0.04,
            ph: 6.0,
            texture: SoilTexture::Loam,
            color: "dark".into(),
        };
        assert!((h.thickness_cm() - 30.0).abs() < 0.01);
    }

    #[test]
    fn ph_classification() {
        assert_eq!(classify_ph(4.0), SoilPhClass::ExtremelyAcid);
        assert_eq!(classify_ph(5.2), SoilPhClass::StronglyAcid);
        assert_eq!(classify_ph(6.8), SoilPhClass::Neutral);
        assert_eq!(classify_ph(7.5), SoilPhClass::SlightlyAlkaline);
        assert_eq!(classify_ph(8.5), SoilPhClass::StronglyAlkaline);
    }

    #[test]
    fn cec_increases_with_clay_and_om() {
        let low = cation_exchange_capacity(0.10, 0.01);
        let high = cation_exchange_capacity(0.50, 0.05);
        assert!(high > low);
    }

    #[test]
    fn awc_sand_lowest() {
        let sand_awc = available_water_capacity(SoilTexture::Sand);
        let loam_awc = available_water_capacity(SoilTexture::Loam);
        assert!(sand_awc < loam_awc);
    }

    #[test]
    fn ksat_sand_highest() {
        let sand_k = hydraulic_conductivity_mm_hr(SoilTexture::Sand);
        let clay_k = hydraulic_conductivity_mm_hr(SoilTexture::Clay);
        assert!(sand_k > clay_k);
    }

    #[test]
    fn topsoil_organic_matter_from_profile() {
        let profile = make_mollisol_profile();
        let om = profile.topsoil_organic_matter().unwrap();
        assert!((om - 0.05).abs() < 0.001);
    }
}