package gateway

import (
	"context"
	"errors"
	"os"
	"strings"
	"time"

	"github.com/tech/sendico/gateway/tgsettle/storage"
	storagemodel "github.com/tech/sendico/gateway/tgsettle/storage/model"
	"github.com/tech/sendico/pkg/api/routers"
	"github.com/tech/sendico/pkg/discovery"
	"github.com/tech/sendico/pkg/merrors"
	msg "github.com/tech/sendico/pkg/messaging"
	mb "github.com/tech/sendico/pkg/messaging/broker"
	cons "github.com/tech/sendico/pkg/messaging/consumer"
	confirmations "github.com/tech/sendico/pkg/messaging/notifications/confirmations"
	paymentgateway "github.com/tech/sendico/pkg/messaging/notifications/paymentgateway"
	np "github.com/tech/sendico/pkg/messaging/notifications/processor"
	tnotifications "github.com/tech/sendico/pkg/messaging/notifications/telegram"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/model"
	"github.com/tech/sendico/pkg/mservice"
	paymenttypes "github.com/tech/sendico/pkg/payments/types"
	moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
	connectorv1 "github.com/tech/sendico/pkg/proto/connector/v1"
	chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
	"github.com/tech/sendico/pkg/server/grpcapp"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/timestamppb"
)

const (
	defaultConfirmationTimeoutSeconds = 259200
	defaultTelegramSuccessReaction    = "\U0001FAE1"
)

const (
	metadataPaymentIntentID = "payment_intent_id"
	metadataQuoteRef        = "quote_ref"
	metadataTargetChatID    = "target_chat_id"
	metadataOutgoingLeg     = "outgoing_leg"
	metadataSourceAmount    = "source_amount"
	metadataSourceCurrency  = "source_currency"
)

type Config struct {
	Rail            string
	TargetChatIDEnv string
	TimeoutSeconds  int32
	AcceptedUserIDs []string
	SuccessReaction string
	InvokeURI       string
}

type Service struct {
	logger          mlogger.Logger
	repo            storage.Repository
	producer        msg.Producer
	broker          mb.Broker
	cfg             Config
	rail            string
	chatID          string
	announcer       *discovery.Announcer
	invokeURI       string
	successReaction string

	consumers []msg.Consumer

	connectorv1.UnimplementedConnectorServiceServer
}

func NewService(logger mlogger.Logger, repo storage.Repository, producer msg.Producer, broker mb.Broker, cfg Config) *Service {
	if logger == nil {
		logger = zap.NewNop()
	}
	logger = logger.Named("service")
	svc := &Service{
		logger:    logger,
		repo:      repo,
		producer:  producer,
		broker:    broker,
		cfg:       cfg,
		rail:      strings.TrimSpace(cfg.Rail),
		invokeURI: strings.TrimSpace(cfg.InvokeURI),
	}
	svc.chatID = strings.TrimSpace(readEnv(cfg.TargetChatIDEnv))
	svc.successReaction = strings.TrimSpace(cfg.SuccessReaction)
	if svc.successReaction == "" {
		svc.successReaction = defaultTelegramSuccessReaction
	}
	svc.startConsumers()
	svc.startAnnouncer()
	return svc
}

func (s *Service) Register(router routers.GRPC) error {
	return router.Register(func(reg grpc.ServiceRegistrar) {
		connectorv1.RegisterConnectorServiceServer(reg, s)
	})
}

func (s *Service) Shutdown() {
	if s == nil {
		return
	}
	if s.announcer != nil {
		s.announcer.Stop()
	}
	for _, consumer := range s.consumers {
		if consumer != nil {
			consumer.Close()
		}
	}
}

func (s *Service) startConsumers() {
	if s == nil || s.broker == nil {
		if s != nil && s.logger != nil {
			s.logger.Warn("Messaging broker not configured; confirmation flow disabled")
		}
		return
	}
	resultProcessor := confirmations.NewConfirmationResultProcessor(s.logger, string(mservice.PaymentGateway), s.rail, s.onConfirmationResult)
	s.consumeProcessor(resultProcessor)
}

