package server

import (
	"music-server/internal/backend"
	"music-server/internal/db"
	"net/http"

	"github.com/labstack/echo/v4"
)

type IndexHandler struct {
}

func NewIndexHandler() *IndexHandler {
	return &IndexHandler{}
}

// GetVersion godoc
//
//	@Summary		Getting the version of the backend
//	@Description	get string by ID
//	@Tags			accounts
//	@Accept			json
//	@Produce		json
//	@Success		200	{object}	backend.VersionData
//	@Failure		404	{object}	string
//	@Router			/version [get]
func (i *IndexHandler) GetVersion(ctx echo.Context) error {
	versionHistory := backend.GetVersionHistory()
	if versionHistory.Version == "" {
		return ctx.JSON(http.StatusNotFound, "version not found")
	}
	return ctx.JSON(http.StatusOK, versionHistory)
}

func (i *IndexHandler) GetDBTest(ctx echo.Context) error {
	backend.TestDB()
	return ctx.JSON(http.StatusOK, "TestedDB")
}

func (i *IndexHandler) HealthCheck(ctx echo.Context) error {
	return ctx.JSON(http.StatusOK, db.Health())
}

func (i *IndexHandler) GetCharacters(ctx echo.Context) error {
	characters := backend.GetCharacters()
	return ctx.JSON(http.StatusOK, characters)
}

func (i *IndexHandler) GetCharacter(ctx echo.Context) error {
	character := ctx.QueryParam("character")
	return ctx.File(backend.GetCharacter(character))
}
