package tmobile

import (
	"context"
	"sync"
	"testing"
	"time"
)

func TestQueueIntegration(t *testing.T) {
	t.Skip()
	tg, err := InitTokenGen(
		"PRIVATE KEY",
		"ID",
		"PASSWORD",
	)

	if err != nil {
		t.Error(err)
	}

	tmobc, err := NewTMobileClient(
		Endpoint,
		tg, time.Minute*2)
	if err != nil {
		t.Error(err)
		return
	}

	client := NewQueue(tmobc, nil)

	iccids := []string{"8901882000784161105", "8901882000784163135", "8901882000784161071",
		"8901882000784164976",
		"8901882000784163135",
		"8901882000787584451",
		"8901882000784166427",
		"8901882000784163671",
		"8901882000784163945",
		"8901882000784167342",
		"8901882000784166625",
	}

	sentMessages := make([]string, 0)
	quickLock := sync.Mutex{}
	wg := sync.WaitGroup{}
	// Running test 10 times in a row to see if we get any failures
	for _, iccid := range iccids {
		wg.Add(1)
		go func(iccid string) {
			msg := SendSMSRequest{
				ICCID:       iccid,
				MessageText: "newer_test",
				await:       true,
			}
			ctx, cancel := context.WithTimeout(context.Background(), time.Second*45)
			defer cancel()
			id, err := client.SendSMS(ctx, &msg)
			if err != nil {
				t.Error(err)
			}
			quickLock.Lock()
			sentMessages = append(sentMessages, id)
			quickLock.Unlock()
			wg.Done()
		}(iccid)
	}
	wg.Wait()

	time.Sleep(time.Second * 12)

	for _, id := range sentMessages {
		out, err := tmobc.Details(context.Background(), id)
		if err != nil {
			t.Error(err)
		}
		t.Log(out.Status)
	}
}

func BenchmarkQueue(b *testing.B) {
	sim := newTMboileSimulator()

	wrap := NewQueue(&sim, nil)
	tt := sync.WaitGroup{}
	for x := 0; x < 1000; x++ {
		tt.Add(1)
		go func() {
			wrap.SendSMS(context.Background(), &SendSMSRequest{
				MessageText: ".",
				await:       true,
			})
			tt.Done()
		}()
	}
	tt.Wait()
	time.Sleep(time.Second * 10)
}
