using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using Microsoft.AspNetCore.Mvc;

namespace VendorClient.Controllers
{
    [Route("Home/[action]")]
    public class PurchaseController : Controller
    {
        private readonly ILogger<PurchaseController> _logger;

        private const string API_ROUTE = "api/purchases";

        public PurchaseController(ILogger<PurchaseController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IActionResult Products()
        {
            if (APIClient.Vendor == null)
            {
                return Redirect("~/Enter");
            }
            ViewBag.Goods = APIClient.GetRequest<List<ProductViewModel>>($"api/products/GetAllProducts");
            return View();
        }

        [HttpPost]
        public void PurchaseCreate([FromBody] PurchaseBindingModel purchaseModel)
        {
            if (APIClient.Vendor == null)
            {
                throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
            }
            if (purchaseModel.Cost <= 0)
            {
                throw new Exception("Цена должна быть больше 0");
            }
            purchaseModel.VendorId = APIClient.Vendor.Id;
            APIClient.PostRequest($"{API_ROUTE}/createpurchase", purchaseModel);
        }

        [HttpGet]
        public void PurchaseDelete(int id)
        {
            if (APIClient.Vendor == null)
            {
                throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
            }
            if (id <= 0)
            {
                throw new Exception("Некорректный идентификатор");
            }
            var purchase = APIClient.GetRequest<PurchaseViewModel>($"api/purchase/getpurchase?purchaseId={id}");
            APIClient.PostRequest($"{API_ROUTE}/DeletePurchase", new PurchaseBindingModel
            {
                Id = id,
            });
        }

        [HttpGet]
        public IActionResult Purchases()
        {
            if (APIClient.Vendor == null)
            {
                return Redirect("~/Home/Enter");
            }

            ViewBag.Purchases = APIClient.PostRequestWithResult<PurchaseSearchModel, List<PurchaseViewModel>>(
                $"{API_ROUTE}/getpurchases", new() { VendorId = APIClient.Vendor.Id });
            return View();
        }

        [HttpGet]
        public void PurchaseUpdate([FromBody] PurchaseBindingModel purchaseModel)
        {
            if (APIClient.Vendor == null)
            {
                throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
            }
            purchaseModel.VendorId = APIClient.Vendor.Id;
            APIClient.PostRequest("api/purchase/update", purchaseModel);
        }

        [HttpGet]
        public string Report(DateTime dateFrom, DateTime dateTo)
        {
            if (APIClient.Vendor == null)
            {
                throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
            }
            return "NotImplement";
        }
    }
}