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
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
"""
Shared utilities for FractalStat validation experiments.


Successor to fractalstat with improved expressivity (100% vs 95%).
Added 8th dimension: 'alignment' for social/coordination dynamics.

Features:
- Hybrid encoding (maps legacy systems to FractalStat coordinates)
- Backward compatibility with existing pets/badges/entities
- LUCA-adjacent bootstrap tracing
- Deterministic coordinate assignment
- Entanglement detection and management
"""

from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import Enum
from typing import Dict, List, Optional, Any, Tuple
from pathlib import Path
import json
import uuid
import hashlib
from abc import ABC, abstractmethod
import secrets
import random
from decimal import Decimal, ROUND_HALF_EVEN



def _utc_now() -> datetime:
    """Helper function for timezone-aware UTC datetime."""
    return datetime.now(timezone.utc)


# ============================================================================
# FractalStat Dimension Enums
# ============================================================================


class Realm(Enum):
    """Domain classification for FractalStat entities"""

    COMPANION = "companion"  # Pets, familiars, companions
    BADGE = "badge"  # Achievement badges
    SPONSOR_RING = "sponsor_ring"  # Sponsor tier badges
    ACHIEVEMENT = "achievement"  # Generic achievements
    PATTERN = "pattern"  # System patterns
    FACULTY = "faculty"  # Faculty-exclusive entities
    TEMPORAL = "temporal"  # Time-based entities
    VOID = "void"  # Null/empty realm


class Horizon(Enum):
    """Lifecycle stage in entity progression"""

    GENESIS = "genesis"  # Entity created, initial state
    EMERGENCE = "emergence"  # Entity becoming active
    PEAK = "peak"  # Entity at maximum activity
    DECAY = "decay"  # Entity waning
    CRYSTALLIZATION = "crystallization"  # Entity settled/permanent
    ARCHIVED = "archived"  # Historical record


class Polarity(Enum):
    """Resonance/affinity classification"""

    # Companion polarities (elemental)
    LOGIC = "logic"
    CREATIVITY = "creativity"
    ORDER = "order"
    CHAOS = "chaos"
    BALANCE = "balance"

    # Badge polarities (category)
    ACHIEVEMENT = "achievement"
    CONTRIBUTION = "contribution"
    COMMUNITY = "community"
    TECHNICAL = "technical"
    CREATIVE = "creative"
    UNITY = "unity"  # Special for sponsor rings

    # Neutral
    VOID = "void"


class Alignment(Enum):
    """Social and coordination dynamics alignment"""

    # Classical alignment system (inspired by fantasy RPGs) - Law vs Chaos
    LAWFUL_GOOD = "lawful_good"  # Principled, helpful
    NEUTRAL_GOOD = "neutral_good"  # Helpful, flexible
    CHAOTIC_GOOD = "chaotic_good"  # Helpful, unconstrained

    LAWFUL_NEUTRAL = "lawful_neutral"  # Principled, pragmatic
    TRUE_NEUTRAL = "true_neutral"  # Balanced, pragmatic
    CHAOTIC_NEUTRAL = "chaotic_neutral"  # Flexible, pragmatic

    LAWFUL_EVIL = "lawful_evil"  # Principled, harmful
    NEUTRAL_EVIL = "neutral_evil"  # Self-serving
    CHAOTIC_EVIL = "chaotic_evil"  # Harmful, unconstrained

    # Special classifications for FractalStat
    HARMONIC = "harmonic"  # Naturally coordinated
    ENTROPIC = "entropic"  # Naturally disruptive
    SYMBIOTIC = "symbiotic"  # Mutually beneficial connections


# ============================================================================
# FractalStat Coordinate Data Class (8 Dimensions)
# ============================================================================


