package shortstat

import (
	"context"
	"database/sql"
	"fmt"
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
	"penahub.gitlab.yandexcloud.net/backend/quiz/worker/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 {
		fmt.Println("SHORTSTAT1")
		if err := t.d.WorkerRepo.WorkerStatProcess(ctx); err != nil {
			fmt.Println("SHORTSTAT2", err)
			if err != sql.ErrNoRows {
				return err
			}
		}

		return nil
	})
}

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