using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DiplomovaPrace.Contracts.Customer;
using DiplomovaPrace.Contracts.Customer.ProductCategories.ViewModels;
using DiplomovaPrace.Contracts.Customer.Products;
using DiplomovaPrace.Contracts.Customer.Products.ViewModels;
using DiplomovaPrace.Facades.Customer.ProductCategories;
using DiplomovaPrace.Facades.Customer.Products;
using Microsoft.AspNetCore.Mvc;

namespace DiplomovaPrace.Web.Server.Controllers
{
	[Route("api/product")]
	public class ProductController : ControllerBase
	{
		private readonly IProductFacade productFacade;
		private readonly IProductCategoryFacade productCategoryFacade;

		public ProductController(IProductFacade productFacade, IProductCategoryFacade productCategoryFacade)
		{
			this.productFacade = productFacade;
			this.productCategoryFacade = productCategoryFacade;
		}

		[HttpGet("getProducts")]
		public async Task<List<ProductVM>> GetProductsAsync()
		{
			return await productFacade.GetAllProductsAsync();
		}

		[HttpPost("getProducts")]
		public async Task<PagedQueryResult<ProductVM>> GetProductsAsync([FromBody] PagedQueryRequest<ProductListQueryFilter> pagedQueryRequest)
		{
			return await productFacade.GetPagedProductsAsync(pagedQueryRequest);
		}

		[HttpGet("getProduct/{productId}")]
		public async Task<ProductVM> GetProductAsync(int productId)
		{
			return await productFacade.GetProductAsync(productId);
		}

		[HttpGet("getCategories")]
		public async Task<List<ProductCategoryVM>> GetProductCategoriesAsync()
		{
			return await productCategoryFacade.GetAllProductCategoriesAsync();
		}

		[HttpPost("isInStock")]
		public async Task<IActionResult> AreProductsInStock([FromBody] List<ProductInStockDto> products)
		{
			var notInStockProducts = await productFacade.AreProductsInStockAsync(products);

			if (notInStockProducts.Count > 0)
			{
				//return BadRequest(notInStockProducts);
				return StatusCode(406, notInStockProducts);
			}

			return Ok();
		}
	}
}