iztro 0.6.0

Strongly typed Zi Wei Dou Shu chart generation aligned with iztro.
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
//! Serializable horoscope facade payload snapshot.
//!
//! [`HoroscopeFacadeSnapshot`] is an export/facade layer, not a new engine layer.
//! It wraps the already-modeled full horoscope facts into one deterministic,
//! serializable payload that moves toward the upstream `iztro@2.5.8`
//! `FunctionalAstrolabe#horoscope` shape, while staying explicit about the parts
//! that remain deferred.
//!
//! It is assembled only from existing facts:
//!
//! * the normalized supported-field blocks of [`HoroscopeSupportedFieldsSnapshot`]
//!   (decadal/age/yearly/monthly/daily/hourly), reused verbatim;
//! * the target numeric solar/lunar/time context retained by full stack assembly,
//!   with a lunar-only fallback for manually assembled full-layer charts;
//! * a minimal natal astrolabe snapshot derived from [`HoroscopeChart::natal`];
//! * the runtime palace projections of [`HoroscopeRuntime`] for the Life palace
//!   across each modeled scope.
//!
//! No placement logic is added here: every projection is produced by
//! [`HoroscopeRuntime`], and every supported-field block by
//! [`HoroscopeSupportedFieldsSnapshot`].
//!
//! Deferred, and intentionally absent from this payload:
//!
//! * the upstream localized `lunarDate` and `solarDate` strings;
//! * the complete upstream natal astrolabe payload with helper/query methods,
//!   localized labels, BaZi strings, decadal ranges, or age arrays;
//! * the runtime query helpers (`hasHoroscopeStars` and friends), which remain
//!   [`HoroscopeRuntime`] methods rather than precomputed DTO fields;
//! * full BaZi output, bindings, renderers, rules, and narrative.

use crate::core::{
    error::ChartError,
    labels::zh_cn,
    model::{
        bureau::FiveElementBureau,
        calendar::Gender,
        chart::{
            Chart, DecorativeStarFamily, DecorativeStarPlacement, HoroscopeChart,
            HoroscopeLunarDate, HoroscopePalaceProjection, HoroscopeProjectionMutagenActivation,
            HoroscopeRuntime, HoroscopeSolarDate, HoroscopeSupportedFieldsSnapshot,
            HoroscopeSurroundPalaces, Palace, PalaceName, StarPlacement, TemporalContext,
        },
        star::{
            Brightness, StarCategory, StarKind, StarName,
            mutagen::{Mutagen, Scope},
        },
    },
};
use lunar_lite::{EarthlyBranch, HeavenlyStem};
use serde::{Deserialize, Serialize};

/// Scopes the facade projects the Life palace through, mirroring the upstream
/// split between `agePalace()` (the [`HoroscopeFacadeSnapshot::age_palace`] field)
/// and `palace(name, scope)` (these scopes). `age` is excluded here because it is
/// already exposed through `age_palace`.
const FACADE_PROJECTION_SCOPES: [Scope; 6] = [
    Scope::Natal,
    Scope::Decadal,
    Scope::Yearly,
    Scope::Monthly,
    Scope::Daily,
    Scope::Hourly,
];

/// Upstream-like, serializable snapshot of the modeled full horoscope surface.
///
/// The decadal/age/yearly/monthly/daily/hourly supported-field blocks are flattened
/// to the top level (reused from [`HoroscopeSupportedFieldsSnapshot`]); the facade
/// adds the target `context` and the Life-palace runtime projections.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct HoroscopeFacadeSnapshot {
    #[serde(flatten)]
    supported_fields: HoroscopeSupportedFieldsSnapshot,
    context: HoroscopeFacadeContext,
    astrolabe: NatalFacadeSnapshot,
    age_palace: HoroscopePalaceProjectionSnapshot,
    palace_projections: Vec<HoroscopePalaceProjectionSnapshot>,
    surround_palaces: Vec<HoroscopeSurroundPalacesSnapshot>,
}

