package transform

import (
	"encoding/json"

	"github.com/UpmeshLTD/urus-aio/infrastructure/db/entities"
	"github.com/UpmeshLTD/urus-aio/shipment/server/http/dto"
)

func CreateSellerCourierRequestDTOToEntity(req *dto.CreateSellerCourierRequest, workspaceID string, courierAuthenticationInfo string, salt string, verified bool) *entities.SellerCourier {
	return &entities.SellerCourier{
		WorkspaceID:               workspaceID,
		CourierID:                 req.CourierID,
		CourierAuthenticationInfo: courierAuthenticationInfo,
		Salt:                      salt,
		Verified:                  verified,
	}
}

func GetSellerCouriersResponseEntityToDTO(res []*entities.SellerCourier) *dto.GetSellerCouriersResponse {
	data := make([]*dto.SellerCourier, 0, len(res))
	for _, v := range res {
		data = append(data, SellerCourierEntityToDTO(v))
	}

	return &dto.GetSellerCouriersResponse{
		Data: data,
	}
}

func GetSellerCouriersRequestDTOToEntity(req *dto.GetSellerCouriersRequest, workspaceID string) *entities.GetSellerCouriersParams {
	return &entities.GetSellerCouriersParams{
		WorkspaceID: workspaceID,
		Deactivate:  req.Deactivate,
		Verified:    req.Verified,
	}
}

func SellerCourierEntityToDTO(res *entities.SellerCourier) *dto.SellerCourier {
	var courierAuthenticationInfo map[string]interface{}
	_ = json.Unmarshal([]byte(res.CourierAuthenticationInfo), &courierAuthenticationInfo)

	return &dto.SellerCourier{
		ID:                        res.ID.ToInt64(),
		CourierID:                 res.CourierID,
		CourierAuthenticationInfo: courierAuthenticationInfo,
		Courier: &dto.Courier{
			ID:         res.Courier.ID.ToInt64(),
			Name:       res.Courier.Name,
			CountryID:  res.Courier.CountryID,
			Deactivate: res.Courier.Deactivate,
			Type:       res.Courier.Type,
			Code:       res.Courier.Code,
		},
		Verified:    res.Verified,
		Deactivate:  res.Deactivate,
		LastUpdated: res.UpdatedAt,
	}
}
