using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Diagnostics;
using VentaUniformes.Application.Exceptions;
using VentaUniformes.Application.Persistencia.Producto.Queries.GetAllProduct;
using VentaUniformes.Application.Sesion.Commands.CreateSesion;
using VentaUniformes.Application.VentasView;
using VentaUniformes.Domain.Models;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace VentaUniformes.app.Controllers
{
    [TypeFilter(typeof(ExceptionManager))]//captura errores de todo el controller
    public class VentaController : Controller
    {
        private readonly ILogger<VentaController> _logger;
        private readonly IVentasViewService ventasViewService;
        public VentaController(ILogger<VentaController> logger,
            IVentasViewService _ventasViewService
            )
        {
            ventasViewService = _ventasViewService;
            _logger = logger;
        }
        //[Authorize(Policy = "AdminOnly")]
        // [AllowAnonymous]
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> Principal()
        {
            var model =await ventasViewService.GetNewVenta();
            //ViewData["Mensaje"] = "";
            return View(model);
        }

        [Authorize]
        [HttpPost]
        public async Task<IActionResult> Principal( VentasViewModel model)
        {
            //verificar 
            //guardar
            //ViewData["Mensaje"] = "";
            if (!await ventasViewService.Save(model))
            {
                ViewData["Mensaje"] = "Error al guardar la venta.";
                return View(model);
            }

            return RedirectToAction("Principal", "Venta");
        }

        [AllowAnonymous]
        [HttpGet]
        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 });
        }
    }
}
