"""YakPanel - Dashboard API"""
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func

from app.core.database import get_db
from app.api.auth import get_current_user
from app.models.user import User

router = APIRouter(prefix="/dashboard", tags=["dashboard"])


@router.get("/stats")
async def get_stats(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    """Get dashboard statistics"""
    import psutil

    from app.services.site_service import get_site_count
    from app.models.ftp import Ftp
    from app.models.database import Database
    from sqlalchemy import select, func
    site_count = await get_site_count(db)
    ftp_result = await db.execute(select(func.count()).select_from(Ftp))
    ftp_count = ftp_result.scalar() or 0
    db_result = await db.execute(select(func.count()).select_from(Database))
    database_count = db_result.scalar() or 0

    # System stats
    cpu_percent = psutil.cpu_percent(interval=1)
    memory = psutil.virtual_memory()
    disk = psutil.disk_usage("/")

    return {
        "site_count": site_count,
        "ftp_count": ftp_count,
        "database_count": database_count,
        "system": {
            "cpu_percent": cpu_percent,
            "memory_percent": memory.percent,
            "memory_used_mb": round(memory.used / 1024 / 1024, 1),
            "memory_total_mb": round(memory.total / 1024 / 1024, 1),
            "disk_percent": disk.percent,
            "disk_used_gb": round(disk.used / 1024 / 1024 / 1024, 2),
            "disk_total_gb": round(disk.total / 1024 / 1024 / 1024, 2),
        },
    }