@dataclass
class FractalStatCoordinates:
    """
    8-dimensional addressing space for all entities with 100% expressivity.

    Each dimension represents a different axis of entity existence:
      1. Realm: Domain/type classification
      2. Lineage: Generation or tier progression from LUCA
      3. Adjacency: Semantic/functional proximity score (0-100)
      4. Horizon: Lifecycle stage
      5. Luminosity: Activity level (0-100)
      6. Polarity: Resonance/affinity type
      7. Dimensionality: Fractal depth / detail level
      8. Alignment: Social/coordination dynamics (NEW - 100% expressivity boost)
    """

    realm: Realm  # Domain classification
    lineage: int  # 0-based generation from LUCA
    adjacency: float  # 0-100 proximity score
    horizon: Horizon  # lifecycle stage
    luminosity: float  # 0-100 activity level
    polarity: Polarity  # resonance/affinity type
    dimensionality: int  # 0+ fractal depth
    alignment: Alignment  # 8th dimension for social dynamics

    @property
    def address(self) -> str:
        """Generate canonical FractalStat address string"""
        return f"FractalStat-{self.realm.value[0].upper()}-{self.lineage:03d}-{int(self.adjacency):02d}-{self.horizon.value[0].upper()}-{int(self.luminosity):02d}-{self.polarity.value[0].upper()}-{self.dimensionality}-{self.alignment.value[0].upper()}"

    @staticmethod
    def from_address(address: str) -> "FractalStatCoordinates":
        """Parse FractalStat address back to coordinates"""
        # Format: FractalStat-R-LLL-AA-H-LL-P-D-A (9 parts total)
        parts = address.split("-")
        if len(parts) != 9 or parts[0] != "FractalStat":
            raise ValueError(f"Invalid FractalStat address: {address}")

        realm_map = {r.value[0].upper(): r for r in Realm}
        horizon_map = {h.value[0].upper(): h for h in Horizon}
        polarity_map = {p.value[0].upper(): p for p in Polarity}
        alignment_map = {a.value[0].upper(): a for a in Alignment}

        try:
            return FractalStatCoordinates(
                realm=realm_map[parts[1]],
                lineage=int(parts[2]),
                adjacency=float(parts[3]),
                horizon=horizon_map[parts[4]],
                luminosity=float(parts[5]),
                polarity=polarity_map[parts[6]],
                dimensionality=int(parts[7]),
                alignment=alignment_map[parts[8]],
            )
        except (KeyError, ValueError) as e:
            raise ValueError(f"Invalid FractalStat address: {address}") from e

    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary for JSON serialization"""
        return {
            "realm": self.realm.value,
            "lineage": self.lineage,
            "adjacency": self.adjacency,
            "horizon": self.horizon.value,
            "luminosity": self.luminosity,
            "polarity": self.polarity.value,
            "dimensionality": self.dimensionality,
            "alignment": self.alignment.value,
            "address": self.address,
        }


# ============================================================================
# Lifecycle Event Tracking
# ============================================================================


@dataclass
class LifecycleEvent:
    """Record of significant moments in entity history"""

    timestamp: datetime
    event_type: str  # "birth", "evolution", "mint", etc.
    description: str
    metadata: Dict[str, Any] = field(default_factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        return {
            "timestamp": self.timestamp.isoformat(),
            "event_type": self.event_type,
            "description": self.description,
            "metadata": self.metadata,
        }


# ============================================================================
# FractalStat Entity Base Class
# ============================================================================


@dataclass
class FractalStatEntity(ABC):
    """
    Abstract base class for all FractalStat-addressed entities.

    8-dimensional successor to fractalstat with enhanced expressivity.

    Provides:
    - Hybrid encoding (bridge between legacy and FractalStat systems)
    - 8D coordinate assignment
    - Entanglement tracking
    - Temporal tracking
    - NFT metadata
    """

    # Identity
    entity_id: str = field(default_factory=lambda: str(uuid.uuid4()))
    entity_type: str = ""  # Overridden in subclasses

    # FractalStat Addressing (8D)
    fractalstat: Optional[FractalStatCoordinates] = None

    # Legacy Fields (backward compatibility)
    legacy_data: Dict[str, Any] = field(default_factory=dict)
    migration_source: Optional[str] = None  # "pet", "badge", etc.

    # NFT Status
    nft_minted: bool = False
    nft_contract: Optional[str] = None
    nft_token_id: Optional[int] = None
    nft_metadata_ipfs: Optional[str] = None

    # Entanglement
    entangled_entities: List[str] = field(default_factory=list)
    entanglement_strength: List[float] = field(default_factory=list)

    # Temporal
    created_at: datetime = field(default_factory=_utc_now)
    last_activity: datetime = field(default_factory=_utc_now)
    lifecycle_events: List[LifecycleEvent] = field(default_factory=list)

    # Owner/User
    owner_id: str = ""

    # User Preferences
    opt_in_fractalstat_nft: bool = True  # Renamed from opt_in_fractalstat_nft
    opt_in_blockchain: bool = False
    preferred_zoom_level: int = 1  # Default display level

    def __post_init__(self):
        """Initialize FractalStat coordinates if not provided"""
        if self.fractalstat is None:
            self.fractalstat = self._compute_fractalstat_coordinates()
        self._record_event("genesis", "Entity initialized in FractalStat space")

    # ========================================================================
    # Abstract Methods (Implemented by Subclasses)
    # ========================================================================

    @abstractmethod
    def _compute_fractalstat_coordinates(self) -> FractalStatCoordinates:
        """
        Compute 8D FractalStat coordinates from entity data.
        Each subclass defines its own coordinate mapping.
        """

    @abstractmethod
    def to_collectible_card_data(self) -> Dict[str, Any]:
        """Convert entity to collectible card display format"""

    @abstractmethod
    def validate_hybrid_encoding(self) -> Tuple[bool, str]:
        """
        Validate that FractalStat coordinates correctly encode legacy data.
        Returns (is_valid, error_message_or_empty_string)
        """

    # ========================================================================
    # Event Tracking
    # ========================================================================

    def _record_event(
        self,
        event_type: str,
        description: str,
        metadata: Optional[Dict[str, Any]] = None,
    ):
        """Record a lifecycle event"""
        event = LifecycleEvent(
            timestamp=datetime.now(timezone.utc),
            event_type=event_type,
            description=description,
            metadata=metadata or {},
        )
        self.lifecycle_events.append(event)
        self.last_activity = event.timestamp

    def get_event_history(self, limit: Optional[int] = None) -> List[LifecycleEvent]:
        """Get lifecycle events, optionally limited to most recent"""
        events = sorted(self.lifecycle_events, key=lambda e: e.timestamp, reverse=True)
        return events[:limit] if limit else events

    # ========================================================================
    # Entanglement Management
    # ========================================================================

    def add_entanglement(self, other_entity_id: str, strength: float = 1.0):
        """
        Link to another entity via resonance/entanglement.
        Strength: 0-1.0 (1.0 = maximum entanglement)
        """
        if other_entity_id not in self.entangled_entities:
            self.entangled_entities.append(other_entity_id)
            self.entanglement_strength.append(strength)
            self._record_event(
                "entanglement_added",
                f"Entangled with {other_entity_id}",
                {"strength": strength},
            )

    def remove_entanglement(self, other_entity_id: str):
        """Remove entanglement with another entity"""
        if other_entity_id in self.entangled_entities:
            idx = self.entangled_entities.index(other_entity_id)
            self.entangled_entities.pop(idx)
            self.entanglement_strength.pop(idx)
            self._record_event(
                "entanglement_removed", f"Untangled from {other_entity_id}"
            )

    def get_entanglements(self) -> List[Tuple[str, float]]:
        """Get all entangled entities with strength"""
        return list(zip(self.entangled_entities, self.entanglement_strength))

    def update_entanglement_strength(self, other_entity_id: str, new_strength: float):
        """Update entanglement strength with another entity"""
        if other_entity_id in self.entangled_entities:
            idx = self.entangled_entities.index(other_entity_id)
            old_strength = self.entanglement_strength[idx]
            self.entanglement_strength[idx] = new_strength
            self._record_event(
                "entanglement_updated",
                f"Entanglement strength changed {old_strength:.2f} -> {new_strength:.2f}",
            )

    # ========================================================================
    # LUCA Bootstrap
    # ========================================================================

    @property
    def luca_distance(self) -> int:
        """Distance from LUCA (Last Universal Common Ancestor)"""
        if self.fractalstat is None:
            raise ValueError("fractalstat coordinates must be initialized")
        return self.fractalstat.lineage

    def get_luca_trace(self) -> Dict[str, Any]:
        """
        Get path back to LUCA bootstrap origin.
        In a real system, this would trace parent entities.
        """
        if self.fractalstat is None:
            raise ValueError("fractalstat coordinates must be initialized")
        return {
            "entity_id": self.entity_id,
            "luca_distance": self.luca_distance,
            "realm": self.fractalstat.realm.value,
            "lineage": self.fractalstat.lineage,
            "created_at": self.created_at.isoformat(),
            "migration_source": self.migration_source,
            "event_count": len(self.lifecycle_events),
        }

    # ========================================================================
    # NFT Integration
    # ========================================================================

    def prepare_for_minting(self) -> Dict[str, Any]:
        """
        Generate NFT metadata for minting.
        Returns ERC-721/ERC-1155 compatible metadata object.
        """
        if not self.opt_in_fractalstat_nft:
            raise ValueError("Entity not opted in to FractalStat-NFT system")

        if self.fractalstat is None:
            raise ValueError("fractalstat coordinates must be initialized")
        card_data = self.to_collectible_card_data()

        return {
            "name": card_data.get("title", self.entity_id),
            "description": card_data.get("fluff_text", ""),
            "image": card_data.get("artwork_url", ""),
            "external_url": f"https://theseed.example.com/entity/{self.entity_id}",
            "attributes": [
                {"trait_type": "Entity Type", "value": self.entity_type},
                {"trait_type": "Realm", "value": self.fractalstat.realm.value},
                {"trait_type": "Lineage", "value": self.fractalstat.lineage},
                {"trait_type": "Horizon", "value": self.fractalstat.horizon.value},
                {
                    "trait_type": "Luminosity",
                    "value": int(self.fractalstat.luminosity),
                },
                {"trait_type": "Polarity", "value": self.fractalstat.polarity.value},
                {
                    "trait_type": "Dimensionality",
                    "value": self.fractalstat.dimensionality,
                },
                {"trait_type": "Alignment", "value": self.fractalstat.alignment.value},
                {
                    "trait_type": "FractalStat Address",
                    "value": self.fractalstat.address,
                },
            ],
            "properties": card_data.get("properties", {}),
        }

    def record_mint(self, contract_address: str, token_id: int, ipfs_hash: str):
        """Record successful NFT minting"""
        self.nft_minted = True
        self.nft_contract = contract_address
        self.nft_token_id = token_id
        self.nft_metadata_ipfs = ipfs_hash
        self._record_event(
            "nft_minted",
            f"Minted as ERC-721 token #{token_id}",
            {
                "contract": contract_address,
                "token_id": token_id,
                "ipfs_hash": ipfs_hash,
            },
        )

    # ========================================================================
    # Serialization
    # ========================================================================

    def to_dict(self) -> Dict[str, Any]:
        """Convert entity to dictionary for JSON storage"""
        return {
            "entity_id": self.entity_id,
            "entity_type": self.entity_type,
            "fractalstat": self.fractalstat.to_dict() if self.fractalstat else None,
            "legacy_data": self.legacy_data,
            "migration_source": self.migration_source,
            "nft_minted": self.nft_minted,
            "nft_contract": self.nft_contract,
            "nft_token_id": self.nft_token_id,
            "nft_metadata_ipfs": self.nft_metadata_ipfs,
            "entangled_entities": self.entangled_entities,
            "entanglement_strength": self.entanglement_strength,
            "created_at": self.created_at.isoformat(),
            "last_activity": self.last_activity.isoformat(),
            "lifecycle_events": [e.to_dict() for e in self.lifecycle_events],
            "owner_id": self.owner_id,
            "opt_in_fractalstat_nft": self.opt_in_fractalstat_nft,
            "opt_in_blockchain": self.opt_in_blockchain,
            "preferred_zoom_level": self.preferred_zoom_level,
        }

    def save_to_file(self, path: Path):
        """Persist entity to JSON file"""
        path.parent.mkdir(parents=True, exist_ok=True)
        with open(path, "w", encoding="utf-8") as f:
            json.dump(self.to_dict(), f, indent=2, default=str)

    @classmethod
    def load_from_file(cls, path: Path) -> "FractalStatEntity":
        """Load entity from JSON file (must know concrete type)"""
        with open(path, "r", encoding="utf-8") as f:
            data = json.load(f)
        entity_type = data.get("entity_type", "unknown")
        raise NotImplementedError(
            f"Use subclass load methods (detected entity_type: {entity_type}). "
            "Use factory pattern to instantiate correct subclass."
        )

    # ========================================================================
    # Display Levels
    # ========================================================================

    def render_zoom_level(self, level: int) -> Dict[str, Any]:
        """
        Render entity at specific zoom level.

        Level 1: Badge (20x20px icon)
        Level 2: Dog-tag (100x150px micro-card)
        Level 3: Collectible Card (300x400px full card)
        Level 4: Profile panel (350x500px interactive)
        Level 5: Entity profile page (full details)
        Level 6+: Fractal descent (dimension breakdown)
        """
        if level < 1 or level > 8:  # Increased max zoom level for 8D
            raise ValueError(f"Invalid zoom level: {level}")

        if self.fractalstat is None:
            raise ValueError("FractalStat coordinates must be initialized")
        card_data = self.to_collectible_card_data()

        base = {
            "zoom_level": level,
            "entity_id": self.entity_id,
            "fractalstat_address": self.fractalstat.address,
            "created_at": self.created_at.isoformat(),
        }

        if level == 1:
            # Badge: Just icon + rarity
            return {
                **base,
                "type": "badge",
                "icon": card_data.get("icon_url"),
                "rarity": card_data.get("rarity"),
            }

        elif level == 2:
            # Dog-tag: Icon, title, key stats
            return {
                **base,
                "type": "dog_tag",
                "icon": card_data.get("icon_url"),
                "title": card_data.get("title"),
                "stats": card_data.get("key_stats"),
            }

        elif level == 3:
            # Full card
            return {**base, "type": "collectible_card", **card_data}

        elif level == 4:
            # Profile panel
            return {
                **base,
                "type": "profile_panel",
                **card_data,
                "owner": self.owner_id,
                "entangled_count": len(self.entangled_entities),
                "events": len(self.lifecycle_events),
            }

        elif level == 5:
            # Full profile page
            return {
                **base,
                "type": "entity_profile",
                **card_data,
                "owner": self.owner_id,
                "lifecycle_events": [e.to_dict() for e in self.lifecycle_events],
                "entanglements": self.get_entanglements(),
                "luca_trace": self.get_luca_trace(),
            }

        elif level == 6:
            # 8th dimension awareness
            return {
                **base,
                "type": "fractal_descent",
                "fractalstat_dimensions": self.fractalstat.to_dict(),
                "alignment_dynamics": self._get_alignment_details(),
                "realm_details": self._get_realm_details(),
                "entanglement_network": self.get_entanglements(),
                "event_chronology": [e.to_dict() for e in self.lifecycle_events],
                "luca_trace": self.get_luca_trace(),
            }

        else:  # level 7+
            # Full fractal descent with 8D awareness
            return {
                **base,
                "type": "fractal_descent",
                "fractalstat_dimensions": self.fractalstat.to_dict(),
                "alignment_dynamics": self._get_alignment_details(),
                "realm_details": self._get_realm_details(),
                "entanglement_network": self.get_entanglements(),
                "event_chronology": [e.to_dict() for e in self.lifecycle_events],
                "luca_trace": self.get_luca_trace(),
            }

    def _get_realm_details(self) -> Dict[str, Any]:
        """Override in subclasses to provide realm-specific details"""
        return {}

    def _get_alignment_details(self) -> Dict[str, Any]:
        """Get alignment-based social/coordination analysis"""
        if self.fractalstat is None:
            return {}

        alignment = self.fractalstat.alignment
        # Analyze social coordination patterns based on alignment
        coordination_style = {
            Alignment.LAWFUL_GOOD: "structured_harmonious",
            Alignment.NEUTRAL_GOOD: "balanced_harmonious",
            Alignment.CHAOTIC_GOOD: "flexible_harmonious",
            Alignment.LAWFUL_NEUTRAL: "structured_pragmatic",
            Alignment.TRUE_NEUTRAL: "balanced_pragmatic",
            Alignment.CHAOTIC_NEUTRAL: "flexible_pragmatic",
            Alignment.LAWFUL_EVIL: "structured_destructive",
            Alignment.NEUTRAL_EVIL: "balanced_destructive",
            Alignment.CHAOTIC_EVIL: "flexible_destructive",
            Alignment.HARMONIC: "naturally_coordinating",
            Alignment.ENTROPIC: "naturally_disruptive",
            Alignment.SYMBIOTIC: "mutually_beneficial",
        }.get(alignment, "unknown")

        return {
            "alignment": alignment.value,
            "coordination_style": coordination_style,
            "social_dynamics": self._analyze_social_dynamics(),
        }

    def _analyze_social_dynamics(self) -> Dict[str, Any]:
        """Analyze social interaction patterns based on entanglement and alignment"""
        if self.fractalstat is None:
            return {}

        # Simplified social analysis based on alignment
        alignment = self.fractalstat.alignment

        if alignment in [Alignment.LAWFUL_GOOD, Alignment.HARMONIC]:
            social_pattern = "coordinating_harmonious"
        elif alignment in [Alignment.CHAOTIC_EVIL, Alignment.ENTROPIC]:
            social_pattern = "disruptive_chaotic"
        else:
            social_pattern = "pragmatic_balanced"

        return {
            "social_pattern": social_pattern,
            "entanglement_quality": len(self.entangled_entities)
            * 0.1,  # Simplified metric
            "coordination_potential": self._calculate_coordination_potential(),
        }

    def _calculate_coordination_potential(self) -> float:
        """Calculate coordination potential based on alignment and entanglements"""
        # Simplified calculation - would be more sophisticated in production
        base_potential = len(self.entangled_entities) * 0.1

        # Alignment modifiers
        alignment_bonus = {
            Alignment.HARMONIC: 1.5,
            Alignment.SYMBIOTIC: 1.3,
            Alignment.LAWFUL_GOOD: 1.2,
            Alignment.CHAOTIC_EVIL: -0.5,
            Alignment.ENTROPIC: -0.3,
        }.get(
            self.fractalstat.alignment if self.fractalstat else Alignment.TRUE_NEUTRAL,
            1.0,
        )

        return min(1.0, base_potential * alignment_bonus)


# ============================================================================
# Helper Functions
# ============================================================================


def hash_for_coordinates(data: Dict[str, Any]) -> str:
    """Deterministic hashing for coordinate assignment"""
    json_str = json.dumps(data, sort_keys=True)
    return hashlib.sha256(json_str.encode()).hexdigest()


def compute_adjacency_score(tags1: List[str], tags2: List[str]) -> float:
    """
    Compute adjacency (similarity) score between two tag sets.
    Returns 0-100 score.
    """
    if not tags1 or not tags2:
        return 0.0

    common = len(set(tags1) & set(tags2))
    total = len(set(tags1) | set(tags2))
    return (common / total) * 100 if total > 0 else 0.0


# ============================================================================
# BitChain Entity (moved from fractalstat_experiments to break circular import)
# ============================================================================

class DataClass(Enum):
    """Data sensitivity classification."""

    PUBLIC = "PUBLIC"  # Anyone can read
    SENSITIVE = "SENSITIVE"  # Authenticated users, role-based
    PII = "PII"  # Owner-only, requires 2FA


class Capability(Enum):
    """Recovery capability levels."""

    COMPRESSED = "compressed"  # Read-only mist form, no expansion
    PARTIAL = "partial"  # Anonymized expansion, limited fields
    FULL = "full"  # Complete recovery


# Coordinate data class for BitChain (different from FractalStatCoordinates)
@dataclass
class Coordinates:
    """FractalStat 8-dimensional coordinates with enhanced expressivity."""

    realm: str  # Domain: data, narrative, system, faculty, event, pattern, void, temporal
    lineage: int  # Generation from LUCA
    adjacency: List[str]  # Relational neighbors (append-only)
    horizon: str  # Lifecycle stage
    luminosity: float  # 0-100 activity level
    polarity: Polarity  # Resonance/affinity type
    dimensionality: int  # 0+ fractal depth
    alignment: Alignment  # Social alignment dynamics - NEW DIMENSION

    def to_dict(self) -> Dict[str, Any]:
        """Convert to canonical dict with normalized floats."""
        return {
            # Append-only, but stored sorted
            "realm": self.realm,
            "lineage": self.lineage,
            "adjacency": sorted(self.adjacency),
            "horizon": self.horizon,
            "luminosity": normalize_float(self.luminosity),
            "polarity": self.polarity.name,
            "dimensionality": self.dimensionality,
            "alignment": self.alignment.name,
        }


@dataclass
class BitChain:
    """
    Minimal addressable unit in FractalStat space.
    Represents a single entity instance (manifestation).

    Security fields (Phase 1 Doctrine: Authentication + Access Control):
    - data_classification: Sensitivity level (PUBLIC, SENSITIVE, PII)
    - access_control_list: Roles allowed to recover this bitchain
    - owner_id: User who owns this bitchain
    - encryption_key_id: Optional key for encrypted-at-rest data
    """

    id: str  # Unique entity ID
    entity_type: str  # Type: concept, artifact, agent, etc.
    realm: str  # Domain classification
    coordinates: Coordinates  # FractalStat 8D position
    created_at: str  # ISO8601 UTC timestamp
    state: Dict[str, Any]  # Mutable state data

    # Security fields (Phase 1)
    data_classification: DataClass = DataClass.PUBLIC
    access_control_list: List[str] = field(default_factory=lambda: ["owner"])
    owner_id: Optional[str] = None
    encryption_key_id: Optional[str] = None

    def __post_init__(self):
        """Normalize timestamps."""
        self.created_at = normalize_timestamp(self.created_at)

    def to_canonical_dict(self) -> Dict[str, Any]:
        """Convert to canonical form for hashing."""
        return {
            "created_at": self.created_at,
            "entity_type": self.entity_type,
            "id": self.id,
            "realm": self.realm,
            "fractalstat_coordinates": self.coordinates.to_dict(),
            "state": sort_json_keys(self.state),
        }

    def compute_address(self) -> str:
        """Compute this bit-chain's FractalStat address (hash)."""
        return compute_address_hash(self.to_canonical_dict())

    def get_fractalstat_uri(self) -> str:
        """Generate FractalStat URI address format."""
        coords = self.coordinates
        adjacency_hash = compute_address_hash({"adjacency": sorted(coords.adjacency)})[
            :8
        ]

        uri = (
            f"fractalstat://{coords.realm}/{coords.lineage}/{adjacency_hash}/{coords.horizon}"
        )
        uri += f"?r={normalize_float(coords.luminosity)}&p={coords.polarity.name}"
        uri += f"&d={coords.dimensionality}&s={self.id}&a={coords.alignment.name}"

        return uri


