package server

import (
	"fmt"
	"github.com/goccy/go-json"
	"madsky.ru/go-tracker/internal/model/user"
	"madsky.ru/go-tracker/internal/server/response"
	"net/http"
)

func (app *Application) getProfile(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	res := ctx.Value(UserContext{}).(*user.User)

	response.WriteJSON(w, nil, http.StatusOK, response.ToUserResponseDto(res))
}

func (app *Application) updateProfile(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	u := ctx.Value(UserContext{}).(*user.User)

	dec := json.NewDecoder(r.Body)
	dec.DisallowUnknownFields()

	var dto user.UpdateUserDTO

	if err := dec.Decode(&dto); err != nil {
		response.Error(w, err, http.StatusBadRequest)
		return
	}

	res, err := app.Storage.User.Update(app.Ctx, u.ID, &dto)
	if err != nil {
		response.Error(w, err, http.StatusBadRequest)
		return
	}

	response.WriteJSON(w, nil, http.StatusOK, response.ToUserResponseDto(res))
}

func (app *Application) deleteProfile(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	u := ctx.Value(UserContext{}).(*user.User)

	fmt.Println(u)

	response.WriteJSON(w, nil, http.StatusOK, nil)
}
