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 CreateBuyerAddressDTOToEntity(c *gin.Context, req *dto.CreateBuyerAddressRequest) *entities.CreateBuyerAddressParams {
	return &entities.CreateBuyerAddressParams{
		// Buyer address
		BuyerID:   middleware.GetBuyerId(c),
		Label:     req.Label,
		Phone:     req.Phone,
		IsDefault: req.IsDefault,
		// Address
		CountryCode:         req.CountryCode,
		Address1:            req.Address1,
		Address2:            req.Address2,
		PostalCode:          req.PostalCode,
		UnitNumber:          req.UnitNumber,
		RecipientName:       req.RecipientName,
		CountryID:           entities.CountryIdsMap[req.CountryCode],
		Deactivate:          false,
		SubLevelLocationIDs: req.SubLevelLocationIDs,
	}
}

func GetBuyerAddressByIDDTOToEntity(c *gin.Context, req *dto.GetBuyerAddressByIDRequest) *entities.GetBuyerAddressByIDParams {
	return &entities.GetBuyerAddressByIDParams{
		ID:      req.ID,
		BuyerID: middleware.GetBuyerId(c),
	}
}

func UpdateBuyerAddressDTOToEntity(c *gin.Context, req *dto.UpdateBuyerAddressRequest) *entities.UpdateBuyerAddressParams {
	return &entities.UpdateBuyerAddressParams{
		ID:                  req.ID,
		BuyerID:             middleware.GetBuyerId(c),
		Label:               req.Label,
		RecipientName:       req.RecipientName,
		IsDefault:           req.IsDefault,
		Phone:               req.Phone,
		Address1:            req.Address1,
		Address2:            req.Address2,
		PostalCode:          req.PostalCode,
		UnitNumber:          req.UnitNumber,
		SubLevelLocationIDs: req.SubLevelLocationIDs,
		Deactivate:          req.Deactivate,
	}
}

func DeleteBuyerAddressDTOToEntity(c *gin.Context, req *dto.DeleteBuyerAddressRequest) *entities.DeleteBuyerAddressParams {
	return &entities.DeleteBuyerAddressParams{
		ID:      req.ID,
		BuyerID: middleware.GetBuyerId(c),
	}
}

func BuyerAddressEntityToDTO(entity *entities.BuyerAddress) *dto.BuyerAddressResponse {
	locations := make([]*dto.SubLevelLocation, 0)
	for _, v := range entity.Address.SortSubLevelLocations() {
		locations = append(locations, &dto.SubLevelLocation{
			ID:                v.ID.ToInt64(),
			InternationalName: v.InternationalName,
			LocalizedName:     v.LocalizedName,
			ParentID:          v.ParentID,
		})
	}

	return &dto.BuyerAddressResponse{
		ID:            entity.ID.ToInt64(),
		Label:         entity.Label,
		RecipientName: entity.RecipientName,
		IsDefault:     entity.IsDefault,
		Phone:         entity.Phone,
		Address: dto.Address{
			ID:                entity.AddressID,
			Address1:          entity.Address.Address1,
			Address2:          entity.Address.Address2,
			PostalCode:        entity.Address.PostalCode,
			UnitNumber:        entity.Address.UnitNumber,
			CountryID:         entity.Address.CountryID,
			Deactivate:        entity.Address.Deactivate,
			SubLevelLocations: locations,
		},
	}
}

func BuyerAddressEntitiesToBuyerAddressArrayResponseDTO(es []*entities.BuyerAddress) *dto.BuyerAddressArrayResponse {
	bas := make([]*dto.BuyerAddressResponse, len(es))
	for i, v := range es {
		bas[i] = BuyerAddressEntityToDTO(v)
	}
	return &dto.BuyerAddressArrayResponse{
		Data: bas,
	}
}
