package job

import (
	"github.com/jobs-scraper/internal/dto"
	"github.com/jobs-scraper/libs/ports"
	"github.com/jobs-scraper/libs/repo"
)

type GetJobs struct {
	jobRepo *repo.JobRepository
}

func NewGetJobsHandler(jobRepo *repo.JobRepository) *GetJobs {
	return &GetJobs{
		jobRepo: jobRepo,
	}
}

func (cj *GetJobs) Handle(query ports.GetJobQuery) ([]dto.JobDTO, int, error) {
	filter := repo.JobFilter{
		Location:   query.Location,
		Keywords:   query.Keywords,
		Provider:   query.Provider,
		PageSize:   query.PageSize,
		PageNumber: query.PageNumber,
	}

	results, totalCount, err := cj.jobRepo.GetAllJobs(filter)

	if err != nil {
		return nil, 0, err
	}
	return dto.JobsFromDomain(results), totalCount, nil
}
