package client

import (
	"sipro-mps/internal/config"
	"sipro-mps/internal/tasks/types"

	"github.com/hibiken/asynq"
)

var Client *asynq.Client

// InitClient initializes the Asynq client with the provided Redis configuration.
func InitClient(redisConfig config.RedisConfig) {
	client := asynq.NewClient(asynq.RedisClientOpt{
		Addr:     redisConfig.Addr,
		Password: redisConfig.Password,
	})
	Client = client
}

func CloseClient() {
	if Client != nil {
		if err := Client.Close(); err != nil {
			panic(err)
		}
	}
}

func EnqueueFetchProductsTask(taskType string, marketplaceId int) error {
	task, err := types.NewFetchProductsTask(taskType, marketplaceId)
	if err != nil {
		return err
	}
	_, err = Client.Enqueue(task)
	if err != nil {
		return err
	}
	return nil
}