# ============================================================================
# Constants and utilities for BitChain (moved from fractalstat_experiments)
# ============================================================================

# Use cryptographically secure random number generator
secure_random = secrets.SystemRandom()

REALMS = ["data", "narrative", "system", "faculty", "event", "pattern", "void"]
HORIZONS = ["genesis", "emergence", "peak", "decay", "crystallization"]
POLARITY_LIST = ["logic", "creativity", "order", "chaos", "balance", "achievement",
            "contribution", "community", "technical", "creative", "unity", "void"]
ALIGNMENT_LIST = ["lawful_good", "neutral_good", "chaotic_good", "lawful_neutral",
            "true_neutral", "chaotic_neutral", "lawful_evil", "neutral_evil"]
ENTITY_TYPES = [
    "concept",
    "artifact",
    "agent",
    "lineage",
    "adjacency",
    "horizon",
    "fragment",
]


def normalize_float(value: float, decimal_places: int = 8) -> str:
    """
    Normalize floating point to 8 decimal places using banker's rounding.
    """
    if isinstance(value, float):
        if value != value or value == float("inf") or value == float("-inf"):
            raise ValueError(f"NaN and Inf not allowed: {value}")

    # Use Decimal for precise rounding
    d = Decimal(str(value))
    quantized = d.quantize(Decimal(10) ** -decimal_places, rounding=ROUND_HALF_EVEN)

    # Convert to string and strip trailing zeros
    result = str(quantized)
    if "." in result:
        result = result.rstrip("0")
        if result.endswith("."):
            result += "0"
    elif "E" in result or "e" in result:
        # Handle scientific notation
        result = "0.0"

    return result