impl HoroscopeFacadeSnapshot {
    /// Builds the facade payload from a full horoscope chart.
    ///
    /// Requires the full six-layer horoscope stack: the supported-field blocks,
    /// the runtime projections, and target context all read from modeled facts
    /// and so propagate the same
    /// [`ChartError`] those builders raise when a required layer, palace layout,
    /// or temporal context is missing.
    pub fn from_horoscope_chart(chart: &HoroscopeChart) -> Result<Self, ChartError> {
        let supported_fields = HoroscopeSupportedFieldsSnapshot::from_horoscope_chart(chart)?;
        let context = HoroscopeFacadeContext::from_horoscope_chart(chart)?;
        let astrolabe = NatalFacadeSnapshot::from_chart(chart.natal());
        let runtime = HoroscopeRuntime::new(chart)?;

        let age_palace = HoroscopePalaceProjectionSnapshot::from_projection(&runtime.age_palace()?);

        let mut palace_projections = Vec::with_capacity(FACADE_PROJECTION_SCOPES.len());
        let mut surround_palaces = Vec::with_capacity(FACADE_PROJECTION_SCOPES.len());
        for scope in FACADE_PROJECTION_SCOPES {
            palace_projections.push(HoroscopePalaceProjectionSnapshot::from_projection(
                &runtime.palace(scope, PalaceName::Life)?,
            ));
            surround_palaces.push(HoroscopeSurroundPalacesSnapshot::from_surround(
                &runtime.surround_palaces(scope, PalaceName::Life)?,
            ));
        }

        Ok(Self {
            supported_fields,
            context,
            astrolabe,
            age_palace,
            palace_projections,
            surround_palaces,
        })
    }

    /// Returns the reused supported-field blocks for every horoscope scope.
    pub const fn supported_fields(&self) -> &HoroscopeSupportedFieldsSnapshot {
        &self.supported_fields
    }

    /// Returns the target facade context.
    pub const fn context(&self) -> &HoroscopeFacadeContext {
        &self.context
    }

    /// Returns the minimal natal astrolabe snapshot derived from the natal chart.
    pub const fn astrolabe(&self) -> &NatalFacadeSnapshot {
        &self.astrolabe
    }

    /// Returns the nominal-age Life palace projection (`agePalace`).
    pub const fn age_palace(&self) -> &HoroscopePalaceProjectionSnapshot {
        &self.age_palace
    }

    /// Returns the Life palace projections across each modeled scope.
    pub fn palace_projections(&self) -> &[HoroscopePalaceProjectionSnapshot] {
        &self.palace_projections
    }

    /// Returns the Life palace 三方四正 projections across each modeled scope.
    pub fn surround_palaces(&self) -> &[HoroscopeSurroundPalacesSnapshot] {
        &self.surround_palaces
    }
}

/// Minimal serializable natal astrolabe snapshot embedded in the horoscope facade.
///
/// This is not the full upstream `astrolabe` object. It contains only stable
/// natal facts already modeled by [`Chart`] and deliberately excludes temporal
/// overlays, BaZi strings, runtime query helpers, decadal ranges, age arrays,
/// render data, rules, and readings.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NatalFacadeSnapshot {
    gender: Gender,
    birth_year_stem: HeavenlyStem,
    birth_year_stem_zh: String,
    birth_year_branch: EarthlyBranch,
    birth_year_branch_zh: String,
    five_element_bureau: Option<FiveElementBureau>,
    life_palace_branch: Option<EarthlyBranch>,
    life_palace_branch_zh: Option<String>,
    body_palace_branch: Option<EarthlyBranch>,
    body_palace_branch_zh: Option<String>,
    palaces: Vec<NatalFacadePalaceSnapshot>,
}

impl NatalFacadeSnapshot {
    /// Builds the natal facade snapshot from an already-assembled natal chart.
    pub fn from_chart(chart: &Chart) -> Self {
        let life_palace_branch = chart.life_palace().map(Palace::branch);
        let body_palace_branch = chart.body_palace_branch();
        Self {
            gender: chart.birth_context().gender(),
            birth_year_stem: chart.birth_year().stem(),
            birth_year_stem_zh: zh_cn::heavenly_stem_zh(chart.birth_year().stem()).to_owned(),
            birth_year_branch: chart.birth_year().branch(),
            birth_year_branch_zh: zh_cn::earthly_branch_zh(chart.birth_year().branch()).to_owned(),
            five_element_bureau: chart.five_element_bureau(),
            life_palace_branch,
            life_palace_branch_zh: life_palace_branch
                .map(|branch| zh_cn::earthly_branch_zh(branch).to_owned()),
            body_palace_branch,
            body_palace_branch_zh: body_palace_branch
                .map(|branch| zh_cn::earthly_branch_zh(branch).to_owned()),
            palaces: chart
                .palaces()
                .iter()
                .map(|palace| NatalFacadePalaceSnapshot::from_palace(chart, palace))
                .collect(),
        }
    }

