from sqlalchemy.orm import Session
from app.models.system_config import SystemConfig

class SystemConfigService:
    @staticmethod
    def get_config(db: Session, key: str) -> str | None:
        config = db.query(SystemConfig).filter(SystemConfig.key == key).first()
        return config.value if config else None

    @staticmethod
    def set_config(db: Session, key: str, value: str, description: str = None) -> SystemConfig:
        config = db.query(SystemConfig).filter(SystemConfig.key == key).first()
        if config:
            config.value = value
            if description:
                config.description = description
        else:
            config = SystemConfig(key=key, value=value, description=description)
            db.add(config)
        
        db.commit()
        db.refresh(config)
        return config

    @staticmethod
    def get_all_configs(db: Session):
        return db.query(SystemConfig).all()

