package util

import (
	"encoding/json"
	"github.com/go-resty/resty/v2"
)

const (
	SUCCESS = "success"
)

type SMS struct {
	Appid     string
	Signature string
}

func NewSMS(appid, signature string) *SMS {
	return &SMS{
		Appid:     appid,
		Signature: signature,
	}
}

type SendRes struct {
	Status  string `json:"status"`
	Send_id string `json:"send_id"`
	Fee     int    `json:"fee"`
	Msg     string `json:"msg"`
	Code    int    `json:"code"`
}

// Send 短信发送
func (t *SMS) Send(to, content string) (SendRes, error) {
	client := resty.New()
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(map[string]string{
			"appid":     t.Appid,
			"signature": t.Signature,
			"to":        to,
			"content":   content,
		}).
		SetResult(&SendRes{}).
		Post("https://api-v4.mysubmail.com/sms/send.json")

	if err != nil {
		return SendRes{}, err
	}

	temp := SendRes{}
	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
		return SendRes{}, err
	}
	return temp, nil
}
