Ë
    }D™jB  ã            
       ób  — d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZmZmZmZmZ ddlmZmZmZmZmZmZ  G d„ d	«      Zed
k(  rb ej6                  dddddddd«      Z ej6                  dddddddd«      Z eee«      Z eejA                  «       jC                  d¬«      «       yy)u²  
Composite Subject Factory Module

This module provides functionality for creating composite astrological charts from two
individual astrological subjects. A composite chart represents the relationship between
two people by calculating midpoint positions between corresponding planetary placements
and house cusps.

The module implements the midpoint composite technique, which is the most commonly used
method for relationship astrology. This technique creates a single chart that symbolizes
the energy and dynamics of the relationship itself, rather than comparing individual charts.

Key Features:
- Midpoint calculation for all planetary positions
- Midpoint calculation for house cusp positions
- Proper handling of zodiacal boundary crossings (0Â°/360Â°)
- Validation of compatible astrological settings between subjects
- Lunar phase calculation for composite charts
- Support for all standard astrological points and house systems

Classes:
    CompositeSubjectFactory: Main factory class for creating composite charts

Dependencies:
    - AstrologicalSubjectFactory: For working with individual astrological subjects
    - Various schemas modules: For type definitions and models
    - utilities module: For astrological calculations and helper functions

Example Usage:
    >>> from kerykeion import AstrologicalSubjectFactory, CompositeSubjectFactory
    >>> person1 = AstrologicalSubjectFactory.from_birth_data(...)
    >>> person2 = AstrologicalSubjectFactory.from_birth_data(...)
    >>> composite = CompositeSubjectFactory(person1, person2)
    >>> composite_chart = composite.get_midpoint_composite_subject_model()

Author: Giacomo Battaglia
Copyright: (C) 2025 Kerykeion Project
License: AGPL-3.0
é    )ÚUnion)ÚAstrologicalSubjectFactory)ÚKerykeionException)ÚCompositeSubjectModelÚAstrologicalSubjectModel)Ú
ZodiacTypeÚPerspectiveTypeÚHousesSystemIdentifierÚSiderealModeÚAstrologicalPointÚHousesÚCompositeChartType)Úget_kerykeion_point_from_degreeÚget_planet_houseÚcircular_meanÚcalculate_moon_phaseÚcircular_sortÚfind_common_active_pointsc            	       ó  — e Zd ZU dZeedf   ed<   eed<   eed<   eed<   e	ed<   e
ed<   eedf   ed	<   eed
<   eed<   eed<   ee   ed<   ee   ed<   	 ddededeedf   fd„Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚCompositeSubjectFactoryaË	  
    Factory class to create composite astrological charts from two astrological subjects.

    A composite chart represents the relationship between two people by calculating the midpoint
    between corresponding planetary positions and house cusps. This creates a single chart
    that symbolizes the energy of the relationship itself.

    Currently supports the midpoint method for composite chart calculation, where:
    - Planetary positions are calculated as the circular mean of corresponding planets
    - House cusps are calculated as the circular mean of corresponding houses
    - Houses are reordered to maintain consistency with the original house system
    - Only common active points between both subjects are included

    The resulting composite chart maintains the zodiac type, sidereal mode, houses system,
    and perspective type of the input subjects (which must be identical between subjects).

    Attributes:
        model (CompositeSubjectModel | None): The generated composite subject model
        first_subject (AstrologicalSubjectModel): First astrological subject
        second_subject (AstrologicalSubjectModel): Second astrological subject
        name (str): Name of the composite chart
        composite_chart_type (CompositeChartType): Type of composite chart (currently "Midpoint")
        zodiac_type (ZodiacType): Zodiac system used (Tropical or Sidereal)
        sidereal_mode (SiderealMode | None): Sidereal calculation mode if applicable
        houses_system_identifier (HousesSystemIdentifier): House system identifier
        houses_system_name (str): Human-readable house system name
        perspective_type (PerspectiveType): Astrological perspective type
        houses_names_list (list[Houses]): List of house names
        active_points (list[AstrologicalPoint]): Common active planetary points

    Example:
        >>> first_person = AstrologicalSubjectFactory.from_birth_data(
        ...     "John", 1990, 1, 1, 12, 0, "New York", "US"
        ... )
        >>> second_person = AstrologicalSubjectFactory.from_birth_data(
        ...     "Jane", 1992, 6, 15, 14, 30, "Los Angeles", "US"
        ... )
        >>> composite = CompositeSubjectFactory(first_person, second_person)
        >>> composite_model = composite.get_midpoint_composite_subject_model()

    Raises:
        KerykeionException: When subjects have incompatible settings (different zodiac types,
                           sidereal modes, house systems, or perspective types)
    NÚmodelÚfirst_subjectÚsecond_subjectÚnameÚcomposite_chart_typeÚzodiac_typeÚsidereal_modeÚhouses_system_identifierÚhouses_system_nameÚperspective_typeÚhouses_names_listÚactive_pointsÚ
