package services

import (
	"context"
	"io"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/sqs"
	"github.com/fgm/izidic"
	"gopkg.in/yaml.v3"
)

func ListerService(dic *izidic.Container) (any, error) {
	cli := dic.MustService("sqs").(*sqs.Client)
	w := dic.MustParam(PWriter).(io.Writer)
	return func(ctx context.Context) string {
		return lister(ctx, w, cli)
	}, nil
}

func lister(ctx context.Context, w io.Writer, client *sqs.Client) string {
	lqo, err := client.ListQueues(ctx, &sqs.ListQueuesInput{
		MaxResults:      aws.Int32(10),
		NextToken:       nil,
		QueueNamePrefix: aws.String(""),
	})
	if err != nil {
		log.Fatalf("failed listing queues: %v", err)
	}
	y := yaml.NewEncoder(w)
	y.Encode(lqo.QueueUrls)
	return lqo.QueueUrls[0]
}
