package cmb

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-kratos/kratos/v2/log"
	"time"
	err2 "voucher/api/err"
	v1 "voucher/api/v1"
	"voucher/internal/biz/bo"
	"voucher/internal/biz/vo"
)

func (v *Cmb) Notify(ctx context.Context, order *bo.OrderBo) (*bo.OrderNotifyBo, error) {

	if !order.Status.IsCanNotify() {
		return nil, fmt.Errorf("订单状态不允许通知,orderNo:%s,orderStatusText:%s", order.OrderNo, order.Status.GetText())
	}

	event, err := order.Status.GetOrderNotifyEvent()
	if err != nil {
		return nil, err
	}

	req := &bo.OrderNotifyBo{
		OrderNo:   order.OrderNo,
		NotifyUrl: order.NotifyUrl,
		Channel:   order.Channel,
		Event:     event,
		Type:      order.Type,
	}

	request, orderNotify, err := v.notifyCreate(ctx, order, req)
	if err != nil {
		return nil, err
	}

	reply, err := v.CmbMixRepo.Request(ctx, request, order.NotifyUrl)
	if err != nil {
		return orderNotify, v.notifyFail(ctx, orderNotify.ID, err.Error())
	}

	if err = v.CmbMixRepo.VerifyResponse(ctx, reply); err != nil {
		log.Errorf("回调通知招行返回验证结果发生错误,rep:%+v error:%s", reply, err.Error())
		return orderNotify, v.notifyFail(ctx, orderNotify.ID, err.Error())
	}

	if reply.RespCode == vo.CmbResponseStatusSuccess.GetValue() {

		replyJson, _ := json.Marshal(reply)
		return orderNotify, v.notifySuccess(ctx, orderNotify.ID, string(replyJson))
	}

	return orderNotify, v.notifyFail(ctx, orderNotify.ID, reply.RespMsg)
}

func (v *Cmb) bizContent(_ context.Context, order *bo.OrderBo, orderNotify *bo.OrderNotifyBo) (string, error) {

	cmbStatus, err := orderNotify.Event.GetCmbStatusText()
	if err != nil {
		return "", err
	}

	req := &v1.CmbNotifyRequest{
		Ticket:        orderNotify.OrderNo,
		Status:        cmbStatus.GetValue(),
		TransDate:     "", // 格式yyyy-mm-dd hh:mm:ss.sss
		OrgNo:         v.bc.Cmb.OrgNo,
		Attach:        order.Attach,
		Ext:           "",
		TransactionId: order.OutBizNo, // 招行订单号
	}

	if cmbStatus == vo.CmbStatusUse {
		if order.LastUseTime == nil || order.LastUseTime.IsZero() {
			req.TransDate = time.Now().Format("2006-01-02 15:04:05.000")
		} else {
			req.TransDate = order.LastUseTime.Format("2006-01-02 15:04:05.000")
		}
	} else {
		req.TransDate = time.Now().Format("2006-01-02 15:04:05.000")
	}

	bizJsonBytes, err := json.Marshal(req)
	if err != nil {
		return "", err
	}

	return string(bizJsonBytes), nil
}

func (v *Cmb) NotifyRequest(ctx context.Context, order *bo.OrderBo, orderNotify *bo.OrderNotifyBo) (*v1.CmbRequest, error) {

	bizContent, err := v.bizContent(ctx, order, orderNotify)
	if err != nil {
		return nil, err
	}

	request, err := v.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
		FuncName:   vo.CmbNotifyFuncName,
		BizContent: bizContent,
	})
	if err != nil {
		return nil, err
	}

	return request, nil
}

func (v *Cmb) notifyCreate(ctx context.Context, order *bo.OrderBo, req *bo.OrderNotifyBo) (*v1.CmbRequest, *bo.OrderNotifyBo, error) {

	request, err := v.NotifyRequest(ctx, order, req)
	if err != nil {
		return nil, nil, err
	}

	requestBytes, err := json.Marshal(request)
	if err != nil {
		return nil, nil, err
	}

	req.Request = string(requestBytes)

	orderNotify, err := v.OrderNotifyRepo.Create(ctx, req)
	if err != nil {
		return nil, nil, err
	}

	return request, orderNotify, err
}

func (v *Cmb) notifySuccess(ctx context.Context, notifyId uint64, responses string) error {

	if len(responses) == 0 {
		responses = "{}"
	}

	return v.OrderNotifyRepo.Success(ctx, notifyId, responses)
}

func (v *Cmb) notifyFail(ctx context.Context, notifyId uint64, errMsg string) error {

	if err := v.OrderNotifyRepo.Fail(ctx, notifyId, errMsg); err != nil {
		return err
	}

	return err2.ErrorNeedRetryNotify(errMsg)
}