    /// Returns the retained gender marker.
    pub const fn gender(&self) -> Gender {
        self.gender
    }

    /// Returns the birth-year Heavenly Stem.
    pub const fn birth_year_stem(&self) -> HeavenlyStem {
        self.birth_year_stem
    }

    /// Returns the Chinese label for the birth-year Heavenly Stem.
    pub fn birth_year_stem_zh(&self) -> &str {
        &self.birth_year_stem_zh
    }

    /// Returns the birth-year Earthly Branch.
    pub const fn birth_year_branch(&self) -> EarthlyBranch {
        self.birth_year_branch
    }

    /// Returns the Chinese label for the birth-year Earthly Branch.
    pub fn birth_year_branch_zh(&self) -> &str {
        &self.birth_year_branch_zh
    }

    /// Returns the five-element bureau, if modeled.
    pub const fn five_element_bureau(&self) -> Option<FiveElementBureau> {
        self.five_element_bureau
    }

    /// Returns the Life Palace branch, if modeled.
    pub const fn life_palace_branch(&self) -> Option<EarthlyBranch> {
        self.life_palace_branch
    }

    /// Returns the Chinese label for the Life Palace branch, if modeled.
    pub fn life_palace_branch_zh(&self) -> Option<&str> {
        self.life_palace_branch_zh.as_deref()
    }

    /// Returns the Body Palace branch, if modeled.
    pub const fn body_palace_branch(&self) -> Option<EarthlyBranch> {
        self.body_palace_branch
    }

    /// Returns the Chinese label for the Body Palace branch, if modeled.
    pub fn body_palace_branch_zh(&self) -> Option<&str> {
        self.body_palace_branch_zh.as_deref()
    }

    /// Returns the natal palace snapshots in chart order.
    pub fn palaces(&self) -> &[NatalFacadePalaceSnapshot] {
        &self.palaces
    }
}

/// Minimal natal palace snapshot for the facade astrolabe.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NatalFacadePalaceSnapshot {
    branch: EarthlyBranch,
    branch_zh: String,
    name: PalaceName,
    name_zh: String,
    stem: HeavenlyStem,
    stem_zh: String,
    roles: Vec<NatalFacadePalaceRole>,
    typed_stars: Vec<NatalFacadeTypedStarSnapshot>,
    decorative_stars: Vec<NatalFacadeDecorativeStarSnapshot>,
}

impl NatalFacadePalaceSnapshot {
    fn from_palace(chart: &Chart, palace: &Palace) -> Self {
        let mut roles = vec![NatalFacadePalaceRole::NatalPalace(palace.name())];
        if chart.is_body_palace_branch(palace.branch()) {
            roles.push(NatalFacadePalaceRole::NatalBodyPalace);
        }

        let mut typed_stars: Vec<NatalFacadeTypedStarSnapshot> = palace
            .stars()
            .iter()
            .map(NatalFacadeTypedStarSnapshot::from_star_placement)
            .collect();
        order_facade_typed_stars(&mut typed_stars);

        let mut decorative_stars: Vec<NatalFacadeDecorativeStarSnapshot> = palace
            .decorative_stars()
            .iter()
            .map(NatalFacadeDecorativeStarSnapshot::from_decorative_star_placement)
            .collect();
        order_facade_decorative_stars(&mut decorative_stars);

        Self {
            branch: palace.branch(),
            branch_zh: zh_cn::earthly_branch_zh(palace.branch()).to_owned(),
            name: palace.name(),
            name_zh: zh_cn::palace_name_zh(palace.name()).to_owned(),
            stem: palace.stem(),
            stem_zh: zh_cn::heavenly_stem_zh(palace.stem()).to_owned(),
            roles,
            typed_stars,
            decorative_stars,
        }
    }

