package swagger

import (
	"context"

	"go.uber.org/zap"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
	utils "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils/transfer"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/treasurer"
)

func (api *API2) GetPaymentLink(ctx context.Context, request *models.GetPaymentLinkRequest) (string, errors.Error) {
	if _, userErr := api.clients.auth.GetUser(ctx, request.UserID); userErr != nil {
		api.logger.Error("failed to get user on <GetPaymentLink> on <PaymentService>",
			zap.Error(userErr),
			zap.String("userID", request.UserID),
		)

		return "", userErr
	}

	switch request.Body.Type {
	case models.PaymentTypeBankCard:
		return api.GetPaymentLinkBankCard(ctx, request)
	case models.PaymentTypeYoomoney:
		return api.GetPaymentLinkYooMoney(ctx, request)
	case models.PaymentTypeSberPay:
		return api.GetPaymentLinkSberPay(ctx, request)
	case models.PaymentTypeTinkoff:
		return api.GetPaymentLinkTinkoff(ctx, request)
	case models.PaymentTypeSBP:
		return api.GetPaymentLinkSBP(ctx, request)
	}

	return "", errors.NewWithMessage("invalid payment method type", errors.ErrInvalidArgs)
}

func (api *API2) GetPaymentLinkBankCard(ctx context.Context, request *models.GetPaymentLinkRequest) (string, errors.Error) {
	link, err := api.clients.payment.GetPaymentLinkBankCard(ctx, &treasurer.GetPaymentLinkRequest{
		MainSettings: &treasurer.MainPaymentSettings{
			Currency:         request.Body.Currency,
			Amount:           request.Body.Amount,
			UserID:           request.UserID,
			ClientIP:         request.ClientIP,
			CallbackHostGRPC: []string{api.grpc.Domen},
			ReturnURL:        request.Body.ReturnURL,
			Customer: &treasurer.Customer{
				FullName: request.Customer.FullName,
				INN: request.Customer.INN,
				Email: request.Customer.Email,
				Phone: request.Customer.Phone,
			},
			Items: utils.ReceiptItemsToProto(request.Items),
		},
	})
	if err != nil {
		api.logger.Error("failed to get bankcard payment link on <GetPaymentLinkBankCard> of <PaymentService>", zap.Error(err))
		return "", err
	}

	return link, nil
}

func (api *API2) GetPaymentLinkYooMoney(ctx context.Context, request *models.GetPaymentLinkRequest) (string, errors.Error) {
	link, err := api.clients.payment.GetPaymentLinkYooMoney(ctx, &treasurer.GetPaymentLinkRequest{
		MainSettings: &treasurer.MainPaymentSettings{
			Currency:         request.Body.Currency,
			Amount:           request.Body.Amount,
			UserID:           request.UserID,
			ClientIP:         request.ClientIP,
			CallbackHostGRPC: []string{api.grpc.Domen},
			ReturnURL:        request.Body.ReturnURL,
			Customer: &treasurer.Customer{
				FullName: request.Customer.FullName,
				INN: request.Customer.INN,
				Email: request.Customer.Email,
				Phone: request.Customer.Phone,
			},
			Items: utils.ReceiptItemsToProto(request.Items),
		},
	})
	if err != nil {
		api.logger.Error("failed to get yoomoney payment link on <GetPaymentLinkYooMoney> of <PaymentService>", zap.Error(err))
		return "", err
	}

	return link, nil
}

