package order

import (
	"context"
	"log"

	"hfb_sys/backend/internal/model"
)

func (r *Repository) prepareRefund(order *model.RentalOrder, amountCent int64, bizType string, remark string) (*refundAction, error) {
	if amountCent <= 0 {
		return nil, nil
	}
	if r.refundStarter == nil {
		return nil, ErrDependencyUnavailable
	}
	order.RefundStatus = refundStatusPending
	order.RefundAmountCent = amountCent
	order.RefundedAt = nil
	return &refundAction{
		OrderID:          order.ID,
		RefundAmountCent: amountCent,
		BizType:          bizType,
		Remark:           remark,
	}, nil
}

func (r *Repository) startRefundBestEffort(ctx context.Context, action *refundAction) {
	if action == nil || r.refundStarter == nil {
		return
	}
	if _, err := r.refundStarter.StartRefund(ctx, action.OrderID, action.RefundAmountCent, action.BizType, action.Remark); err != nil {
		log.Printf("[order] start refund failed order_id=%d biz_type=%s amount_cent=%d err=%v", action.OrderID, action.BizType, action.RefundAmountCent, err)
	}
}
