package grpc

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/domain/payment/entity"
	"git.gocasts.ir/ebhomengo/niki/domain/payment/service"
	paymentpb "git.gocasts.ir/ebhomengo/niki/paymentapp/protobuf"
)

type Handler struct {
	paymentpb.UnimplementedPaymentServiceServer
	scvPayment *service.PaymentService
}

func NewHandler(s *service.PaymentService) *Handler {
	return &Handler{
		scvPayment: s,
	}
}

func (h *Handler) InitiatePayment(ctx context.Context, req *paymentpb.InitiatePaymentRequest) (*paymentpb.InitiatePaymentResponse, error) {

	paymentResponse, err := h.scvPayment.InitiatePayment(ctx, service.InitiatePaymentRequest{
		UserID:      uint(req.GetUserId()),
		PayableType: entity.PayableType(req.GetPayableType()),
		PayableID:   uint(req.GetPayableId()),
		GatewayCode: req.GetGatewayCode(),
		CallbackURL: req.GetCallbackUrl(),
		Amount:      req.GetAmount(),
	})
	if err != nil {
		return nil, err
	}

	response := &paymentpb.InitiatePaymentResponse{
		PaymentId:   uint64(paymentResponse.PaymentID),
		RedirectUrl: paymentResponse.RedirectURL,
	}

	return response, nil
}
