import math
from ..state import ChromaticState

class FileSealer:
    @staticmethod
    def seal(file_hash: str) -> ChromaticState:
        """
        Seals a file hash into a Chromatic State.
        """
        # 1. Seed Generation
        segment_a = int(file_hash[0:8], 16)
        segment_b = int(file_hash[8:16], 16)

        # 2. Map to Spherical (Theta, Phi)
        max_int = 0xFFFFFFFF
        theta = (segment_a / max_int) * math.pi
        phi = (segment_b / max_int) * (2 * math.pi)

        # 3. Spherical to Cartesian
        alpha = math.sin(theta) * math.cos(phi)
        beta  = math.sin(theta) * math.sin(phi)
        gamma = math.cos(theta)

        # 4. Enforce Coherence Domain
        alpha = abs(alpha) + 0.5

        return ChromaticState(alpha, beta, gamma)

    @staticmethod
    def verify(vector_state: ChromaticState, original_hash: str) -> bool:
        expected_state = FileSealer.seal(original_hash)
        
        dot = (
            vector_state.c0 * expected_state.c0 +
            vector_state.c1 * expected_state.c1 +
            vector_state.c2 * expected_state.c2
        )

        return dot > 0.999
