package transform

import (
	"github.com/UpmeshLTD/urus-aio/infrastructure/db/entities"
	"github.com/UpmeshLTD/urus-aio/infrastructure/server/transport/http/gin/middleware"
	"github.com/UpmeshLTD/urus-aio/mdm/server/http/dto"
	"github.com/gin-gonic/gin"
)

func SaveDraftCheckoutInfoDTOToParams(c *gin.Context, req *dto.SaveDraftCheckoutInfoRequest) *entities.SaveDraftCheckoutInfoParams {
	return &entities.SaveDraftCheckoutInfoParams{
		CartID:               req.CartID,
		PhoneNumber:          req.PhoneNumber,
		DeliveryInstructions: req.DeliveryInstructions,
		Email:                req.Email,
		RecipientName:        req.RecipientName,
		BuyerID:              middleware.GetBuyerId(c),
		// Address
		Address1:          req.Address1,
		Address2:          req.Address2,
		PostalCode:        req.PostalCode,
		UnitNumber:        req.UnitNumber,
		CountryID:         entities.CountryIdsMap[req.CountryCode],
		SubLevelLocations: SubLevelLocationsDTOToEntity(req.SubLevelLocationIDs),
	}
}

func DraftCheckoutInfoEntityToDTO(entity *entities.DraftCheckoutInfo) *dto.DraftCheckoutInfoResponse {
	return &dto.DraftCheckoutInfoResponse{
		CartID:               entity.CartID,
		PhoneNumber:          entity.PhoneNumber,
		DeliveryInstructions: entity.DeliveryInstructions,
		Email:                entity.Email,
		RecipientName:        entity.RecipientName,
		Address:              AddressEntityToDTO(&entity.Address),
	}
}
