//go:build integration

package test

import (
	"context"
	"log"
	"testing"
	"time"

	pb "bsm/full/module/base/sender/pb"
	"git.apinb.com/bsm-sdk/core/utils"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/metadata"
)

func TestMail(t *testing.T) {
	conn, err := grpc.NewClient("api.apinb.com:10020",
		grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("连接失败: %v", err)
	}
	defer conn.Close()

	client := pb.NewMailClient(conn)

	ctx, cancel := createContext()
	defer cancel()

	req := &pb.SendMailRequest{
		TemplateKey: "default",
		To:          "271055687@qq.com",
		Paramters: map[string]string{
			"code": "123456",
		},
		Provider: "gmail",
	}
	res, err := client.Send(ctx, req)
	if err != nil {
		log.Fatalf("RPC调用失败: %v", err)
	}

	log.Printf("响应结果: %v", res.String())
}

// 创建上下文和元数据
func createContext() (context.Context, context.CancelFunc) {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

	newMetaData := metadata.New(nil)
	newMetaData.Set("request_id", utils.UUID())
	outCtx := metadata.NewOutgoingContext(ctx, newMetaData)

	return outCtx, cancel
}