func (s *Service) consumeProcessor(processor np.EnvelopeProcessor) {
	consumer, err := cons.NewConsumer(s.logger, s.broker, processor.GetSubject())
	if err != nil {
		s.logger.Warn("Failed to create messaging consumer", zap.Error(err), zap.String("event", processor.GetSubject().ToString()))
		return
	}
	s.consumers = append(s.consumers, consumer)
	go func() {
		if err := consumer.ConsumeMessages(processor.Process); err != nil && !errors.Is(err, context.Canceled) {
			s.logger.Warn("Messaging consumer stopped", zap.Error(err), zap.String("event", processor.GetSubject().ToString()))
		}
	}()
}

func (s *Service) SubmitTransfer(ctx context.Context, req *chainv1.SubmitTransferRequest) (*chainv1.SubmitTransferResponse, error) {
	if req == nil {
		s.logger.Warn("Submit transfer rejected", zap.String("reason", "request is required"))
		return nil, merrors.InvalidArgument("submit_transfer: request is required")
	}
	idempotencyKey := strings.TrimSpace(req.GetIdempotencyKey())
	if idempotencyKey == "" {
		s.logger.Warn("Submit transfer rejected", zap.String("reason", "idempotency_key is required"))
		return nil, merrors.InvalidArgument("submit_transfer: idempotency_key is required")
	}
	amount := req.GetAmount()
	if amount == nil || strings.TrimSpace(amount.GetAmount()) == "" || strings.TrimSpace(amount.GetCurrency()) == "" {
		s.logger.Warn("Submit transfer rejected", zap.String("reason", "amount is required"), zap.String("idempotency_key", idempotencyKey))
		return nil, merrors.InvalidArgument("submit_transfer: amount is required")
	}
	intent, err := intentFromSubmitTransfer(req, s.rail, s.chatID)
	if err != nil {
		s.logger.Warn("Submit transfer rejected", zap.Error(err), zap.String("idempotency_key", idempotencyKey))
		return nil, err
	}
	logFields := []zap.Field{
		zap.String("idempotency_key", intent.IdempotencyKey),
		zap.String("payment_intent_id", intent.PaymentIntentID),
		zap.String("quote_ref", intent.QuoteRef),
		zap.String("rail", intent.OutgoingLeg),
		zap.String("organization_ref", strings.TrimSpace(req.GetOrganizationRef())),
		zap.String("source_wallet_ref", strings.TrimSpace(req.GetSourceWalletRef())),
	}
	if intent.RequestedMoney != nil {
		logFields = append(logFields,
			zap.String("amount", strings.TrimSpace(intent.RequestedMoney.Amount)),
			zap.String("currency", strings.TrimSpace(intent.RequestedMoney.Currency)),
		)
	}
	logFields = append(logFields, transferDestinationLogFields(req.GetDestination())...)
	if s.repo == nil || s.repo.Payments() == nil {
		s.logger.Warn("Payment gateway storage unavailable", logFields...)
		return nil, merrors.Internal("payment gateway storage unavailable")
	}
	existing, err := s.repo.Payments().FindByIdempotencyKey(ctx, idempotencyKey)
	if err != nil {
		s.logger.Warn("Submit transfer lookup failed", append(logFields, zap.Error(err))...)
		return nil, err
	}
	if existing != nil {
		existing, err = s.expirePaymentIfNeeded(ctx, existing)
		if err != nil {
			s.logger.Warn("Submit transfer status refresh failed", append(logFields, zap.Error(err))...)
			return nil, err
		}
		s.logger.Info("Submit transfer idempotent hit", append(logFields, zap.String("status", string(paymentStatus(existing))))...)
		return &chainv1.SubmitTransferResponse{Transfer: transferFromPayment(existing, req)}, nil
	}
	if err := s.onIntent(ctx, intent); err != nil {
		s.logger.Warn("Submit transfer intent handling failed", append(logFields, zap.Error(err))...)
		return nil, err
	}
	s.logger.Info("Submit transfer accepted", logFields...)
	return &chainv1.SubmitTransferResponse{Transfer: transferFromRequest(req)}, nil
}