chart_namec                 óÐ  — d| _         d| _        || _        || _        t	        |j
                  |j
                  «      | _        |€"|j                  › d|j                  › d�| _        n|| _        |j                  |j                  k7  rt        d«      ‚|j                  | _        |j                  |j                  k7  rt        d«      ‚|j                  �|j                  | _	        nd| _	        |j                  |j                  k7  rt        d«      ‚|j                  | _
        |j                  |j                  k7  rt        d«      ‚|j                  | _        |j                  |j                  k7  rt        d	«      ‚|j                  | _        g | _        |j
                  D ],  }||j
                  v sŒ| j
                  j                  |«       Œ. | j                  j                  | _        y)
a  
        Initialize the composite subject factory with two astrological subjects.

        Validates that both subjects have compatible settings and extracts common
        active points for composite chart calculation.

        Args:
            first_subject (AstrologicalSubjectModel): First astrological subject for the composite
            second_subject (AstrologicalSubjectModel): Second astrological subject for the composite
            chart_name (str | None, optional): Custom name for the composite chart.
                                             If None, generates name from subject names.
                                             Defaults to None.

        Raises:
            KerykeionException: If subjects have different zodiac types, sidereal modes,
                              house systems, house system names, or perspective types.

        Note:
            Both subjects must have identical astrological calculation settings to ensure
            meaningful composite chart calculations.
        NÚMidpointz and z Composite Chartz,Both subjects must have the same zodiac typez.Both subjects must have the same sidereal modez.Both subjects must have the same houses systemz3Both subjects must have the same houses system namez1Both subjects must have the same perspective type)r   r   r   r   r   r"   r   r   r   r   r   r   r    Úappendr!   )Úselfr   r   r#   Úplanets        úo/var/www/html/Astrologybackend/backend/venv/lib/python3.12/site-packages/kerykeion/composite_subject_factory.pyÚ__init__z CompositeSubjectFactory.__init__}   sÌ  € ð6 :>ˆŒ
Ø$.ˆÔ!à*ˆÔØ,ˆÔÜ6°}×7RÑ7RÐTb×TpÑTpÓqˆÔð ÐØ(×-Ñ-Ð.¨e°N×4GÑ4GÐ3HÐHXÐYˆD�Ià"ˆDŒIð ×$Ñ$¨×(BÑ(BÒBÜ$Ð%SÓTÐTØ(×4Ñ4ˆÔð ×&Ñ&¨.×*FÑ*FÒFÜ$Ð%UÓVÐVà×&Ñ&Ð2Ø!.×!<Ñ!<ˆDÕà!%ˆDÔð ×1Ñ1°^×5\Ñ5\Ò\Ü$Ð%UÓVÐVØ(5×(NÑ(NˆÔ%ð ×+Ñ+¨~×/PÑ/PÒPÜ$Ð%ZÓ[Ð[Ø"/×"BÑ"BˆÔð ×)Ñ)¨^×-LÑ-LÒLÜ$Ð%XÓYÐYØ -× >Ñ >ˆÔð  ˆÔØ#×1Ñ1ò 	2ˆFØ˜×5Ñ5Ò5Ø×"Ñ"×)Ñ)¨&Õ1ð	2ð
 "&×!3Ñ!3×!EÑ!EˆÕó    c                 ó    — d| j                   › �S )z¡
        Return string representation of the composite subject.

        Returns:
            str: Human-readable string describing the composite chart.
        úComposite Chart Data for ©r   ©r'   s    r)   Ú__str__zCompositeSubjectFactory.__str__Ë   ó   € ð +¨4¯9©9¨+Ð6Ð6r+   c                 ó    — d| j                   › �S )z«
        Return detailed string representation of the composite subject.

        Returns:
            str: Detailed string representation for debugging purposes.
        r-   r.   r/   s    r)   Ú__repr__z CompositeSubjectFactory.__repr__Ô   r1   r+   c                 ó    — | j                   |j                   k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S )zÿ
        Check equality with another composite subject.

        Args:
            other (CompositeSubjectFactory): Another composite subject to compare with.

        Returns:
            bool: True if both subjects and chart name are identical.
        )r   r   r   r#   ©r'   Úothers     r)   Ú__eq__zCompositeSubjectFactory.__eq__Ý   sM   € ð ×Ñ %×"5Ñ"5Ñ5ò .Ø×#Ñ# u×';Ñ';Ñ;ò.à—	‘	˜U×-Ñ-Ñ-ð	
