package services

import (
	"flag"
	"fmt"
	"io"
	"log"
	"time"

	"github.com/fgm/izidic"
)

const (
	// Flags
	PAddr        = "addr"
	PPrefix      = "prefix"
	PProfile     = "profile"
	PQName       = "queue-name"
	PRegion      = "region"
	PCSRFSecret  = "csrf-secret"
	PStoreSecret = "store-secret"
	PTTL         = "ttl"
	PURL         = "url"
	PVTO         = "vto"
	PWait        = "wait"

	// Non-flags
	PArgs    = "args"
	PHandler = "handler"
	PName    = "name"
	PWriter  = "writer"

	// Services
	SvcClient       = "sqs"
	SvcConsumer     = "consume-message"
	SvcFlags        = "flags"
	SvcHttp         = "http"
	SvcLister       = "lister"
	SvcLogger       = "logger"
	SvcMessageStore = "message-store"
	SvcProducer     = "producer"
	SvcReceiver     = "receiver"
	SvcRedriver     = "redriver"
	SvcRenderer     = "renderer"
)

func FlagsService(dic *izidic.Container) (any, error) {
	fs := flag.NewFlagSet(dic.MustParam(PName).(string), flag.ContinueOnError)
	addr := fs.String(PAddr, ":8080", "The IP address on which to listen")
	profile := fs.String(PProfile, "test-profile", "The AWS profile")
	region := fs.String(PRegion, "eu-west-3", "The AWS region to connect to")
	qName := fs.String(PQName, "dummy-queue", "The queue name")
	csrfSecret := fs.String(PCSRFSecret, "csrfSecret", "The CSRF secret")
	prefix := fs.String(PPrefix, "", "The queue prefix to filter the main list")
	storeSecret := fs.String(PStoreSecret, "storeSecret", "The session store secret")
	sqsURL := fs.String(PURL, "http://localhost:4566", "The SQS endpoint URL")
	ttl := fs.Duration(PTTL, 10*time.Minute, "The message store TTL")
	vto := fs.Duration(PVTO, 10*time.Minute, "The redrive visibility timeout")
	wait := fs.Int(PWait, 3, "The maximum number of seconds to wait when receiving messages")
	if err := fs.Parse(dic.MustParam(PArgs).([]string)); err != nil {
		return nil, fmt.Errorf("cannot obtain CLI args")
	}

	// Durations are signed, and Duration.Round rounds down, but we want positive durations, rounded up.
	*vto = (vto.Abs() + 500*time.Millisecond).Round(time.Second)

	dic.Store(PAddr, *addr)
	dic.Store(PProfile, *profile)
	dic.Store(PQName, *qName)
	dic.Store(PRegion, *region)
	dic.Store(PCSRFSecret, []byte(*csrfSecret))
	dic.Store(PPrefix, *prefix)
	dic.Store(PStoreSecret, []byte(*storeSecret))
	dic.Store(PTTL, *ttl)
	dic.Store(PURL, *sqsURL)
	dic.Store(PVTO, *vto)
	dic.Store(PWait, *wait)
	return fs, nil
}

// LoggerService is an izidic.Service also containing a one-time initialization action.
func LoggerService(dic *izidic.Container) (any, error) {
	w := dic.MustParam(PWriter).(io.Writer)
	log.SetOutput(w) // Support dependency code not taking an injected logger.
	logger := log.New(w, "", log.LstdFlags)
	return logger, nil
}
