package transfer

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

func TariffsToProductInformations(tarrifs []models.Tariff) []*discount.ProductInformation {
	productInformations := []*discount.ProductInformation{}

	for _, tariff := range tarrifs {
		for _, privilege := range tariff.Privileges {
			productInformations = append(productInformations, PrivilegeToProductInformation(privilege))
		}

		if tariff.Price != 0 {
        price := tariff.Price
				productInformations[len(productInformations) - 1].Usage = &price
		}
	}

	return productInformations
}

func PrivilegeToProductInformation(privilege models.Privilege) *discount.ProductInformation {
	return &discount.ProductInformation{
		ID:    privilege.PrivilegeID,
		Price: privilege.Price * privilege.Amount,
		Group: &privilege.ServiceKey,
		Term:  &privilege.Amount,
	}
}

func TariffMessageProtoToTariffModel(tariff *broker.TariffMessage) *models.Tariff {
	if tariff == nil {
		return &models.Tariff{}
	}

	return &models.Tariff{Privileges: PrivilegeArrayProtoToModel(tariff.Privileges)}
}

func TariffModelToProtoMessage(userID string, tariffModel *models.Tariff) *broker.TariffMessage {
	if tariffModel == nil {
		return &broker.TariffMessage{}
	}

	return &broker.TariffMessage{
		UserID:     userID,
		Privileges: PrivilegeArrayModelToProto(tariffModel.Privileges),
	}
}
