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

using watcher_monitoring.Data;
using watcher_monitoring.ViewModels;
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();
        List<Container> _containers = await _context.Containers.ToListAsync();

        var homeVm = new HomeViewModel
        {
            servers = _servers,
            containers = _containers,
            serversCount = _servers.Count,
            serversOnline = (from server in _servers where server.State=="online" select server).Count(),
            serversOffline = (from server in _servers where server.State=="offline" select server).Count()
        };

        return View(homeVm);
    }

    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 });
    }
}