    /// Returns the palace branch.
    pub const fn branch(&self) -> EarthlyBranch {
        self.branch
    }

    /// Returns the Chinese label for the palace branch.
    pub fn branch_zh(&self) -> &str {
        &self.branch_zh
    }

    /// Returns the natal palace name.
    pub const fn name(&self) -> PalaceName {
        self.name
    }

    /// Returns the Chinese label for the natal palace name.
    pub fn name_zh(&self) -> &str {
        &self.name_zh
    }

    /// Returns the natal palace stem.
    pub const fn stem(&self) -> HeavenlyStem {
        self.stem
    }

    /// Returns the Chinese label for the natal palace stem.
    pub fn stem_zh(&self) -> &str {
        &self.stem_zh
    }

    /// Returns role markers for this natal palace.
    pub fn roles(&self) -> &[NatalFacadePalaceRole] {
        &self.roles
    }

    /// Returns typed natal stars in this palace.
    pub fn typed_stars(&self) -> &[NatalFacadeTypedStarSnapshot] {
        &self.typed_stars
    }

    /// Returns decorative natal stars in this palace.
    pub fn decorative_stars(&self) -> &[NatalFacadeDecorativeStarSnapshot] {
        &self.decorative_stars
    }
}

/// Role markers attached to a natal facade palace.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(tag = "kind", content = "palace_name", rename_all = "snake_case")]
pub enum NatalFacadePalaceRole {
    /// The cell contains this natal palace.
    NatalPalace(PalaceName),
    /// The cell is the Body Palace branch.
    NatalBodyPalace,
}

/// Typed natal star DTO for the facade astrolabe.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NatalFacadeTypedStarSnapshot {
    name: StarName,
    name_zh: String,
    kind: StarKind,
    kind_zh: String,
    category: StarCategory,
    brightness: Brightness,
    brightness_zh: String,
    mutagen: Option<Mutagen>,
    mutagen_zh: Option<String>,
    scope: Scope,
}

impl NatalFacadeTypedStarSnapshot {
    fn from_star_placement(placement: &StarPlacement) -> Self {
        Self {
            name: placement.name(),
            name_zh: zh_cn::star_name_zh(placement.name()).to_owned(),
            kind: placement.kind(),
            kind_zh: zh_cn::star_kind_zh(placement.kind()).to_owned(),
            category: placement.category(),
            brightness: placement.brightness(),
            brightness_zh: zh_cn::brightness_zh(placement.brightness()).to_owned(),
            mutagen: placement.mutagen(),
            mutagen_zh: placement
                .mutagen()
                .map(|mutagen| zh_cn::mutagen_zh(mutagen).to_owned()),
            scope: placement.scope(),
        }
    }

    /// Returns the star name.
    pub const fn name(&self) -> StarName {
        self.name
    }

    /// Returns the Chinese label for the star name.
    pub fn name_zh(&self) -> &str {
        &self.name_zh
    }

    /// Returns the fine star kind.
    pub const fn kind(&self) -> StarKind {
        self.kind
    }

    /// Returns the Chinese label for the fine star kind.
    pub fn kind_zh(&self) -> &str {
        &self.kind_zh
    }

    /// Returns the coarse star category.
    pub const fn category(&self) -> StarCategory {
        self.category
    }

    /// Returns the brightness state.
    pub const fn brightness(&self) -> Brightness {
        self.brightness
    }

    /// Returns the Chinese label for the brightness state.
    pub fn brightness_zh(&self) -> &str {
        &self.brightness_zh
    }

    /// Returns the natal mutagen attached to the placement, if present.
    pub const fn mutagen(&self) -> Option<Mutagen> {
        self.mutagen
    }

    /// Returns the Chinese label for the attached mutagen, if present.
    pub fn mutagen_zh(&self) -> Option<&str> {
        self.mutagen_zh.as_deref()
    }

    /// Returns the placement scope.
    pub const fn scope(&self) -> Scope {
        self.scope
    }
}

