package transform

import (
	"time"

	"github.com/UpmeshLTD/urus-aio/infrastructure/db/entities"
	"github.com/UpmeshLTD/urus-aio/paid-order/service/dto"
)

func FulfillmentEntityToDTO(entity *entities.Fulfillment, deliveryAddresses []*entities.DeliveryAddress) *dto.Fulfillment {
	mappingCartIdAndDeliveryAddress := make(map[string]*entities.DeliveryAddress)
	for _, deliveryAddress := range deliveryAddresses {
		mappingCartIdAndDeliveryAddress[deliveryAddress.CartID] = deliveryAddress
	}

	currentStep := entity.Step
	orders := make([]*dto.OrderDetail, 0)
	for _, v := range entity.PaidOrders {
		orders = append(orders, &dto.OrderDetail{
			ID:              v.ID,
			CartID:          v.CartID,
			ShippingGroupID: v.ShippingGroupID,
			Width:           v.Dimension.Width,
			Length:          v.Dimension.Length,
			Height:          v.Dimension.Height,
			Weight:          &v.Dimension.Weight,
			EstimatedFee:    v.EstimatedFee,
			DeliveryAddress: DeliveryAddressEntityToDTO(mappingCartIdAndDeliveryAddress[v.CartID]),
		})

		if currentStep > entities.FulfillmentStepOrderDetails {
			for _, v := range v.IncorrectInformation {
				if v == entities.IncorrectDeliveryAddress || v == entities.IncorrectDeliveryPhoneNumber {
					currentStep = entities.FulfillmentStepOrderDetails
					break
				}
			}
		}
	}

	return &dto.Fulfillment{
		ID: entity.ID,
		Shipping: dto.Shipping{
			CourierDeliveryPlanID: entity.CourierDeliveryPlanID,
			CourierID:             entity.CourierID,
		},
		OrderDetails: orders,
		PickupDetail: dto.PickupDetail{
			PickupPointID:     entity.PickupPointID,
			PickupTime:        entity.PickupTime,
			PickupStartTime:   entity.PickupStartTime,
			PickupEndTime:     entity.PickupEndTime,
			PickupInstruction: entity.PickupInstruction,
		},
		Step:      currentStep,
		CreatedAt: entity.CreatedAt,
		UpdatedAt: entity.UpdatedAt,
	}
}

func FulfillmentEntitiesToDTOs(entities []*entities.Fulfillment) []*dto.Fulfillment {
	values := make([]*dto.Fulfillment, 0, len(entities))
	for _, v := range entities {
		values = append(values, FulfillmentEntityToDTO(v, nil))
	}
	return values
}

func UpdateFulfillmentRequestDTOToEntity(req *dto.UpdateFulfillmentRequest) *entities.Fulfillment {
	var (
		pickupPointID     *int64
		courierID         *int64
		courierDeliveryID *int64
		pickupTime        *time.Time
		pickupStartTime   *time.Time
		pickupEndTime     *time.Time
		pickupInstruction *string
	)
	if req.Shipping != nil {
		courierID = req.Shipping.CourierID
		courierDeliveryID = req.Shipping.CourierDeliveryPlanID
	}
	if req.PickupDetail != nil {
		pickupPointID = req.PickupDetail.PickupPointID
		pickupTime = req.PickupDetail.PickupTime
		pickupStartTime = req.PickupDetail.PickupStartTime
		pickupEndTime = req.PickupDetail.PickupEndTime
		pickupInstruction = req.PickupDetail.PickupInstruction
	}
	return &entities.Fulfillment{
		CourierID:             courierID,
		CourierDeliveryPlanID: courierDeliveryID,
		PickupPointID:         pickupPointID,
		PickupTime:            pickupTime,
		PickupStartTime:       pickupStartTime,
		PickupEndTime:         pickupEndTime,
		PickupInstruction:     pickupInstruction,
		Step:                  req.Step,
	}
}
