package shortstat

import (
	"context"
	"database/sql"
	"gitea.pena/SQuiz/common/dal"
	"gitea.pena/SQuiz/worker/internal/workers"
	"time"
)

// ShortStat struct of worker for expiration worker
type ShortStat struct {
	w *workers.Worker
	d *dal.DAL
}

// New creation of worker
func New(d *dal.DAL, p time.Duration) *ShortStat {
	return &ShortStat{
		w: workers.New(p),
		d: d,
	}
}

// Start method for starting worker with long polling from postgres
func (t *ShortStat) Start(ctx context.Context) {
	t.w.Start(ctx, func(ctx context.Context) error {
		if err := t.d.WorkerRepo.WorkerStatProcess(ctx); err != nil {
			if err != sql.ErrNoRows {
				return err
			}
		}

		return nil
	})
}

func (t *ShortStat) ExposeErr(ctx context.Context, err *error) {
	t.w.ExposeErr(ctx, err)
}