func (s *Service) GetTransfer(ctx context.Context, req *chainv1.GetTransferRequest) (*chainv1.GetTransferResponse, error) {
	if req == nil {
		s.logger.Warn("Get transfer rejected", zap.String("reason", "request is required"))
		return nil, merrors.InvalidArgument("get_transfer: request is required")
	}
	transferRef := strings.TrimSpace(req.GetTransferRef())
	if transferRef == "" {
		s.logger.Warn("Get transfer rejected", zap.String("reason", "transfer_ref is required"))
		return nil, merrors.InvalidArgument("get_transfer: transfer_ref is required")
	}
	logFields := []zap.Field{zap.String("transfer_ref", transferRef)}
	if s.repo == nil || s.repo.Payments() == nil {
		s.logger.Warn("Payment gateway storage unavailable", logFields...)
		return nil, merrors.Internal("payment gateway storage unavailable")
	}
	existing, err := s.repo.Payments().FindByIdempotencyKey(ctx, transferRef)
	if err != nil {
		s.logger.Warn("Get transfer lookup failed", append(logFields, zap.Error(err))...)
		return nil, err
	}
	if existing != nil {
		existing, err = s.expirePaymentIfNeeded(ctx, existing)
		if err != nil {
			s.logger.Warn("Get transfer status refresh failed", append(logFields, zap.Error(err))...)
			return nil, err
		}
		s.logger.Info("Get transfer resolved from execution", append(logFields,
			zap.String("payment_intent_id", strings.TrimSpace(existing.PaymentIntentID)),
			zap.String("status", string(paymentStatus(existing))),
		)...)
		return &chainv1.GetTransferResponse{Transfer: transferFromPayment(existing, nil)}, nil
	}
	s.logger.Warn("Get transfer not found", logFields...)
	return nil, status.Error(codes.NotFound, "transfer not found")
}

func (s *Service) onIntent(ctx context.Context, intent *model.PaymentGatewayIntent) error {
	if intent == nil {
		s.logger.Warn("Payment gateway intent rejected", zap.String("reason", "intent is nil"))
		return merrors.InvalidArgument("payment gateway intent is nil", "intent")
	}
	intent = normalizeIntent(intent)
	if intent.IdempotencyKey == "" {
		s.logger.Warn("Payment gateway intent rejected", zap.String("reason", "idempotency_key is required"))
		return merrors.InvalidArgument("idempotency_key is required", "idempotency_key")
	}
	if intent.PaymentIntentID == "" {
		s.logger.Warn("Payment gateway intent rejected", zap.String("reason", "payment_intent_id is required"), zap.String("idempotency_key", intent.IdempotencyKey))
		return merrors.InvalidArgument("payment_intent_id is required", "payment_intent_id")
	}
	if intent.RequestedMoney == nil || strings.TrimSpace(intent.RequestedMoney.Amount) == "" || strings.TrimSpace(intent.RequestedMoney.Currency) == "" {
		s.logger.Warn("Payment gateway intent rejected", zap.String("reason", "requested_money is required"), zap.String("idempotency_key", intent.IdempotencyKey))
		return merrors.InvalidArgument("requested_money is required", "requested_money")
	}
	if s.repo == nil || s.repo.Payments() == nil {
		s.logger.Warn("Payment gateway storage unavailable", zap.String("idempotency_key", intent.IdempotencyKey))
		return merrors.Internal("payment gateway storage unavailable")
	}

	confirmReq, err := s.buildConfirmationRequest(intent)
	if err != nil {
		s.logger.Warn("Failed to build confirmation request", zap.Error(err), zap.String("idempotency_key", intent.IdempotencyKey), zap.String("payment_intent_id", intent.PaymentIntentID))
		return err
	}

	existing, err := s.repo.Payments().FindByIdempotencyKey(ctx, confirmReq.RequestID)
	if err != nil {
		return err
	}
	if existing != nil {
		existing, err = s.expirePaymentIfNeeded(ctx, existing)
		if err != nil {
			return err
		}
		s.logger.Info("Payment gateway intent already recorded",
			zap.String("idempotency_key", confirmReq.RequestID),
			zap.String("payment_intent_id", confirmReq.PaymentIntentID),
			zap.String("quote_ref", confirmReq.QuoteRef),
			zap.String("rail", confirmReq.Rail),
			zap.String("status", string(paymentStatus(existing))))
		return nil
	}

	record := paymentRecordFromIntent(intent, confirmReq)
	if err := s.repo.Payments().Upsert(ctx, record); err != nil {
		s.logger.Warn("Failed to persist pending payment", zap.Error(err), zap.String("idempotency_key", confirmReq.RequestID))
		return err
	}
	if err := s.sendConfirmationRequest(confirmReq); err != nil {
		s.logger.Warn("Failed to publish confirmation request", zap.Error(err), zap.String("idempotency_key", confirmReq.RequestID))
		s.markPaymentExpired(ctx, record, time.Now())
		return err
	}
	return nil
}