func (api *API2) GetPaymentLinkSberPay(ctx context.Context, request *models.GetPaymentLinkRequest) (string, errors.Error) {
	link, err := api.clients.payment.GetPaymentLinkSberPay(ctx, &treasurer.GetPaymentLinkRequest{
		MainSettings: &treasurer.MainPaymentSettings{
			Currency:         request.Body.Currency,
			Amount:           request.Body.Amount,
			UserID:           request.UserID,
			ClientIP:         request.ClientIP,
			CallbackHostGRPC: []string{api.grpc.Domen},
			ReturnURL:        request.Body.ReturnURL,
			Customer: &treasurer.Customer{
				FullName: request.Customer.FullName,
				INN: request.Customer.INN,
				Email: request.Customer.Email,
				Phone: request.Customer.Phone,
			},
			Items: utils.ReceiptItemsToProto(request.Items),
		},
	})
	if err != nil {
		api.logger.Error("failed to get sberpay payment link on <GetPaymentLinkSberPay> of <PaymentService>", zap.Error(err))
		return "", err
	}

	return link, nil
}

func (api *API2) GetPaymentLinkSberB2B(ctx context.Context, request *models.GetPaymentLinkRequest) (string, errors.Error) {
	link, err := api.clients.payment.GetPaymentLinkSberbankB2B(ctx, &treasurer.GetPaymentLinkRequest{
		MainSettings: &treasurer.MainPaymentSettings{
			Currency:         request.Body.Currency,
			Amount:           request.Body.Amount,
			UserID:           request.UserID,
			ClientIP:         request.ClientIP,
			CallbackHostGRPC: []string{api.grpc.Domen},
			ReturnURL:        request.Body.ReturnURL,
			Customer: &treasurer.Customer{
				FullName: request.Customer.FullName,
				INN: request.Customer.INN,
				Email: request.Customer.Email,
				Phone: request.Customer.Phone,
			},
			Items: utils.ReceiptItemsToProto(request.Items),
		},
	})
	if err != nil {
		api.logger.Error("failed to get sberpay payment link on <GetPaymentLinkSberPay> of <PaymentService>", zap.Error(err))
		return "", err
	}

	return link, nil
}

func (api *API2) GetPaymentLinkSBP(ctx context.Context, request *models.GetPaymentLinkRequest) (string, errors.Error) {
	link, err := api.clients.payment.GetPaymentLinkSBP(ctx, &treasurer.GetPaymentLinkRequest{
		MainSettings: &treasurer.MainPaymentSettings{
			Currency:         request.Body.Currency,
			Amount:           request.Body.Amount,
			UserID:           request.UserID,
			ClientIP:         request.ClientIP,
			CallbackHostGRPC: []string{api.grpc.Domen},
			ReturnURL:        request.Body.ReturnURL,
			Customer: &treasurer.Customer{
				FullName: request.Customer.FullName,
				INN: request.Customer.INN,
				Email: request.Customer.Email,
				Phone: request.Customer.Phone,
			},
			Items: utils.ReceiptItemsToProto(request.Items),
		},
	})
	if err != nil {
		api.logger.Error("failed to get sberpay payment link on <GetPaymentLinkSberPay> of <PaymentService>", zap.Error(err))
		return "", err
	}

	return link, nil
}

func (api *API2) GetPaymentLinkTinkoff(ctx context.Context, request *models.GetPaymentLinkRequest) (string, errors.Error) {
	link, err := api.clients.payment.GetPaymentLinkTinkoff(ctx, &treasurer.GetPaymentLinkRequest{
		MainSettings: &treasurer.MainPaymentSettings{
			Currency:         request.Body.Currency,
			Amount:           request.Body.Amount,
			UserID:           request.UserID,
			ClientIP:         request.ClientIP,
			CallbackHostGRPC: []string{api.grpc.Domen},
			ReturnURL:        request.Body.ReturnURL,
			Customer: &treasurer.Customer{
				FullName: request.Customer.FullName,
				INN: request.Customer.INN,
				Email: request.Customer.Email,
				Phone: request.Customer.Phone,
			},
			Items: utils.ReceiptItemsToProto(request.Items),
		},
	})
	if err != nil {
		api.logger.Error("failed to get tinkoff payment link on <GetPaymentLinkTinkoff> of <PaymentService>", zap.Error(err))
		return "", err
	}

	return link, nil
}