/// Decorative natal star DTO for the facade astrolabe.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NatalFacadeDecorativeStarSnapshot {
    name: StarName,
    name_zh: String,
    family: DecorativeStarFamily,
    family_zh: String,
    scope: Scope,
}

impl NatalFacadeDecorativeStarSnapshot {
    fn from_decorative_star_placement(placement: &DecorativeStarPlacement) -> Self {
        Self {
            name: placement.name(),
            name_zh: zh_cn::star_name_zh(placement.name()).to_owned(),
            family: placement.family(),
            family_zh: zh_cn::decorative_star_family_zh(placement.family()).to_owned(),
            scope: placement.scope(),
        }
    }

    /// Returns the decorative star name.
    pub const fn name(&self) -> StarName {
        self.name
    }

    /// Returns the Chinese label for the decorative star name.
    pub fn name_zh(&self) -> &str {
        &self.name_zh
    }

    /// Returns the decorative star family.
    pub const fn family(&self) -> DecorativeStarFamily {
        self.family
    }

    /// Returns the Chinese label for the decorative star family.
    pub fn family_zh(&self) -> &str {
        &self.family_zh
    }

    /// Returns the placement scope.
    pub const fn scope(&self) -> Scope {
        self.scope
    }
}

/// Deterministic facade ordering policy for exported palace star arrays.
///
/// Core engine placement facts are **order-independent**: a palace is the set of
/// stars placed in it, and the core compatibility tests compare those sets
/// without regard to `Vec` order (Rust and upstream TS `iztro` do not
/// necessarily emit stars in the same order). The facade/export layer, however,
/// must not depend on accidental `Vec` order, so this helper imposes one stable,
/// deterministic ordering for typed natal stars in a serialized palace:
///
/// * sort by `(kind, name, brightness, mutagen)`, using the canonical
///   declaration-order [`Ord`] of each typed enum.
///
/// This is a stable Rust-side canonical order. It is **not** a claim of byte
/// parity with the upstream TS `iztro` palace-star array order; full upstream
/// order parity is deferred unless separately targeted.
fn order_facade_typed_stars(stars: &mut [NatalFacadeTypedStarSnapshot]) {
    stars.sort_by_key(|star| (star.kind, star.name, star.brightness, star.mutagen));
}

/// Deterministic facade ordering policy for exported decorative palace stars.
///
/// Like [`order_facade_typed_stars`], this imposes a stable Rust-side canonical
/// order on the facade/export array without changing which stars are present:
///
/// * sort by `(family, name)`, using the canonical declaration-order [`Ord`] of
///   [`DecorativeStarFamily`] and [`StarName`].
///
/// It is not a claim of upstream TS array-order parity.
fn order_facade_decorative_stars(stars: &mut [NatalFacadeDecorativeStarSnapshot]) {
    stars.sort_by_key(|star| (star.family, star.name));
}

/// Target date/context fields exposed by the facade.
///
/// Full-stack-built charts retain numeric solar date, lunar date, leap-month
/// flag, and upstream `timeIndex`. Manually assembled charts without retained
/// target context still expose the older lunar year/month/day facts derived from
/// temporal layer contexts, with solar date and time index absent.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct HoroscopeFacadeContext {
    solar_date: Option<HoroscopeSolarDate>,
    lunar_date: HoroscopeLunarDate,
    time_index: Option<u8>,
}

impl HoroscopeFacadeContext {
    fn from_horoscope_chart(chart: &HoroscopeChart) -> Result<Self, ChartError> {
        if let Some(context) = chart.target_context() {
            return Ok(Self {
                solar_date: Some(context.solar_date()),
                lunar_date: context.lunar_date(),
                time_index: Some(context.time_index()),
            });
        }

        Ok(Self {
            solar_date: None,
            lunar_date: HoroscopeLunarDate::new(
                lunar_year(chart)?,
                lunar_month(chart)?,
                lunar_day(chart)?,
                false,
            ),
            time_index: None,
        })
    }

    /// Returns the retained target solar date, if available.
    pub const fn solar_date(&self) -> Option<HoroscopeSolarDate> {
        self.solar_date
    }