func (s *Service) onConfirmationResult(ctx context.Context, result *model.ConfirmationResult) error {
	if result == nil {
		s.logger.Warn("Confirmation result rejected", zap.String("reason", "result is nil"))
		return merrors.InvalidArgument("confirmation result is nil", "result")
	}
	requestID := strings.TrimSpace(result.RequestID)
	if requestID == "" {
		s.logger.Warn("Confirmation result rejected", zap.String("reason", "request_id is required"))
		return merrors.InvalidArgument("confirmation request_id is required", "request_id")
	}
	record, err := s.loadPayment(ctx, requestID)
	if err != nil {
		s.logger.Warn("Confirmation result lookup failed", zap.Error(err), zap.String("request_id", requestID))
		return err
	}
	if record == nil {
		s.logger.Warn("Confirmation result ignored: payment not found", zap.String("request_id", requestID))
		return nil
	}

	if result.RawReply != nil && s.repo != nil && s.repo.TelegramConfirmations() != nil {
		if err := s.repo.TelegramConfirmations().Upsert(ctx, &storagemodel.TelegramConfirmation{
			RequestID:       requestID,
			PaymentIntentID: record.PaymentIntentID,
			QuoteRef:        record.QuoteRef,
			RawReply:        result.RawReply,
		}); err != nil {
			s.logger.Warn("Failed to store telegram confirmation", zap.Error(err), zap.String("request_id", requestID))
		} else {
			s.logger.Info("Stored telegram confirmation", zap.String("request_id", requestID),
				zap.String("payment_intent_id", record.PaymentIntentID),
				zap.String("reply_text", result.RawReply.Text), zap.String("reply_user_id", result.RawReply.FromUserID),
				zap.String("reply_user", result.RawReply.FromUsername))
		}
	}

	nextStatus := paymentStatusFromResult(result)
	currentStatus := paymentStatus(record)
	if currentStatus == storagemodel.PaymentStatusExecuted || currentStatus == storagemodel.PaymentStatusExpired {
		s.logger.Info("Confirmation result ignored: payment already finalized",
			zap.String("request_id", requestID),
			zap.String("status", string(currentStatus)))
		return nil
	}
	s.applyPaymentResult(record, nextStatus, result)
	if err := s.repo.Payments().Upsert(ctx, record); err != nil {
		s.logger.Warn("Failed to persist payment status", zap.Error(err), zap.String("request_id", requestID))
		return err
	}

	intent := intentFromPayment(record)
	s.publishExecution(intent, result)
	s.publishTelegramReaction(result)
	return nil
}

func (s *Service) buildConfirmationRequest(intent *model.PaymentGatewayIntent) (*model.ConfirmationRequest, error) {
	targetChatID := strings.TrimSpace(intent.TargetChatID)
	if targetChatID == "" {
		targetChatID = s.chatID
	}
	if targetChatID == "" {
		return nil, merrors.InvalidArgument("target_chat_id is required", "target_chat_id")
	}
	rail := strings.TrimSpace(intent.OutgoingLeg)
	if rail == "" {
		rail = s.rail
	}
	timeout := s.cfg.TimeoutSeconds
	if timeout <= 0 {
		timeout = int32(defaultConfirmationTimeoutSeconds)
	}
	return &model.ConfirmationRequest{
		RequestID:       intent.IdempotencyKey,
		TargetChatID:    targetChatID,
		RequestedMoney:  intent.RequestedMoney,
		PaymentIntentID: intent.PaymentIntentID,
		QuoteRef:        intent.QuoteRef,
		AcceptedUserIDs: s.cfg.AcceptedUserIDs,
		TimeoutSeconds:  timeout,
		SourceService:   string(mservice.PaymentGateway),
		Rail:            rail,
	}, nil
}

