"""Auth service"""
import hashlib, re
from datetime import datetime, timezone
import bcrypt
from app.models import User

class AuthService:
    @staticmethod
    def dummy_hash_check():
        bcrypt.checkpw(b"dummy", bcrypt.hashpw(b"dummy", bcrypt.gensalt(4)))

    @staticmethod
    def validate_password(password, confirm):
        errors = []
        if password != confirm:
            errors.append("Les mots de passe ne correspondent pas.")
        if len(password) < 12:
            errors.append("Minimum 12 caractères.")
        if not re.search(r"[A-Z]", password):
            errors.append("Au moins une majuscule.")
        if not re.search(r"[0-9]", password):
            errors.append("Au moins un chiffre.")
        if not re.search(r"[^A-Za-z0-9]", password):
            errors.append("Au moins un caractère spécial.")
        return errors

    @staticmethod
    def verify_reset_token(token):
        hashed = hashlib.sha256(token.encode()).hexdigest()
        user = User.query.filter_by(password_reset_token=hashed).first()
        if not user or not user.password_reset_expires:
            return None
        if user.password_reset_expires < datetime.now(timezone.utc):
            return None
        return user

    @staticmethod
    def send_reset_email(user, token):
        pass  # À implémenter avec Flask-Mail
