// File: Plugins/YourCompany.ProductAttributes/Components/ProductAttributesViewComponent.cs

using FruitBank.Common.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Orders;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using Nop.Plugin.Misc.FruitBankPlugin.Models.Orders;
using Nop.Plugin.Misc.FruitBankPlugin.Services;
using Nop.Services.Common;
using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Areas.Admin.Models.Orders;
using Nop.Web.Framework.Components;

namespace Nop.Plugin.Misc.FruitBankPlugin.Components
{
    [ViewComponent(Name = "OrderAttributes")]
    public class OrderAttributesViewComponent : NopViewComponent
    {
        private readonly FruitBankAttributeService _fruitBankAttributeService;
        private readonly IWorkContext _workContext;
        private readonly IStoreContext _storeContext;
        private FruitBankDbContext _dbContext;

        public OrderAttributesViewComponent(FruitBankDbContext dbContext, FruitBankAttributeService fruitBankAttributeService, IWorkContext workContext, IStoreContext storeContext)
        {
            _workContext = workContext;
            _storeContext = storeContext;
            _fruitBankAttributeService = fruitBankAttributeService;

            _dbContext= dbContext;
        }

        public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
        {
            if (additionalData is not OrderModel orderModel) return Content("");

            var model = new OrderAttributesModel { OrderId = orderModel.Id };

            if (model.OrderId > 0)
            {
                var orderDto = await _dbContext.OrderDtos.GetByIdAsync(model.OrderId, true);

                model.IsMeasurable = orderDto.IsMeasurable;
                model.DateOfReceipt = orderDto.DateOfReceipt;
                model.OrderDto = orderDto;
                //var orderPickupAttributeValue = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Order, DateTime?>(model.OrderId, nameof(IOrderDto.DateOfReceipt));

                //if (orderPickupAttributeValue.HasValue && orderPickupAttributeValue.Value != DateTime.MinValue)
                //{
                //    model.DateOfReceipt = orderPickupAttributeValue;
                //}
                //else
                //{
                //    model.DateOfReceipt = null;
                //}
            }

            return View("~/Plugins/Misc.FruitBankPlugin/Views/OrderAttributes.cshtml", model);
        }
    }
}