def normalize_timestamp(ts: Optional[str] = None) -> str:
    """
    Normalize timestamp to ISO8601 UTC with millisecond precision.
    """
    if ts is None:
        now = datetime.now(timezone.utc)
        return now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
    else:
        # Parse input timestamp and convert to UTC
        if ts.endswith("Z"):
            ts = ts[:-1] + "+00:00"
        now = datetime.fromisoformat(ts).astimezone(timezone.utc)

    # Format with millisecond precision
    return now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"


def sort_json_keys(obj: Any) -> Any:
    """
    Recursively sort all JSON object keys in ASCII order (case-sensitive).
    """
    if isinstance(obj, dict):
        return {k: sort_json_keys(obj[k]) for k in sorted(obj.keys())}
    elif isinstance(obj, list):
        return [sort_json_keys(item) for item in obj]
    else:
        return obj


def canonical_serialize(data: Dict[str, Any]) -> str:
    """
    Serialize to canonical form for deterministic hashing.
    """
    sorted_data = sort_json_keys(data)
    canonical = json.dumps(
        sorted_data, separators=(",", ":"), ensure_ascii=True, sort_keys=False
    )
    return canonical


def compute_address_hash(data: Dict[str, Any]) -> str:
    """
    Compute SHA-256 hash of canonical serialization.
    """
    canonical = canonical_serialize(data)
    return hashlib.sha256(canonical.encode("utf-8")).hexdigest()


