package smsservice

import (
	"fmt"
	statuscode "go-admin/common/status_code"
	"go-admin/config"
	"io"
	"net/http"
	"strings"

	"github.com/bytedance/sonic"
	"github.com/go-admin-team/go-admin-core/logger"
)

type InnopaasService struct {
}

func (i *InnopaasService) SendSMS(area, phoneNumber string, message string) int {
	// Implement the logic to send SMS using Innopass API
	params := map[string]string{
		"account":  config.ExtConfig.InnoPaas.ApiKey,
		"password": config.ExtConfig.InnoPaas.Password,
		"mobile":   area + phoneNumber,
		"msg":      message,
	}
	payload, _ := sonic.MarshalString(params) // strings.NewReader("{\"account\":\"I7145744\",\"password\":\"password\",\"mobile\":\"0012012074149,0012012074142\",\"msg\":\"Your verification code is 8888\"}")

	req, _ := http.NewRequest("POST", config.ExtConfig.InnoPaas.Url, strings.NewReader(payload))

	req.Header.Add("accept", "application/json")
	req.Header.Add("content-type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, err := io.ReadAll(res.Body)
	if err != nil {
		logger.Error("读取响应体失败:", err)
		fmt.Printf("响应体: %s", string(body))
		return statuscode.CaptchaFailInSend
		//return fmt.Errorf("读取响应体失败: %v", err)
	}
	return statuscode.OK
}
