using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using kehati.Models.View;
using kehati.Database;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace kehati.Controllers;

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;
    private readonly ApplicationDbContext db;

    public HomeController(ILogger<HomeController> logger,ApplicationDbContext context)
    {
        _logger = logger;
        db = context;
    }

    public async Task<IActionResult> Index()
    {
        //memanggil data lokasi dari master DB Lokasi
        ViewBag.lokasiHome = await db.Lokasi
                        .OrderByDescending(l => l.MsLokasiId)
                        .Take(3)
                        .ToListAsync();
        ViewBag.tumbuhanHome = await db.Tumbuhan
                        .OrderByDescending(l => l.MsTumbuhanId)
                        .Take(4)
                        .ToListAsync();
        
        ViewBag.faunaHome = await db.Fauna
                        .OrderByDescending(l => l.MsFaunaId)
                        .Take(4)
                        .ToListAsync();
        
        var model = new HomeViewModel
        {
            Statistik = new StatistikHomeModel
            {
                TotalLocations = 10,
                TotalFlora = 50,
                TotalFauna = 30,
                TotalSpecies = 80
            },
        };

        return View(model);
    }

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

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