def generate_random_bitchain(seed: Optional[int] = None) -> BitChain:
    """
    Generate a random bit-chain for testing and validation experiments.
    """
    
    # Use seedable random when seed is provided, otherwise use secure random
    if seed is not None:
        rng = random.Random(seed)
        base_id = hashlib.sha256(str(seed).encode()).hexdigest()[:32]
        id_str = f"{base_id[:8]}-{base_id[8:12]}-{base_id[12:16]}-{base_id[16:20]}"
        id_str += f"-{base_id[20:32]}"
        created_at_str = f"2024-01-01T{seed % 24:02d}:{(seed // 24) % 60:02d}"
        created_at_str += f":{(seed // 1440) % 60:02d}.000Z"
    else:
        rng = secure_random
        id_str = str(uuid.uuid4())
        created_at_str = datetime.now(timezone.utc).isoformat()

    # Replace all instances of `secure_random.` with `rng.` in the rest of the function
    adjacency_ids = [
        (
            hashlib.sha256(f"{seed}-adj-{i}".encode()).hexdigest()[:32]
            if seed is not None
            else str(uuid.uuid4())
        )
        for i in range(rng.randint(0, 5))  # Changed from secure_random.randint
    ]

    if seed is not None and adjacency_ids:
        adjacency_ids = [
            f"{uuid_hex[:8]}-{uuid_hex[8:12]}-{uuid_hex[12:16]}-{uuid_hex[16:20]}"
            f"-{uuid_hex[20:32]}"
            for uuid_hex in adjacency_ids
        ]

    # Generate coordinates with alignment
    luminosity_val = rng.uniform(0, 100)  # Changed from secure_random.uniform
    polarity_val = rng.choice(POLARITY_LIST)  # Changed from secure_random.choice
    dimensionality_val = rng.randint(0, 5)  # Changed from secure_random.randint
    alignment_val = rng.choice(list(Alignment))  # Changed from secure_random.choice

    return BitChain(
        id=id_str,
        entity_type=rng.choice(ENTITY_TYPES),  # Changed from secure_random.choice
        realm=rng.choice(REALMS),  # Changed from secure_random.choice
        coordinates=Coordinates(
            realm=rng.choice(REALMS),  # Changed from secure_random.choice
            lineage=rng.randint(1, 100),  # Changed from secure_random.randint
            adjacency=adjacency_ids,
            horizon=rng.choice(HORIZONS),  # Changed from secure_random.choice
            luminosity=luminosity_val,
            polarity=Polarity[polarity_val.upper()],
            dimensionality=dimensionality_val,
            alignment=alignment_val,
        ),
        created_at=created_at_str,
        state={"value": rng.randint(0, 1000)},  # Changed from secure_random.randint
    )