package dispute

import (
	"context"
	"errors"

	"gorm.io/gorm"
)

type Repository struct {
	db            *gorm.DB
	refundStarter RefundStarter
}

// RefundStarter 由支付模块适配实现，避免 dispute 直接依赖 payment。
type RefundStarter interface {
	StartRefund(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (status string, err error)
}

type RefundStarterFunc func(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (status string, err error)

func (fn RefundStarterFunc) StartRefund(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (string, error) {
	return fn(ctx, orderID, refundAmountCent, bizType, remark)
}

type Dependencies struct {
	RefundStarter RefundStarter
}

type refundAction struct {
	OrderID          uint64
	RefundAmountCent int64
	BizType          string
	Remark           string
}

func NewRepository(db *gorm.DB, deps ...Dependencies) *Repository {
	repo := &Repository{db: db}
	if len(deps) > 0 {
		repo.refundStarter = deps[0].RefundStarter
	}
	return repo
}

func IsNotFound(err error) bool {
	return errors.Is(err, gorm.ErrRecordNotFound)
}
