package handlers

import (
	"encoding/json"

	"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
	"github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/response"
	"github.com/gofiber/fiber/v2"
)

type ResultRes struct {
	ResultData json.RawMessage     `json:"result_data" swaggerignore:"true"`
	Outcomes   []domain.BetOutcome `json:"outcomes"`
}

// This will take an event ID and return the success results for
// all the odds for that event.
// @Summary Get results for an event
// @Description Get results for an event
// @Tags result
// @Accept json
// @Produce json
// @Param id path string true "Event ID"
// @Success 200 {array} ResultRes
// @Failure 400 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /result/{id} [get]
func (h *Handler) GetResultsByEventID(c *fiber.Ctx) error {
	eventID := c.Params("id")
	if eventID == "" {
		h.logger.Error("Event ID is required")
		return fiber.NewError(fiber.StatusBadRequest, "Event ID is required")
	}

	results, outcomes, err := h.resultSvc.GetResultsForEvent(c.Context(), eventID)
	if err != nil {
		h.logger.Error("Failed to get results by Event ID", "eventID", eventID, "error", err)
		return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve results")
	}

	resultRes := ResultRes{
		ResultData: results,
		Outcomes:   outcomes,
	}

	return response.WriteJSON(c, fiber.StatusOK, "Results retrieved successfully", resultRes, nil)
}