    /// Returns the target lunar date.
    pub const fn lunar_date(&self) -> HoroscopeLunarDate {
        self.lunar_date
    }

    /// Returns the retained upstream `iztro` target `timeIndex`, if available.
    pub const fn time_index(&self) -> Option<u8> {
        self.time_index
    }
}

/// Serializable form of a [`HoroscopePalaceProjection`].
///
/// Preserves the natal-versus-temporal split exactly as the runtime projection
/// produces it: the natal palace identity and stars stay separate from the
/// period's temporal palace name, temporal stars, and temporal mutagen
/// activations.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct HoroscopePalaceProjectionSnapshot {
    scope: Scope,
    requested_palace_name: PalaceName,
    branch: EarthlyBranch,
    natal_palace_name: PalaceName,
    temporal_palace_name: Option<PalaceName>,
    natal_palace_stem: HeavenlyStem,
    natal_typed_stars: Vec<StarName>,
    natal_decorative_stars: Vec<StarName>,
    temporal_stars: Vec<StarName>,
    temporal_decorative_stars: Vec<StarName>,
    temporal_mutagen_activations: Vec<HoroscopeProjectionMutagenActivationSnapshot>,
}

impl HoroscopePalaceProjectionSnapshot {
    fn from_projection(projection: &HoroscopePalaceProjection) -> Self {
        Self {
            scope: projection.scope(),
            requested_palace_name: projection.requested_palace_name(),
            branch: projection.branch(),
            natal_palace_name: projection.natal_palace_name(),
            temporal_palace_name: projection.temporal_palace_name(),
            natal_palace_stem: projection.natal_palace_stem(),
            natal_typed_stars: projection.natal_typed_stars().to_vec(),
            natal_decorative_stars: projection.natal_decorative_stars().to_vec(),
            temporal_stars: projection.temporal_stars().to_vec(),
            temporal_decorative_stars: projection.temporal_decorative_stars().to_vec(),
            temporal_mutagen_activations: projection
                .temporal_mutagen_activations()
                .iter()
                .map(HoroscopeProjectionMutagenActivationSnapshot::from_activation)
                .collect(),
        }
    }

    /// Returns the scope used to select the projection branch.
    pub const fn scope(&self) -> Scope {
        self.scope
    }

    /// Returns the palace name requested by the facade.
    pub const fn requested_palace_name(&self) -> PalaceName {
        self.requested_palace_name
    }

    /// Returns the stable branch identity of this projection.
    pub const fn branch(&self) -> EarthlyBranch {
        self.branch
    }

    /// Returns the natal palace name at this branch.
    pub const fn natal_palace_name(&self) -> PalaceName {
        self.natal_palace_name
    }

    /// Returns the temporal palace name assigned to this branch, if any.
    pub const fn temporal_palace_name(&self) -> Option<PalaceName> {
        self.temporal_palace_name
    }

    /// Returns the natal palace stem at this branch.
    pub const fn natal_palace_stem(&self) -> HeavenlyStem {
        self.natal_palace_stem
    }

    /// Returns natal typed star names at this branch.
    pub fn natal_typed_stars(&self) -> &[StarName] {
        &self.natal_typed_stars
    }

    /// Returns natal decorative star names at this branch.
    pub fn natal_decorative_stars(&self) -> &[StarName] {
        &self.natal_decorative_stars
    }

    /// Returns temporal scoped star names at this branch.
    pub fn temporal_stars(&self) -> &[StarName] {
        &self.temporal_stars
    }

    /// Returns temporal decorative star names at this branch.
    pub fn temporal_decorative_stars(&self) -> &[StarName] {
        &self.temporal_decorative_stars
    }

    /// Returns temporal mutagen activations at this branch.
    pub fn temporal_mutagen_activations(&self) -> &[HoroscopeProjectionMutagenActivationSnapshot] {
        &self.temporal_mutagen_activations
    }
}

/// Serializable form of a [`HoroscopeProjectionMutagenActivation`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct HoroscopeProjectionMutagenActivationSnapshot {
    target_star: StarName,
    mutagen: Mutagen,
}

