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 theo sale
    /// </summary>
    public class SaleRevenueResult : BaseResult
    {
        private List<SaleRevenueItem> _items = new List<SaleRevenueItem>();

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

        public SaleRevenueItem SaleRevenueTotalItem { get; private set; } = new SaleRevenueItem();
        public double TotalRecord { get; set; }

        /// <summary>
        /// Cập nhật item tổng hợp từ danh sách items
        /// </summary>
        private void UpdateTotalItem()
        {
            if (_items == null || !_items.Any())
            {
                SaleRevenueTotalItem = new SaleRevenueItem();
                return;
            }

            SaleRevenueTotalItem = new SaleRevenueItem
            {
                SaleId = 0, // ID = 0 để đánh dấu là dòng tổng hợp
                SaleName = "TỔNG CỘNG",
                SalePhoneNumber = "",
                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),

                PickupFailed = _items.Sum(x => x.PickupFailed),
                PickupFailedRevenue = _items.Sum(x => x.PickupFailedRevenue),
                PickupFailedCollectAmount = _items.Sum(x => x.PickupFailedCollectAmount),

                Transporting = _items.Sum(x => x.Transporting),
                TransportingRevenue = _items.Sum(x => x.TransportingRevenue),
                TransportingCollectAmount = _items.Sum(x => x.TransportingCollectAmount),

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

                DeliveryFailed = _items.Sum(x => x.DeliveryFailed),
                DeliveryFailedRevenue = _items.Sum(x => x.DeliveryFailedRevenue),
                DeliveryFailedCollectAmount = _items.Sum(x => x.DeliveryFailedCollectAmount),

                DeliverySuccess = _items.Sum(x => x.DeliverySuccess),
                DeliverySuccessRevenue = _items.Sum(x => x.DeliverySuccessRevenue),
                DeliverySuccessCollectAmount = _items.Sum(x => x.DeliverySuccessCollectAmount),

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