<?php

namespace App\Http\Controllers;

use App\Models\Device;
use App\Models\SendLog;
use App\Services\PingService;

class DashboardController extends Controller
{
    public function index()
    {
        $devices      = Device::with('schedule')->get();
        $totalDevices = $devices->count();
        $today        = now()->startOfDay();

        $successToday = SendLog::where('created_at', '>=', $today)->where('success', true)->count();
        $failedToday  = SendLog::where('created_at', '>=', $today)->where('success', false)->count();
        $recentLogs   = SendLog::with('device')->latest()->limit(10)->get();

        $onlineCount  = $devices->where('last_status', 'online')->count();
        $offlineCount = $devices->where('last_status', 'offline')->count();

        return view('dashboard', compact(
            'devices', 'totalDevices', 'successToday', 'failedToday',
            'recentLogs', 'onlineCount', 'offlineCount'
        ));
    }
}
