package mixrepoimpl

import (
	"context"
	"fmt"
	"voucher/internal/biz/mixrepos"
	"voucher/internal/conf"
	"voucher/internal/pkg/ding"
)

type DingMixRepoImpl struct {
	bc *conf.Bootstrap

	client *ding.TalkClient
}

func NewDingMixRepoImpl(bc *conf.Bootstrap) mixrepos.DingMixRepo {
	client := ding.NewDingTalkClient(bc.Alarm.WebhookURL, bc.Alarm.Secret)

	return &DingMixRepoImpl{bc: bc, client: client}
}

func (s *DingMixRepoImpl) SendMessage(_ context.Context, title, text string) error {

	if err := s.client.SendMarkdownMessage(title, text, s.bc.Alarm.WarningMobiles, false); err != nil {
		return fmt.Errorf("markdown 消息发送失败: %v", err)
	}

	return nil
}

func (s *DingMixRepoImpl) SendMarkdownMessage(_ context.Context, title, text string) error {

	isAtAll := false
	if len(s.bc.Alarm.AtMobiles) == 0 {
		isAtAll = true
	}

	if err := s.client.SendMarkdownMessage(title, text, s.bc.Alarm.AtMobiles, isAtAll); err != nil {
		return fmt.Errorf("markdown 消息发送失败: %v", err)
	}

	return nil
}
