using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using watcher_monitoring.Models;

using watcher_monitoring.Data;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace watcher_monitoring.Controllers;

[Authorize]
public class HomeController : Controller
{
    private readonly WatcherDbContext _context;
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger, WatcherDbContext dbContext)
    {
        _logger = logger;
        _context = dbContext;
    }

    // Dashboard
    public async Task<IActionResult> Index()
    {
        List<Server> servers = await _context.Servers.ToListAsync();

        ViewBag.TotalServers = servers.Count;
        ViewBag.OnlineServers = servers.Count(s => s.IsOnline);
        ViewBag.OfflineServers = servers.Count(s => !s.IsOnline);
        ViewBag.ServiceCount = 8;
        ViewBag.Servers = servers;

        return View();
    }

    public IActionResult Privacy()
    {
        return View();
    }

    public IActionResult SystemInformation()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}
