package ebaiapi

import "encoding/json"

// ApplyCompensation 饿了么订单索赔
func (a *API) ApplyCompensation(orderID string, detailList []*ApplyCompensationList) ([]*ApplyCompensationStatus, error) {
	result, err := a.AccessAPI("order.apply.compensation", map[string]interface{}{
		"order_id":    orderID,
		"detail_list": detailList,
	})
	if err != nil {
		return nil, err
	}
	if result.ErrNo != 0 {
		return nil, err
	}

	byteDate, err := json.Marshal(result.Data)
	if err != nil {
		return nil, err
	}
	applyCompensationResultList := &ApplyCompensationResultList{}
	if err := json.Unmarshal(byteDate, applyCompensationResultList); err != nil {
		return nil, err
	}

	return applyCompensationResultList.ResultList, nil
}

// ApplyCompensationList 请求参数
type ApplyCompensationList struct {
	RefundId           string `json:"refund_id"`           // 逆向单id
	CompensationReason string `json:"compensation_reason"` // 申请索赔原因code
	Description        string `json:"description"`         // 申请索赔描述
}

// ApplyCompensationResultList 返回参数
type ApplyCompensationResultList struct {
	OrderId    string                     `json:"order_id"`
	ResultList []*ApplyCompensationStatus `json:"result_list"`
}
type ApplyCompensationStatus struct {
	RefundId               string `json:"refund_id"`                // 逆向单id
	CompensationStatusCode string `json:"compensation_status_code"` // 索赔状态编码
	CompensationStatusDesc string `json:"compensation_status_desc"` // 索赔状态描述
	CompensationFailCode   string `json:"compensation_fail_code"`   // 索赔失败code
	CompensationFailDesc   string `json:"compensation_fail_desc"`   // 索赔失败描述
}

const (
	CompensationCodeE1 = "E1" // 长时间无骑手接单，导致订单取消
	CompensationCodeE2 = "E2" // 长时间无骑手接单，导致订单取消
	CompensationCodeE3 = "E3" // 骑手接单后未完成配送，导致订单取消
	CompensationCodeE4 = "E4" // 配送时间过长导致订单被取消或退单
	CompensationCodeE5 = "E5" // 配送时间过长导致订单被取消或退单
	CompensationCodeE6 = "E6" // 骑手虚假完成配送，导致用户退单
	CompensationCodeE7 = "E7" // 骑手导致外卖破损或错送漏送
	CompensationCodeE8 = "E8" // 骑手态度差
	CompensationCodeE9 = "E9" // 配送超时或者异常，导致订单取消
)
