package transfer

import (
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/broker"
)

func PrivilegeProtoToModel(privilege *broker.PrivilegeMessage) *models.Privilege {
	if privilege == nil {
		return &models.Privilege{}
	}

	return &models.Privilege{
		PrivilegeID: privilege.GetPrivilegeID(),
		ServiceKey:  privilege.GetServiceKey(),
		Type:        models.PrivilegeTypeMap[privilege.GetType()],
		Value:       privilege.GetValue(),
		Amount:      privilege.GetAmount(),
	}
}

func PrivilegeArrayProtoToModel(privileges []*broker.PrivilegeMessage) []models.Privilege {
	privilegesModel := make([]models.Privilege, len(privileges))

	for index, privilege := range privileges {
		privilegesModel[index] = *PrivilegeProtoToModel(privilege)
	}

	return privilegesModel
}

func PrivilegeModelToProto(privilege *models.Privilege) *broker.PrivilegeMessage {
	if privilege == nil {
		return &broker.PrivilegeMessage{}
	}

	return &broker.PrivilegeMessage{
		PrivilegeID: privilege.PrivilegeID,
		ServiceKey:  privilege.ServiceKey,
		Type:        models.PrivilegeBrokerTypeMap[privilege.Type],
		Value:       privilege.Value,
		Amount:      privilege.Amount,
	}
}

func PrivilegeArrayModelToProto(privileges []models.Privilege) []*broker.PrivilegeMessage {
	privilegesProto := make([]*broker.PrivilegeMessage, len(privileges))

	for index, privilege := range privileges {
		privilegeCopy := privilege
		privilegesProto[index] = PrivilegeModelToProto(&privilegeCopy)
	}

	return privilegesProto
}