impl HoroscopeProjectionMutagenActivationSnapshot {
    fn from_activation(activation: &HoroscopeProjectionMutagenActivation) -> Self {
        Self {
            target_star: activation.target_star(),
            mutagen: activation.mutagen(),
        }
    }

    /// Returns the activated natal star.
    pub const fn target_star(&self) -> StarName {
        self.target_star
    }

    /// Returns the transform applied to the star.
    pub const fn mutagen(&self) -> Mutagen {
        self.mutagen
    }
}

/// Serializable form of a [`HoroscopeSurroundPalaces`] 三方四正 projection.
///
/// The `scope` and `requested_palace_name` are lifted from the target projection
/// so the surround block mirrors the upstream facade shape.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct HoroscopeSurroundPalacesSnapshot {
    scope: Scope,
    requested_palace_name: PalaceName,
    target: HoroscopePalaceProjectionSnapshot,
    opposite: HoroscopePalaceProjectionSnapshot,
    wealth: HoroscopePalaceProjectionSnapshot,
    career: HoroscopePalaceProjectionSnapshot,
}

impl HoroscopeSurroundPalacesSnapshot {
    fn from_surround(surround: &HoroscopeSurroundPalaces) -> Self {
        let target = HoroscopePalaceProjectionSnapshot::from_projection(surround.target());
        Self {
            scope: target.scope(),
            requested_palace_name: target.requested_palace_name(),
            target,
            opposite: HoroscopePalaceProjectionSnapshot::from_projection(surround.opposite()),
            wealth: HoroscopePalaceProjectionSnapshot::from_projection(surround.wealth()),
            career: HoroscopePalaceProjectionSnapshot::from_projection(surround.career()),
        }
    }

    /// Returns the scope used to select the projection branches.
    pub const fn scope(&self) -> Scope {
        self.scope
    }

    /// Returns the palace name requested by the facade.
    pub const fn requested_palace_name(&self) -> PalaceName {
        self.requested_palace_name
    }

    /// Returns the target palace projection.
    pub const fn target(&self) -> &HoroscopePalaceProjectionSnapshot {
        &self.target
    }

    /// Returns the opposite palace projection.
    pub const fn opposite(&self) -> &HoroscopePalaceProjectionSnapshot {
        &self.opposite
    }

    /// Returns the 财帛位 projection in the upstream 三方四正 convention.
    pub const fn wealth(&self) -> &HoroscopePalaceProjectionSnapshot {
        &self.wealth
    }

    /// Returns the 官禄位 projection in the upstream 三方四正 convention.
    pub const fn career(&self) -> &HoroscopePalaceProjectionSnapshot {
        &self.career
    }
}

fn single_layer_context(
    chart: &HoroscopeChart,
    scope: Scope,
) -> Result<&TemporalContext, ChartError> {
    let mut layers = chart.layers_in_scope(scope);
    let layer = layers
        .next()
        .ok_or(ChartError::MissingHoroscopeLayer { scope })?;
    if layers.next().is_some() {
        return Err(ChartError::DuplicateHoroscopeLayer { scope });
    }
    Ok(layer.context())
}

fn lunar_year(chart: &HoroscopeChart) -> Result<i32, ChartError> {
    match single_layer_context(chart, Scope::Yearly)? {
        TemporalContext::Yearly { lunar_year, .. } => Ok(*lunar_year),
        context => Err(ChartError::TemporalScopeMismatch {
            layer: Scope::Yearly,
            context: context.scope(),
        }),
    }
}

fn lunar_month(chart: &HoroscopeChart) -> Result<u8, ChartError> {
    match single_layer_context(chart, Scope::Monthly)? {
        TemporalContext::Monthly { lunar_month, .. } => Ok(*lunar_month),
        context => Err(ChartError::TemporalScopeMismatch {
            layer: Scope::Monthly,
            context: context.scope(),
        }),
    }
}

fn lunar_day(chart: &HoroscopeChart) -> Result<u8, ChartError> {
    match single_layer_context(chart, Scope::Daily)? {
        TemporalContext::Daily { lunar_day, .. } => Ok(*lunar_day),
        context => Err(ChartError::TemporalScopeMismatch {
            layer: Scope::Daily,
            context: context.scope(),
        }),
    }
}