func (s *Service) sendConfirmationRequest(request *model.ConfirmationRequest) error {
	if request == nil {
		s.logger.Warn("Confirmation request rejected", zap.String("reason", "request is nil"))
		return merrors.InvalidArgument("confirmation request is nil", "request")
	}
	if s.producer == nil {
		s.logger.Warn("Messaging producer not configured")
		return merrors.Internal("messaging producer is not configured")
	}
	env := confirmations.ConfirmationRequest(string(mservice.PaymentGateway), request)
	if err := s.producer.SendMessage(env); err != nil {
		s.logger.Warn("Failed to publish confirmation request",
			zap.Error(err),
			zap.String("request_id", request.RequestID),
			zap.String("payment_intent_id", request.PaymentIntentID),
			zap.String("quote_ref", request.QuoteRef),
			zap.String("rail", request.Rail),
			zap.Int32("timeout_seconds", request.TimeoutSeconds))
		return err
	}
	s.logger.Info("Published confirmation request",
		zap.String("request_id", request.RequestID),
		zap.String("payment_intent_id", request.PaymentIntentID),
		zap.String("quote_ref", request.QuoteRef),
		zap.String("rail", request.Rail),
		zap.Int32("timeout_seconds", request.TimeoutSeconds))
	return nil
}

func (s *Service) publishExecution(intent *model.PaymentGatewayIntent, result *model.ConfirmationResult) {
	if s == nil || intent == nil || result == nil || s.producer == nil {
		return
	}
	exec := &model.PaymentGatewayExecution{
		PaymentIntentID: intent.PaymentIntentID,
		IdempotencyKey:  intent.IdempotencyKey,
		QuoteRef:        intent.QuoteRef,
		ExecutedMoney:   result.Money,
		Status:          result.Status,
		RequestID:       result.RequestID,
		RawReply:        result.RawReply,
	}
	env := paymentgateway.PaymentGatewayExecution(string(mservice.PaymentGateway), exec)
	if err := s.producer.SendMessage(env); err != nil {
		s.logger.Warn("Failed to publish gateway execution result",
			zap.Error(err),
			zap.String("request_id", result.RequestID),
			zap.String("idempotency_key", intent.IdempotencyKey),
			zap.String("payment_intent_id", intent.PaymentIntentID),
			zap.String("quote_ref", intent.QuoteRef),
			zap.String("status", string(result.Status)))
		return
	}
	s.logger.Info("Published gateway execution result",
		zap.String("request_id", result.RequestID),
		zap.String("idempotency_key", intent.IdempotencyKey),
		zap.String("payment_intent_id", intent.PaymentIntentID),
		zap.String("quote_ref", intent.QuoteRef),
		zap.String("status", string(result.Status)))
}

func (s *Service) publishTelegramReaction(result *model.ConfirmationResult) {
	if s == nil || s.producer == nil || result == nil || result.RawReply == nil {
		return
	}
	if result.Status != model.ConfirmationStatusConfirmed && result.Status != model.ConfirmationStatusClarified {
		return
	}
	request := &model.TelegramReactionRequest{
		RequestID: strings.TrimSpace(result.RequestID),
		ChatID:    strings.TrimSpace(result.RawReply.ChatID),
		MessageID: strings.TrimSpace(result.RawReply.MessageID),
		Emoji:     s.successReaction,
	}
	if request.ChatID == "" || request.MessageID == "" || request.Emoji == "" {
		return
	}
	env := tnotifications.TelegramReaction(string(mservice.PaymentGateway), request)
	if err := s.producer.SendMessage(env); err != nil {
		s.logger.Warn("Failed to publish telegram reaction",
			zap.Error(err),
			zap.String("request_id", request.RequestID),
			zap.String("chat_id", request.ChatID),
			zap.String("message_id", request.MessageID),
			zap.String("emoji", request.Emoji))
		return
	}
	s.logger.Info("Published telegram reaction",
		zap.String("request_id", request.RequestID),
		zap.String("chat_id", request.ChatID),
		zap.String("message_id", request.MessageID),
		zap.String("emoji", request.Emoji))
}

