package http

import (
	"github.com/gin-gonic/gin"
	"golang.source-fellows.com/training-fellow/booking/internal"
	"golang.source-fellows.com/training-fellow/booking/internal/model"
	"net/http"
)

type Training struct {
	Name         string
	NumberOfDays int
}

type TrainingHandler struct {
	trainingListingService *internal.TrainingListingService
}

func (th *TrainingHandler) GetAllTrainings(ginCtx *gin.Context) {

	ctx := ginCtx.Request.Context()

	trainings, err := th.trainingListingService.GetAllTrainings(ctx)
	if err != nil {
		ginCtx.JSON(NewProblemFromErr(err))
		return
	}

	ginCtx.JSON(http.StatusOK, mapTrainingsListForJSON(trainings))

}

func mapTrainingsListForJSON(trainings []*model.Training) []Training {
	var returnList = make([]Training, 0)
	for _, training := range trainings {
		returnList = append(returnList, mapTrainingForJSON(training))
	}
	return returnList
}

func mapTrainingForJSON(training *model.Training) Training {
	return Training{
		Name:         training.Name(),
		NumberOfDays: training.NumberOfDays(),
	}
}
