using Lada.Framework.DTO;
using Ladaer.BackEnd.Infrastructures.DTO.Reports.Items.Orders;
using System.Linq;

namespace Ladaer.BackEnd.Infrastructures.DTO.Reports.Results
{
    /// <summary>
    /// Kết quả báo cáo doanh thu đơn hàng
    /// </summary>
    public class OrderRevenueResult : BaseResult
    {
        private List<OrderRevenueItem> _items = new List<OrderRevenueItem>();

        public List<OrderRevenueItem> Items
        {
            get => _items;
            set
            {
                _items = value ?? new List<OrderRevenueItem>();
                UpdateTotalItem();
            }
        }

        public OrderRevenueItem OrderRevenueTotalItem { get; private set; } = new OrderRevenueItem();
        public double TotalRecord { get; set; }

        /// <summary>
        /// Cập nhật OrderRevenueTotalItem dựa trên Items
        /// </summary>
        private void UpdateTotalItem()
        {
            if (_items == null || !_items.Any())
            {
                OrderRevenueTotalItem = new OrderRevenueItem();
                return;
            }

            OrderRevenueTotalItem = new OrderRevenueItem
            {
                OrderDate = DateTime.MinValue, // Ngày tổng hợp
                TotalOrders = _items.Sum(x => x.TotalOrders),
                TotalRevenue = _items.Sum(x => x.TotalRevenue),
                TotalCollectAmount = _items.Sum(x => x.TotalCollectAmount),

                WaitPickup = _items.Sum(x => x.WaitPickup),
                WaitPickupRevenue = _items.Sum(x => x.WaitPickupRevenue),
                WaitPickupCollectAmount = _items.Sum(x => x.WaitPickupCollectAmount),

                Picking = _items.Sum(x => x.Picking),
                PickingRevenue = _items.Sum(x => x.PickingRevenue),
                PickingCollectAmount = _items.Sum(x => x.PickingCollectAmount),

                Picked = _items.Sum(x => x.Picked),
                PickedRevenue = _items.Sum(x => x.PickedRevenue),
                PickedCollectAmount = _items.Sum(x => x.PickedCollectAmount),

                InWarehouse = _items.Sum(x => x.InWarehouse),
                InWarehouseRevenue = _items.Sum(x => x.InWarehouseRevenue),
                InWarehouseCollectAmount = _items.Sum(x => x.InWarehouseCollectAmount),

                OutWarehouse = _items.Sum(x => x.OutWarehouse),
                OutWarehouseRevenue = _items.Sum(x => x.OutWarehouseRevenue),
                OutWarehouseCollectAmount = _items.Sum(x => x.OutWarehouseCollectAmount),

                Delivering = _items.Sum(x => x.Delivering),
                DeliveringRevenue = _items.Sum(x => x.DeliveringRevenue),
                DeliveringCollectAmount = _items.Sum(x => x.DeliveringCollectAmount),

                Delivered = _items.Sum(x => x.Delivered),
                DeliveredRevenue = _items.Sum(x => x.DeliveredRevenue),
                DeliveredCollectAmount = _items.Sum(x => x.DeliveredCollectAmount),

                Cancelled = _items.Sum(x => x.Cancelled),
                CancelledRevenue = _items.Sum(x => x.CancelledRevenue),
                CancelledCollectAmount = _items.Sum(x => x.CancelledCollectAmount)
            };
        }

        /// <summary>
        /// Thêm item vào danh sách và tự động cập nhật tổng
        /// </summary>
        public void AddItem(OrderRevenueItem item)
        {
            if (item != null)
            {
                _items.Add(item);
                UpdateTotalItem();
            }
        }

        /// <summary>
        /// Xóa tất cả items và reset tổng
        /// </summary>
        public void ClearItems()
        {
            _items.Clear();
            UpdateTotalItem();
        }
    }
}