func (s *Service) loadPayment(ctx context.Context, requestID string) (*storagemodel.PaymentRecord, error) {
	if s == nil || s.repo == nil || s.repo.Payments() == nil {
		return nil, merrors.Internal("payment gateway storage unavailable")
	}
	requestID = strings.TrimSpace(requestID)
	if requestID == "" {
		return nil, merrors.InvalidArgument("request_id is required", "request_id")
	}
	return s.repo.Payments().FindByIdempotencyKey(ctx, requestID)
}

func (s *Service) expirePaymentIfNeeded(ctx context.Context, record *storagemodel.PaymentRecord) (*storagemodel.PaymentRecord, error) {
	if record == nil {
		return nil, nil
	}
	status := paymentStatus(record)
	if status != storagemodel.PaymentStatusPending {
		return record, nil
	}
	if record.ExpiresAt.IsZero() {
		return record, nil
	}
	if time.Now().Before(record.ExpiresAt) {
		return record, nil
	}
	record.Status = storagemodel.PaymentStatusExpired
	if record.ExpiredAt.IsZero() {
		record.ExpiredAt = time.Now()
	}
	if s != nil && s.repo != nil && s.repo.Payments() != nil {
		if err := s.repo.Payments().Upsert(ctx, record); err != nil {
			return record, err
		}
	}
	return record, nil
}

func (s *Service) markPaymentExpired(ctx context.Context, record *storagemodel.PaymentRecord, when time.Time) {
	if record == nil || s == nil || s.repo == nil || s.repo.Payments() == nil {
		return
	}
	if when.IsZero() {
		when = time.Now()
	}
	record.Status = storagemodel.PaymentStatusExpired
	record.ExpiredAt = when
	if err := s.repo.Payments().Upsert(ctx, record); err != nil {
		s.logger.Warn("Failed to mark payment as expired", zap.Error(err), zap.String("request_id", record.IdempotencyKey))
	}
}

func (s *Service) startAnnouncer() {
	if s == nil || s.producer == nil {
		return
	}
	caps := []string{"telegram_confirmation", "money_persistence", "observe.confirm", "payout.fiat"}
	if s.rail != "" {
		caps = append(caps, "confirmations."+strings.ToLower(string(mservice.PaymentGateway))+"."+strings.ToLower(s.rail))
	}
	announce := discovery.Announcement{
		Service:    string(mservice.PaymentGateway),
		Rail:       s.rail,
		Operations: caps,
		InvokeURI:  s.invokeURI,
	}
	s.announcer = discovery.NewAnnouncer(s.logger, s.producer, string(mservice.PaymentGateway), announce)
	s.announcer.Start()
}

func normalizeIntent(intent *model.PaymentGatewayIntent) *model.PaymentGatewayIntent {
	if intent == nil {
		return nil
	}
	cp := *intent
	cp.PaymentIntentID = strings.TrimSpace(cp.PaymentIntentID)
	cp.IdempotencyKey = strings.TrimSpace(cp.IdempotencyKey)
	cp.OutgoingLeg = strings.TrimSpace(cp.OutgoingLeg)
	cp.QuoteRef = strings.TrimSpace(cp.QuoteRef)
	cp.TargetChatID = strings.TrimSpace(cp.TargetChatID)
	if cp.RequestedMoney != nil {
		cp.RequestedMoney.Amount = strings.TrimSpace(cp.RequestedMoney.Amount)
		cp.RequestedMoney.Currency = strings.TrimSpace(cp.RequestedMoney.Currency)
	}
	return &cp
}

func paymentStatus(record *storagemodel.PaymentRecord) storagemodel.PaymentStatus {
	if record == nil {
		return storagemodel.PaymentStatusPending
	}
	if record.Status != "" {
		return record.Status
	}
	if record.ExecutedMoney != nil || !record.ExecutedAt.IsZero() {
		return storagemodel.PaymentStatusExecuted
	}
	return storagemodel.PaymentStatusPending
}

func paymentStatusFromResult(result *model.ConfirmationResult) storagemodel.PaymentStatus {
	if result == nil {
		return storagemodel.PaymentStatusPending
	}
	switch result.Status {
	case model.ConfirmationStatusConfirmed, model.ConfirmationStatusClarified:
		return storagemodel.PaymentStatusExecuted
	case model.ConfirmationStatusTimeout, model.ConfirmationStatusRejected:
		return storagemodel.PaymentStatusExpired
	default:
		return storagemodel.PaymentStatusPending
	}
}