r+   c                 ó&   — | j                  |«       S )zû
        Check inequality with another composite subject.

        Args:
            other (CompositeSubjectFactory): Another composite subject to compare with.

        Returns:
            bool: True if subjects or chart name are different.
        )r7   r5   s     r)   Ú__ne__zCompositeSubjectFactory.__ne__í   s   € ð —;‘;˜uÓ%Ð%Ð%r+   c                 óZ   — t        | j                  | j                  | j                  f«      S )z�
        Generate hash for the composite subject.

        Returns:
            int: Hash value based on both subjects and chart name.
        )Úhashr   r   r   r/   s    r)   Ú__hash__z CompositeSubjectFactory.__hash__ù   s&   € ô �T×'Ñ'¨×)<Ñ)<¸d¿i¹iÐHÓIÐIr+   c                 óX   — t        | j                  | j                  | j                  «      S )z¦
        Create a shallow copy of the composite subject.

        Returns:
            CompositeSubjectFactory: New instance with the same subjects and name.
        )r   r   r   r   r/   s    r)   Ú__copy__z CompositeSubjectFactory.__copy__  s&   € ô ' t×'9Ñ'9¸4×;NÑ;NÐPT×PYÑPYÓZÐZr+   c                 ó   — t        | ||«       y)z³
        Set an attribute using dictionary-style access.

        Args:
            key (str): Attribute name to set.
            value: Value to assign to the attribute.
        N)Úsetattr)r'   ÚkeyÚvalues      r)   Ú__setitem__z#CompositeSubjectFactory.__setitem__  s   € ô 	��c˜5Õ!r+   c                 ó   — t        | |«      S )a  
        Get an attribute using dictionary-style access.

        Args:
            key (str): Attribute name to retrieve.

        Returns:
            Any: Value of the requested attribute.

        Raises:
            AttributeError: If the attribute doesn't exist.
        )Úgetattr)r'   rA   s     r)   Ú__getitem__z#CompositeSubjectFactory.__getitem__  s   € ô �t˜SÓ!Ð!r+   c                 ó  — g }| j                   j                  D ]M  }|j                  «       }|j                  t	        | j                   |   d   | j
                  |   d   «      «       ŒO t        |«      }t        | j                   j                  «      D ](  \  }}|j                  «       }t        ||   |d«      | |<   Œ* g }| j                   j                  D ],  }|| j
                  j                  v sŒ|j                  |«       Œ. i }|D ]w  }|j                  «       }	i ||	<   t	        | j                   |	   d   | j
                  |	   d   «      ||	   d<   t        ||	   d   |d«      | |	<   t        | |	   d   |«      | |	   d<   Œy y)u¿  
        Calculate midpoint positions for all planets and house cusps in the composite chart.

        This method implements the midpoint composite technique by:
        1. Computing circular means of house cusp positions from both subjects
        2. Sorting house positions to maintain proper house order
        3. Creating composite house cusps with calculated positions
        4. Computing circular means of planetary positions for common active points
        5. Assigning planets to their appropriate houses in the composite chart

        The circular mean calculation ensures proper handling of zodiacal positions
        around the 360-degree boundary (e.g., when one position is at 350Â° and
        another at 10Â°, the midpoint is correctly calculated as 0Â°).

        Side Effects:
            - Updates instance attributes with calculated house cusp positions
            - Updates instance attributes with calculated planetary positions
            - Sets house assignments for each planetary position

        Note:
            This is an internal method called by get_midpoint_composite_subject_model().
            Only planets that exist in both subjects' active_points are included.
        Úabs_posÚHouser   ÚhouseN)r   r!   Úlowerr&   r   r   r   Ú	enumerater   r"   r   )
