"""System settings model for key-value configuration storage."""

from datetime import datetime
from shopdb.extensions import db


class Setting(db.Model):
    """
    Key-value store for system settings.

    Settings can be managed via the admin UI and are cached
    for performance.
    """
    __tablename__ = 'settings'

    settingid = db.Column(db.Integer, primary_key=True, autoincrement=True)
    key = db.Column(db.String(100), unique=True, nullable=False, index=True)
    value = db.Column(db.Text, nullable=True)
    valuetype = db.Column(db.String(20), default='string')  # string, boolean, integer, json
    category = db.Column(db.String(50), default='general')  # For grouping in UI
    description = db.Column(db.String(255), nullable=True)
    createddate = db.Column(db.DateTime, default=datetime.utcnow)
    modifieddate = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    def to_dict(self):
        return {
            'settingid': self.settingid,
            'key': self.key,
            'value': self.get_typed_value(),
            'valuetype': self.valuetype,
            'category': self.category,
            'description': self.description,
            'createddate': self.createddate.isoformat() + 'Z' if self.createddate else None,
            'modifieddate': self.modifieddate.isoformat() + 'Z' if self.modifieddate else None,
        }

    def get_typed_value(self):
        """Return value converted to its proper type."""
        if self.value is None:
            return None
        if self.valuetype == 'boolean':
            return self.value.lower() in ('true', '1', 'yes')
        if self.valuetype == 'integer':
            try:
                return int(self.value)
            except (ValueError, TypeError):
                return 0
        return self.value

    @classmethod
    def get(cls, key: str, default=None):
        """Get a setting value by key."""
        setting = cls.query.filter_by(key=key).first()
        if setting:
            return setting.get_typed_value()
        return default

    @classmethod
    def set(cls, key: str, value, valuetype: str = 'string', category: str = 'general', description: str = None):
        """Set a setting value, creating if it doesn't exist."""
        setting = cls.query.filter_by(key=key).first()
        if not setting:
            setting = cls(key=key, valuetype=valuetype, category=category, description=description)
            db.session.add(setting)

        # Convert value to string for storage
        if isinstance(value, bool):
            setting.value = 'true' if value else 'false'
        else:
            setting.value = str(value) if value is not None else None

        db.session.commit()
        return setting
