package quotation_service_v2

import (
	"strings"

	"github.com/tech/sendico/payments/quotation/internal/service/quotation/quote_response_mapper_v2"
	"github.com/tech/sendico/payments/storage/model"
	"github.com/tech/sendico/pkg/merrors"
	quotationv2 "github.com/tech/sendico/pkg/proto/payments/quotation/v2"
)

func (s *QuotationServiceV2) singleResultFromRecord(record *model.PaymentQuoteRecord) (*QuotePaymentResult, error) {
	if record == nil {
		return nil, merrors.InvalidArgument("record is required")
	}
	if record.RequestShape != model.QuoteRequestShapeSingle {
		return nil, merrors.InvalidArgument("record request shape is not single")
	}
	if len(record.Items) != 1 || record.Items[0] == nil {
		return nil, merrors.InvalidArgument("record single item is required")
	}
	item := record.Items[0]
	if item.Intent == nil {
		return nil, merrors.InvalidArgument("record intent is required")
	}
	if item.Quote == nil {
		return nil, merrors.InvalidArgument("record quote is required")
	}

	status := statusFromStored(item.Status)
	mapped, err := s.deps.ResponseMapper.Map(quote_response_mapper_v2.MapInput{
		Meta: quote_response_mapper_v2.QuoteMeta{
			ID:        record.GetID().Hex(),
			CreatedAt: record.CreatedAt,
			UpdatedAt: record.UpdatedAt,
		},
		Quote:  canonicalFromSnapshot(item.Quote, record.ExpiresAt, s.deps.Now().UTC(), strings.TrimSpace(record.QuoteRef)),
		Status: status,
	})
	if err != nil {
		return nil, err
	}
	mapped.Quote.IntentRef = strings.TrimSpace(item.Intent.Ref)
	return &QuotePaymentResult{
		Response: &quotationv2.QuotePaymentResponse{
			Quote:          mapped.Quote,
			IdempotencyKey: strings.TrimSpace(record.IdempotencyKey),
		},
		Record: record,
	}, nil
}

func (s *QuotationServiceV2) batchResultFromRecord(record *model.PaymentQuoteRecord) (*QuotePaymentsResult, error) {
	if record == nil {
		return nil, merrors.InvalidArgument("record is required")
	}
	if record.RequestShape != model.QuoteRequestShapeBatch {
		return nil, merrors.InvalidArgument("record request shape is not batch")
	}
	if len(record.Items) == 0 {
		return nil, merrors.InvalidArgument("record items are required")
	}

	quotes := make([]*quotationv2.PaymentQuote, 0, len(record.Items))
	for _, item := range record.Items {
		if item == nil {
			return nil, merrors.InvalidArgument("record item is required")
		}
		if item.Quote == nil {
			return nil, merrors.InvalidArgument("record item quote is required")
		}
		status := statusFromStored(item.Status)

		mapped, err := s.deps.ResponseMapper.Map(quote_response_mapper_v2.MapInput{
			Meta: quote_response_mapper_v2.QuoteMeta{
				ID:        record.GetID().Hex(),
				CreatedAt: record.CreatedAt,
				UpdatedAt: record.UpdatedAt,
			},
			Quote:  canonicalFromSnapshot(item.Quote, record.ExpiresAt, s.deps.Now().UTC(), strings.TrimSpace(record.QuoteRef)),
			Status: status,
		})
		if err != nil {
			return nil, err
		}
		if item.Intent != nil {
			mapped.Quote.IntentRef = strings.TrimSpace(item.Intent.Ref)
		}
		quotes = append(quotes, mapped.Quote)
	}

	return &QuotePaymentsResult{
		Response: &quotationv2.QuotePaymentsResponse{
			QuoteRef:       strings.TrimSpace(record.QuoteRef),
			Quotes:         quotes,
			IdempotencyKey: strings.TrimSpace(record.IdempotencyKey),
		},
		Record: record,
	}, nil
}
