package service

import (
	"bpmsapi/dto"
	"bpmsapi/entity"
	"bpmsapi/repository"
	"database/sql"

	"github.com/jmoiron/sqlx"
)

type TimeSlotusecase interface {
	GetTimeSlot(input entity.GetTimeSlotInput) entity.CustomResponse
}

type TimeSlotService struct {
	timeslotRepository *repository.TimeSlotRepository
	tx                 *sqlx.Tx
}

func TimeSlotServiceInit(timeslotRepository *repository.TimeSlotRepository, tx *sqlx.Tx) *TimeSlotService {
	return &TimeSlotService{
		timeslotRepository: timeslotRepository,
		tx:                 tx,
	}
}

func (timeslotServ *TimeSlotService) GetTimeSlot(input entity.GetTimeSlotInput) (response entity.CustomResponse) {

	resp, err := timeslotServ.timeslotRepository.FetchAvailableTimeSlot(input)
	if err != nil {
		timeslotServ.tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErrorMessageGetTimeSlot + err.Error()
	} else {
		commitError := timeslotServ.tx.Commit()
		if commitError != nil && commitError == sql.ErrNoRows {
			response.Resultcode = 1
			response.Message = dto.ErrorMessageGetTimeSlot + err.Error()
		} else {
			
			response.Resultcode = 0
			response.Message = dto.SuccessMessageGetTimeSlot
			response.Payload = resp

		}
	}

	return response
}