r'   Úhouse_degree_list_utrJ   Úhouse_lowerÚhouse_indexÚ
house_nameÚcommon_planetsr(   ÚplanetsÚplanet_lowers
             r)   Ú/_calculate_midpoint_composite_points_and_houseszGCompositeSubjectFactory._calculate_midpoint_composite_points_and_houses$  s¼  € ð2  "ÐØ×'Ñ'×9Ñ9ò 	ˆEØŸ+™+›-ˆKØ ×'Ñ'Ü˜d×0Ñ0°Ñ=¸iÑHÈ$×J]ÑJ]Ð^iÑJjÐktÑJuÓvõð	ô
  -Ð-AÓBÐä'0°×1CÑ1C×1UÑ1UÓ'Vò 	xÑ#ˆK˜Ø$×*Ñ*Ó,ˆKÜ ?Ð@TÐU`Ñ@aÐcmÐovÓ wˆD�Òð	xð
 ˆØ×(Ñ(×6Ñ6ò 	.ˆFØ˜×,Ñ,×:Ñ:Ò:Ø×%Ñ% fÕ-ð	.ð ˆØ$ò 		pˆFØ!Ÿ<™<›>ˆLØ$&ˆG�LÑ!Ü/<Ø×"Ñ" <Ñ0°Ñ;¸T×=PÑ=PÐQ]Ñ=^Ð_hÑ=ió0ˆG�LÑ! )Ñ,ô "AØ˜Ñ% iÑ0°&Ð:Mó"ˆD�Ñô +;¸4ÀÑ;MÈiÑ;XÐZnÓ*oˆD�Ñ˜wÒ'ñ		pr+   c                 óZ   — t        | d   j                  | d   j                  «      | _        y)a)  
        Calculate the lunar phase for the composite chart based on Sun-Moon midpoints.

        Uses the composite positions of the Sun and Moon to determine the lunar phase
        angle, representing the relationship's emotional and instinctual dynamics.

        Side Effects:
            Sets the lunar_phase attribute with the calculated phase information.

        Note:
            This method should be called after _calculate_midpoint_composite_points_and_houses()
            to ensure Sun and Moon composite positions are available.
        ÚmoonÚsunN)r   rH   Úlunar_phaser/   s    r)   Ú _calculate_composite_lunar_phasez8CompositeSubjectFactory._calculate_composite_lunar_phase[  s'   € ô 0°°V±×0DÑ0DÀdÈ5Ák×FYÑFYÓZˆÕr+   c                 ól   — | j                  «        | j                  «        t        di | j                  ¤ŽS )u˜  
        Generate the complete composite chart model using the midpoint technique.

        This is the main public method for creating a composite chart. It orchestrates
        the calculation of all composite positions and creates a complete CompositeSubjectModel
        containing all necessary astrological data for the relationship chart.

        The process includes:
        1. Calculating midpoint positions for all planets and house cusps
        2. Computing the composite lunar phase
        3. Assembling all data into a comprehensive model

        Returns:
            CompositeSubjectModel: Complete composite chart data model containing:
                - All calculated planetary positions and their house placements
                - House cusp positions maintaining proper house system order
                - Lunar phase information for the composite chart
                - All metadata from the original subjects (names, chart type, etc.)

        Example:
            >>> composite = CompositeSubjectFactory(person1, person2, "Our Relationship")
            >>> model = composite.get_midpoint_composite_subject_model()
            >>> print(f"Composite Sun at {model.sun.abs_pos}Â° in House {model.sun.house}")

        Note:
            This method performs all calculations internally and returns a complete,
            ready-to-use composite chart model suitable for analysis or chart drawing.
        © )rT   rY   r   Ú__dict__r/   s    r)   Ú$get_midpoint_composite_subject_modelz<CompositeSubjectFactory.get_midpoint_composite_subject_modelk  s.   € ð: 	×<Ñ<Ô>Ø×-Ñ-Ô/ä$Ñ5 t§}¡}Ñ5Ð5r+   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__r   Ústrr   r   r   r
   r	   Úlistr   r   r*   r0   r3   r7   r9   r<   r>   rC   rF   rT   rY   r]   r[   r+   r)   r   r   B   sä   … ñ+ðZ Ð&¨Ð,Ñ-Ó-Ø+Ó+Ø,Ó,Ø
ƒIØ,Ó,ØÓØ˜ tÐ+Ñ,Ó,Ø4Ó4ØÓØ%Ó%Ø˜F‘|Ó#ØÐ)Ñ*Ó*ð (,ñ	LFà/ðLFð 1ðLFð ˜#˜t˜)Ñ$ó	LFò\7ò7ò
ò 
&òJò[ò"ò"ò5pòn[ó  6r+   r   Ú__main__zJohn Lennoni”  é
   é	   é   é   Ú	LiverpoolÚGBzPaul McCartneyi–  é   é   é   )ÚindentN)"ra   Útypingr   Ú&kerykeion.astrological_subject_factoryr   Ú%kerykeion.schemas.kerykeion_exceptionr   Úkerykeion.schemas.kr_modelsr   r   Úkerykeion.schemas.kr_literalsr   r	   r
   r   r   r   r   Úkerykeion.utilitiesr   r   r   r   r   r   r   r^   Úfrom_birth_dataÚfirstÚsecondÚcomposite_chartÚprintr]   Úmodel_dump_jsonr[   r+   r)   ú<module>r|      sË   ðñ&õP õ NÝ Dß W÷÷ ñ ÷÷ ÷I6ñ I6ðX
 ˆzÒØ6Ð&×6Ñ6°}ÀdÈBÐPQÐSUÐWYÐ[fÐhlÓm€EØ7Ð'×7Ñ7Ð8HÈ$ÐPQÐSUÐWYÐ[]Ð_jÐlpÓq€Fá-¨e°VÓ<€OÙ	ˆ/×
>Ñ
>Ó
@×
PÑ
PÐXYÐ
PÓ
ZÕ[ð r+   