package notify

import (
	"context"
	"fmt"

	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/gclient"
	"github.com/gogf/gf/v2/os/glog"

	"kami/utility/config"
)

type Feishu struct {
	WebHook string
	gclient.Client
}
type Response struct {
	Code int      `json:"code"`
	Msg  string   `json:"msg"`
	Data struct{} `json:"data"`
}

func (f *Feishu) SendMessage(ctx context.Context, title, message string) error {
	notifyCfg, err := config.NewConfig(ctx).GetNotification()
	if err != nil {
		return err
	}
	res := f.PostContent(ctx, notifyCfg.WebHook, g.Map{
		"msg_type": "text",
		"content":  g.Map{"text": title + "\n\n" + message},
	})
	var response Response
	err = gjson.Unmarshal([]byte(res), response)
	if err != nil {
		glog.Error(ctx, err)
	}
	if response.Code != 0 {
		glog.Error(ctx, fmt.Sprintf("飞书返回数据错误：%s", res))
	}
	return nil
}

func (f *Feishu) SendMessageWithImage(ctx context.Context, title, message, imagePath string) error {
	// TODO implement me
	panic("implement me")
}

func (f *Feishu) SendImage(ctx context.Context, imagePath string) error {
	return nil
}
