"""YakPanel - Config service (panel settings)"""
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from app.models.config import Config
from app.core.config import get_settings


async def get_config_value(db: AsyncSession, key: str) -> str:
    """Get config value by key"""
    result = await db.execute(select(Config).where(Config.key == key))
    row = result.scalar_one_or_none()
    return row.value if row else ""


async def set_config_value(db: AsyncSession, key: str, value: str) -> None:
    """Set config value"""
    result = await db.execute(select(Config).where(Config.key == key))
    row = result.scalar_one_or_none()
    if row:
        row.value = value
    else:
        db.add(Config(key=key, value=value))
    await db.commit()


def get_webserver_type() -> str:
    """Get webserver type (nginx, apache, openlitespeed)"""
    return get_settings().webserver_type


def get_setup_path() -> str:
    """Get server setup path"""
    return get_settings().setup_path


def get_www_root() -> str:
    """Get www root path"""
    return get_settings().www_root


def get_www_logs() -> str:
    """Get www logs path"""
    return get_settings().www_logs


def get_vhost_path() -> str:
    """Get vhost config path"""
    return get_settings().vhost_path