func (s *Service) applyPaymentResult(record *storagemodel.PaymentRecord, status storagemodel.PaymentStatus, result *model.ConfirmationResult) {
	if record == nil {
		return
	}
	record.Status = status
	switch status {
	case storagemodel.PaymentStatusExecuted:
		record.ExecutedMoney = result.Money
		if record.ExecutedAt.IsZero() {
			record.ExecutedAt = time.Now()
		}
	case storagemodel.PaymentStatusExpired:
		if record.ExpiredAt.IsZero() {
			record.ExpiredAt = time.Now()
		}
	}
}

func paymentRecordFromIntent(intent *model.PaymentGatewayIntent, confirmReq *model.ConfirmationRequest) *storagemodel.PaymentRecord {
	record := &storagemodel.PaymentRecord{
		Status: storagemodel.PaymentStatusPending,
	}
	if intent != nil {
		record.IdempotencyKey = strings.TrimSpace(intent.IdempotencyKey)
		record.PaymentIntentID = strings.TrimSpace(intent.PaymentIntentID)
		record.QuoteRef = strings.TrimSpace(intent.QuoteRef)
		record.OutgoingLeg = strings.TrimSpace(intent.OutgoingLeg)
		record.TargetChatID = strings.TrimSpace(intent.TargetChatID)
		record.RequestedMoney = intent.RequestedMoney
	}
	if confirmReq != nil {
		record.IdempotencyKey = strings.TrimSpace(confirmReq.RequestID)
		record.PaymentIntentID = strings.TrimSpace(confirmReq.PaymentIntentID)
		record.QuoteRef = strings.TrimSpace(confirmReq.QuoteRef)
		record.OutgoingLeg = strings.TrimSpace(confirmReq.Rail)
		record.TargetChatID = strings.TrimSpace(confirmReq.TargetChatID)
		record.RequestedMoney = confirmReq.RequestedMoney
		if confirmReq.TimeoutSeconds > 0 {
			record.ExpiresAt = time.Now().Add(time.Duration(confirmReq.TimeoutSeconds) * time.Second)
		}
	}
	return record
}

func intentFromPayment(record *storagemodel.PaymentRecord) *model.PaymentGatewayIntent {
	if record == nil {
		return nil
	}
	return &model.PaymentGatewayIntent{
		PaymentIntentID: strings.TrimSpace(record.PaymentIntentID),
		IdempotencyKey:  strings.TrimSpace(record.IdempotencyKey),
		OutgoingLeg:     strings.TrimSpace(record.OutgoingLeg),
		QuoteRef:        strings.TrimSpace(record.QuoteRef),
		RequestedMoney:  record.RequestedMoney,
		TargetChatID:    strings.TrimSpace(record.TargetChatID),
	}
}

func intentFromSubmitTransfer(req *chainv1.SubmitTransferRequest, defaultRail, defaultChatID string) (*model.PaymentGatewayIntent, error) {
	if req == nil {
		return nil, merrors.InvalidArgument("submit_transfer: request is required")
	}
	idempotencyKey := strings.TrimSpace(req.GetIdempotencyKey())
	if idempotencyKey == "" {
		return nil, merrors.InvalidArgument("submit_transfer: idempotency_key is required")
	}
	amount := req.GetAmount()
	if amount == nil {
		return nil, merrors.InvalidArgument("submit_transfer: amount is required")
	}
	metadata := req.GetMetadata()
	requestedMoney := &paymenttypes.Money{
		Amount:   strings.TrimSpace(amount.GetAmount()),
		Currency: strings.TrimSpace(amount.GetCurrency()),
	}
	if requestedMoney.Amount == "" || requestedMoney.Currency == "" {
		return nil, merrors.InvalidArgument("submit_transfer: amount is required")
	}
	sourceAmount := strings.TrimSpace(metadata[metadataSourceAmount])
	sourceCurrency := strings.TrimSpace(metadata[metadataSourceCurrency])
	if sourceAmount != "" && sourceCurrency != "" {
		requestedMoney = &paymenttypes.Money{
			Amount:   sourceAmount,
			Currency: sourceCurrency,
		}
	}
	paymentIntentID := strings.TrimSpace(req.GetClientReference())
	if paymentIntentID == "" {
		paymentIntentID = strings.TrimSpace(metadata[metadataPaymentIntentID])
	}
	if paymentIntentID == "" {
		return nil, merrors.InvalidArgument("submit_transfer: payment_intent_id is required")
	}
	quoteRef := strings.TrimSpace(metadata[metadataQuoteRef])
	targetChatID := strings.TrimSpace(metadata[metadataTargetChatID])
	outgoingLeg := strings.TrimSpace(metadata[metadataOutgoingLeg])
	if outgoingLeg == "" {
		outgoingLeg = strings.TrimSpace(defaultRail)
	}
	if targetChatID == "" {
		targetChatID = strings.TrimSpace(defaultChatID)
	}
	return &model.PaymentGatewayIntent{
		PaymentIntentID: paymentIntentID,
		IdempotencyKey:  idempotencyKey,
		OutgoingLeg:     outgoingLeg,
		QuoteRef:        quoteRef,
		RequestedMoney:  requestedMoney,
		TargetChatID:    targetChatID,
	}, nil
}

