"""Phone number value object.

Phone represents a validated phone number.
"""

import re
from typing import Any


class Phone:
    """Phone number value object.
    
    Represents a validated Iranian phone number.
    Supports both mobile and landline numbers.
    
    Attributes:
        value: Phone number string (normalized format)
    """
    
    # Iranian mobile: starts with 09, 10 digits
    MOBILE_REGEX = re.compile(r'^09\d{9}$')
    
    # International format: +989...
    INTERNATIONAL_REGEX = re.compile(r'^\+989\d{9}$')
    
    def __init__(self, value: str):
        """Initialize phone number.
        
        Args:
            value: Phone number string
            
        Raises:
            ValueError: If phone format is invalid
        """
        normalized = self._normalize(value)
        if not self._is_valid(normalized):
            raise ValueError(f"Invalid phone number: {value}")
        self._value = normalized
    
    @property
    def value(self) -> str:
        """Get phone number.
        
        Returns:
            str: Phone number
        """
        return self._value
    
    @property
    def international_format(self) -> str:
        """Get international format.
        
        Returns:
            str: +989XXXXXXXXX format
        """
        if self._value.startswith('+'):
            return self._value
        return f"+98{self._value[1:]}"
    
    @staticmethod
    def _normalize(phone: str) -> str:
        """Normalize phone number.
        
        Removes spaces, dashes, parentheses.
        
        Args:
            phone: Phone string
            
        Returns:
            str: Normalized phone
        """
        if not phone:
            return ""
        
        # Remove common separators
        phone = re.sub(r'[\s\-\(\)]', '', phone.strip())
        
        # Convert international format to local
        if phone.startswith('+98'):
            phone = '0' + phone[3:]
        elif phone.startswith('0098'):
            phone = '0' + phone[4:]
        elif phone.startswith('98') and len(phone) == 12:
            phone = '0' + phone[2:]
        
        return phone
    
    @staticmethod
    def _is_valid(phone: str) -> bool:
        """Validate phone number.
        
        Args:
            phone: Normalized phone string
            
        Returns:
            bool: True if valid
        """
        if not phone or not isinstance(phone, str):
            return False
        
        # Check if matches mobile pattern
        return bool(Phone.MOBILE_REGEX.match(phone))
    
    def __str__(self) -> str:
        """String representation.
        
        Returns:
            str: Phone number
        """
        return self._value
    
    def __repr__(self) -> str:
        """Developer representation.
        
        Returns:
            str: Phone representation
        """
        return f"Phone('{self._value}')"
    
    def __eq__(self, other: Any) -> bool:
        """Check equality.
        
        Args:
            other: Other object
            
        Returns:
            bool: True if equal
        """
        if not isinstance(other, Phone):
            return False
        return self._value == other._value
    
    def __hash__(self) -> int:
        """Hash value.
        
        Returns:
            int: Hash
        """
        return hash(self._value)

