package users

import (
	"context"
	"net/http"

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

func HandleUser(w http.ResponseWriter, r *http.Request) {
	uuid := r.PathValue("user_uuid")
	var user models.User
	count, err := core.DB.NewSelect().
		Model(&user).
		Where("user_id = ?", uuid).
		Relation("Roles.Permissions").
		Limit(1).
		ScanAndCount(context.Background())

	if count == 0 {
		core.JSONError{
			Status:  core.Error,
			Message: "User not found.",
		}.Respond(w, http.StatusNotFound)
		return
	}

	if err != nil {
		core.JSONError{
			Status:  core.Error,
			Message: err.Error(),
		}.Respond(w, http.StatusInternalServerError)
		return
	}

	user.Password = ""

	// TODO : Remove password
	core.JSONSuccess{
		Status:  core.Success,
		Message: "User found.",
		Data:    user,
	}.Respond(w, http.StatusOK)
}
