// Controllers/HomeController.cs
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using WebVentaCoche.DataBase;
using WebVentaCoche.Models;

namespace WebVentaCoche.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly ApplicationDbContext _context;

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

        public IActionResult Index() => View();

        public IActionResult Privacy() => View();

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

        //GET:/Home/ProductsHome
        public async Task<IActionResult> ProductsHome()
        {
            var products = await _context.Products.AsNoTracking().ToListAsync();
            return View(products);
        }

        //GET:/Home/ProductsDetailsHome/{id}
        public async Task<IActionResult> ProductsDetailsHome(int id)
        {
            if (id <= 0) 
                return BadRequest();

            var product = await _context.Products.FindAsync(id);
            if (product == null) 
                return NotFound();

            return View(product);
        }
    }
}
