package events

import (
	"context"
	"fmt"
	"net/http"

	"fr.latosa-escrima/core"
	"fr.latosa-escrima/core/models"
)

func HandleEvents(w http.ResponseWriter, r *http.Request) {
	var events []models.Event
	rowsCount, err := core.DB.NewSelect().Model(&events).ScanAndCount(context.Background())
	if err != nil {
		core.JSONError{
			Status:  core.Error,
			Message: err.Error(),
		}.Respond(w, http.StatusInternalServerError)
		return
	}

	core.JSONSuccess{
		Status:  core.Success,
		Message: fmt.Sprintf("%d Event successfully sent", rowsCount),
		Data:    events,
	}.Respond(w, http.StatusOK)
	return
}
