import os
import re
from django.core.exceptions import ValidationError
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _

def validate_phone_number(value: str) -> None:
    """Validate phone number format."""
    if not value:
        return
    # Basic regex for phone numbers: optional +, followed by 9 to 15 digits
    pattern = r'^\+?1?\d{9,15}$'
    if not re.match(pattern, value):
        raise ValidationError(
            _("Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
        )

def validate_resume_file(value) -> None:
    """Validate that the uploaded file is a PDF, DOC, or DOCX and is under 5MB."""
    ext = os.path.splitext(value.name)[1].lower()
    valid_extensions = ['.pdf', '.doc', '.docx']
    if ext not in valid_extensions:
        raise ValidationError(
            _("Unsupported file extension. Only PDF, DOC, and DOCX are allowed.")
        )
    
    limit = 5 * 1024 * 1024  # 5 MB
    if value.size > limit:
        raise ValidationError(_("File size exceeds the 5 MB limit."))

def validate_document_file(value) -> None:
    """Validate that verification documents are under 5MB."""
    limit = 5 * 1024 * 1024  # 5 MB
    if value.size > limit:
        raise ValidationError(_("File size exceeds the 5 MB limit."))

def validate_non_negative(value) -> None:
    """Validate that CTC or experience values are not negative."""
    if value is not None and value < 0:
        raise ValidationError(_("This value cannot be negative."))

def validate_passing_year(value: int) -> None:
    """Validate that passing year does not exceed the current year."""
    if value is not None:
        current_year = now().year
        if value > current_year:
            raise ValidationError(
                _("Passing year cannot exceed the current year (%(current_year)d)."),
                params={'current_year': current_year},
            )
