from typing import Optional
from .state import ChromaticState

class QuantumSystem:
    def __init__(self, id: str):
        self.id = id
        self.chi = ChromaticState(1, 0, 0)
        self.entangled_partner: Optional['QuantumSystem'] = None

    def entangle(self, other_system: 'QuantumSystem'):
        self.entangled_partner = other_system
        other_system.entangled_partner = self

        # Bell State initialization: 1/sqrt(2) approx 0.707
        bell_val = 0.70710678
        self.chi.vector = [bell_val, bell_val, 0.0]
        other_system.chi.vector = [bell_val, bell_val, 0.0]

        self.chi.normalize()
        other_system.chi.normalize()

    def check_correlation(self) -> float:
        if not self.entangled_partner:
            return 0.0

        # Dot product of vectors
        dot = (
            self.chi.vector[0] * self.entangled_partner.chi.vector[0] +
            self.chi.vector[1] * self.entangled_partner.chi.vector[1] +
            self.chi.vector[2] * self.entangled_partner.chi.vector[2]
        )
        return abs(dot)

    def __repr__(self):
        return f"QuantumSystem({self.id}, Chi={self.chi})"
