package server

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

	"github.com/hibiken/asynq"
	"github.com/jackc/pgx/v5/pgxpool"
)

type AsynqServer struct {
	redisConfig *config.RedisConfig
	dbpool      *pgxpool.Pool
}

func NewAsynqServer(redisConfig *config.RedisConfig, dbpool *pgxpool.Pool) *AsynqServer {
	return &AsynqServer{
		redisConfig: redisConfig,
		dbpool:      dbpool,
	}
}
func (s *AsynqServer) createMux() *asynq.ServeMux {
	mux := asynq.NewServeMux()

	// Register task handlers here
	mux.Handle(types.TypeWbFetchProducts, &wb.FetchProductsProcessor{Dbpool: s.dbpool})
	mux.Handle(types.TypeOzonFetchProducts, &ozon.FetchProductsProcessor{Dbpool: s.dbpool})
	return mux
}

func (s *AsynqServer) Run() {
	srv := asynq.NewServer(
		asynq.RedisClientOpt{Addr: s.redisConfig.Addr, Password: s.redisConfig.Password},
		asynq.Config{
			Concurrency: 2,
		},
	)
	mux := s.createMux()
	if err := srv.Run(mux); err != nil {
		panic(err)
	}
}
