using FruitBank.Common.Enums;
using LinqToDB;
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using Nop.Services.Customers;
using Nop.Web.Framework.Controllers;
using static Nop.Plugin.Misc.FruitBankPlugin.Controllers.CustomerPreOrderController;

namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers;

public class CustomerPreOrderController : BasePluginController
{
    private readonly IWorkContext _workContext;
    private readonly ICustomerService _customerService;
    private readonly PreOrderDbContext _preorderDbContext;
    private readonly FruitBankDbContext _dbContext;

    public CustomerPreOrderController(
        IWorkContext workContext,
        ICustomerService customerService,
        PreOrderDbContext preorderDbContext,
        FruitBankDbContext dbContext)
    {
        _workContext = workContext;
        _customerService = customerService;
        _preorderDbContext = preorderDbContext;
        _dbContext = dbContext;
    }

    [HttpGet]
    public async Task<IActionResult> List()
    {
        var customer = await _workContext.GetCurrentCustomerAsync();
        if (await _customerService.IsGuestAsync(customer))
            return Challenge();

        // Load this customer's preorders, newest first
        var preorders = await _preorderDbContext.PreOrders
            .GetAllByCustomerIdAsync(customer.Id, false)
            .OrderByDescending(p => p.CreatedOnUtc)
            .ToListAsync();

        var allItems = await _preorderDbContext.PreOrderItems.GetAll()
            .Where(i => preorders.Select(p => p.Id).Contains(i.PreOrderId))
            .ToListAsync();

        // Resolve product names
        var productIds  = allItems.Select(i => i.ProductId).Distinct().ToList();
        var productDtos = await _dbContext.ProductDtos
            .GetAll(false)
            .Where(p => productIds.Contains(p.Id))
            .ToListAsync();
        var productById = productDtos.ToDictionary(p => p.Id);

        var rows = preorders.Select(p =>
        {
            var items = allItems.Where(i => i.PreOrderId == p.Id).ToList();

            // Derive status from quantities (enum reads unreliable in LinqToDB)
            var allFulfilled = items.Any() && items.All(i => i.FulfilledQuantity >= i.RequestedQuantity);
            var anyFulfilled = items.Any(i => i.FulfilledQuantity > 0);
            var allDropped   = items.Any() && items.All(i => i.FulfilledQuantity == 0 &&
                                                              i.RequestedQuantity > 0);

            var effectiveStatus = (int)p.Status != 0 ? p.Status
                : allFulfilled  ? PreOrderStatus.Confirmed
                : anyFulfilled  ? PreOrderStatus.PartiallyFulfilled
                : PreOrderStatus.Pending;

            return new CustomerPreOrderRow
            {
                PreOrderId    = p.Id,
                OrderId       = p.OrderId,
                DateOfReceipt = p.DateOfReceipt,
                CreatedOnUtc  = p.CreatedOnUtc,
                Status        = effectiveStatus,
                CustomerNote  = p.CustomerNote,
                Items         = items.Select(i =>
                {
                    productById.TryGetValue(i.ProductId, out var dto);
                    return new CustomerPreOrderItemRow
                    {
                        ProductName       = dto?.Name ?? $"Termék #{i.ProductId}",
                        IsMeasurable      = dto?.IsMeasurable ?? false,
                        RequestedQuantity = i.RequestedQuantity,
                        FulfilledQuantity = i.FulfilledQuantity,
                        UnitPriceInclTax  = i.UnitPriceInclTax,
                        Status            = i.FulfilledQuantity == 0
                            ? PreOrderItemStatus.Pending
                            : i.FulfilledQuantity >= i.RequestedQuantity
                                ? PreOrderItemStatus.Fulfilled
                                : PreOrderItemStatus.PartiallyFulfilled
                    };
                }).ToList()
            };
        }).ToList();

        return View("~/Plugins/Misc.FruitBankPlugin/Views/CustomerPreOrder/List.cshtml", rows);
    }

    // ── Inner models ──────────────────────────────────────────────────────────

    public class CustomerPreOrderRow
    {
        public int                          PreOrderId    { get; set; }
        public int?                         OrderId       { get; set; }
        public DateTime                     DateOfReceipt { get; set; }
        public DateTime                     CreatedOnUtc  { get; set; }
        public PreOrderStatus               Status        { get; set; }
        public string?                      CustomerNote  { get; set; }
        public List<CustomerPreOrderItemRow> Items        { get; set; } = new();
    }

    public class CustomerPreOrderItemRow
    {
        public string            ProductName       { get; set; } = string.Empty;
        public bool              IsMeasurable      { get; set; }
        public int               RequestedQuantity { get; set; }
        public int               FulfilledQuantity { get; set; }
        public decimal           UnitPriceInclTax  { get; set; }
        public PreOrderItemStatus Status           { get; set; }
    }
}
