"""
Modèles pour les paramètres de l'application
"""
from datetime import datetime, timezone
from app_new.extensions import db
import os

def _cipher():
    from cryptography.fernet import Fernet
    key = os.environ.get('OUTLOOK_ENCRYPTION_KEY')
    return Fernet(key.encode()) if key else None


class AppSettings(db.Model):
    """Paramètres globaux de l'application."""
    __tablename__ = 'app_settings'
    
    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(100), unique=True, nullable=False, index=True)
    value = db.Column(db.Text, nullable=True)
    description = db.Column(db.String(255), nullable=True)
    is_encrypted = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    @staticmethod
    def get(key, default=None):
        """Récupère un paramètre par sa clé."""
        setting = AppSettings.query.filter_by(key=key).first()
        if setting:
            if setting.is_encrypted and setting.value and setting.value.startswith('enc:'):
                cipher = _cipher()
                if not cipher:
                    return default
                try:
                    return cipher.decrypt(setting.value[4:].encode()).decode()
                except Exception:
                    return default
            return setting.value
        return default
    
    @staticmethod
    def set(key, value, description=None, is_encrypted=False):
        """Définit un paramètre."""
        stored_value = value
        if is_encrypted and value:
            cipher = _cipher()
            if not cipher:
                raise RuntimeError("OUTLOOK_ENCRYPTION_KEY est requise pour stocker ce secret.")
            stored_value = 'enc:' + cipher.encrypt(value.encode()).decode()
        setting = AppSettings.query.filter_by(key=key).first()
        if setting:
            setting.value = stored_value
            setting.is_encrypted = is_encrypted
            setting.updated_at = datetime.now(timezone.utc)
            if description:
                setting.description = description
        else:
            setting = AppSettings(
                key=key,
                value=stored_value,
                description=description,
                is_encrypted=is_encrypted
            )
            db.session.add(setting)
        db.session.commit()
        return setting
    
    @staticmethod
    def get_openrouter_api_key():
        """Récupère la clé API OpenRouter."""
        return AppSettings.get('openrouter_api_key')
    
    @staticmethod
    def set_openrouter_api_key(api_key):
        """Définit la clé API OpenRouter."""
        return AppSettings.set(
            'openrouter_api_key',
            api_key,
            description='Clé API OpenRouter pour l\'interprétation des emails',
            is_encrypted=True
        )
    
    def __repr__(self):
        return f'<AppSettings {self.key}>'