func transferFromRequest(req *chainv1.SubmitTransferRequest) *chainv1.Transfer {
	if req == nil {
		return nil
	}
	amount := req.GetAmount()
	return &chainv1.Transfer{
		TransferRef:     strings.TrimSpace(req.GetIdempotencyKey()),
		IdempotencyKey:  strings.TrimSpace(req.GetIdempotencyKey()),
		OrganizationRef: strings.TrimSpace(req.GetOrganizationRef()),
		SourceWalletRef: strings.TrimSpace(req.GetSourceWalletRef()),
		Destination:     req.GetDestination(),
		RequestedAmount: amount,
		Status:          chainv1.TransferStatus_TRANSFER_SUBMITTED,
	}
}

func transferFromPayment(record *storagemodel.PaymentRecord, req *chainv1.SubmitTransferRequest) *chainv1.Transfer {
	if record == nil {
		return nil
	}
	var requested *moneyv1.Money
	if req != nil && req.GetAmount() != nil {
		requested = req.GetAmount()
	} else {
		requested = moneyFromPayment(record.RequestedMoney)
	}
	net := moneyFromPayment(record.ExecutedMoney)
	status := chainv1.TransferStatus_TRANSFER_SUBMITTED
	switch paymentStatus(record) {
	case storagemodel.PaymentStatusExecuted:
		status = chainv1.TransferStatus_TRANSFER_CONFIRMED
	case storagemodel.PaymentStatusExpired:
		status = chainv1.TransferStatus_TRANSFER_CANCELLED
	}
	transfer := &chainv1.Transfer{
		TransferRef:     strings.TrimSpace(record.IdempotencyKey),
		IdempotencyKey:  strings.TrimSpace(record.IdempotencyKey),
		RequestedAmount: requested,
		NetAmount:       net,
		Status:          status,
	}
	if req != nil {
		transfer.OrganizationRef = strings.TrimSpace(req.GetOrganizationRef())
		transfer.SourceWalletRef = strings.TrimSpace(req.GetSourceWalletRef())
		transfer.Destination = req.GetDestination()
	}
	if !record.ExecutedAt.IsZero() {
		ts := timestamppb.New(record.ExecutedAt)
		transfer.CreatedAt = ts
		transfer.UpdatedAt = ts
	} else if !record.UpdatedAt.IsZero() {
		ts := timestamppb.New(record.UpdatedAt)
		transfer.UpdatedAt = ts
		if !record.CreatedAt.IsZero() {
			transfer.CreatedAt = timestamppb.New(record.CreatedAt)
		}
	}
	return transfer
}

func moneyFromPayment(m *paymenttypes.Money) *moneyv1.Money {
	if m == nil {
		return nil
	}
	currency := strings.TrimSpace(m.Currency)
	amount := strings.TrimSpace(m.Amount)
	if currency == "" || amount == "" {
		return nil
	}
	return &moneyv1.Money{
		Currency: currency,
		Amount:   amount,
	}
}

func readEnv(env string) string {
	if strings.TrimSpace(env) == "" {
		return ""
	}
	return strings.TrimSpace(os.Getenv(env))
}

var _ grpcapp.Service = (*Service)(nil)
