"""
Validation utilities for microservices
"""

import re
from typing import Any, Optional
from email_validator import validate_email, EmailNotValidError

def validate_spanish_phone(phone: str) -> bool:
    """Validate Spanish phone number"""
    # Spanish phone pattern: +34 followed by 9 digits
    pattern = r'^(\+34|0034|34)?[6-9]\d{8}$'
    return bool(re.match(pattern, phone.replace(' ', '').replace('-', '')))

def validate_email_address(email: str) -> bool:
    """Validate email address"""
    try:
        validate_email(email)
        return True
    except EmailNotValidError:
        return False

def validate_tenant_name(name: str) -> bool:
    """Validate tenant name"""
    # Must be 2-50 characters, letters, numbers, spaces, hyphens, apostrophes
    pattern = r"^[a-zA-ZÀ-ÿ0-9\s\-']{2,50}$"
    return bool(re.match(pattern, name))

def validate_address(address: str) -> bool:
    """Validate address"""
    # Must be 5-200 characters
    return 5 <= len(address.strip()) <= 200

def validate_coordinates(latitude: float, longitude: float) -> bool:
    """Validate Madrid coordinates"""
    # Madrid is roughly between these coordinates
    madrid_bounds = {
        'lat_min': 40.3,
        'lat_max': 40.6,
        'lon_min': -3.8,
        'lon_max': -3.5
    }
    
    return (
        madrid_bounds['lat_min'] <= latitude <= madrid_bounds['lat_max'] and
        madrid_bounds['lon_min'] <= longitude <= madrid_bounds['lon_max']
    )

def validate_product_name(name: str) -> bool:
    """Validate product name"""
    # Must be 1-50 characters, letters, numbers, spaces
    pattern = r"^[a-zA-ZÀ-ÿ0-9\s]{1,50}$"
    return bool(re.match(pattern, name))

def validate_positive_number(value: Any) -> bool:
    """Validate positive number"""
    try:
        return float(value) > 0
    except (ValueError, TypeError):
        return False

def validate_non_negative_number(value: Any) -> bool:
    """Validate non-negative number"""
    try:
        return float(value) >= 0
    except (ValueError, TypeError):
        return False