package authhttp

import (
	authsvc "legalgo-BE-go/internal/services/auth"
	"legalgo-BE-go/internal/utilities/response"
	"legalgo-BE-go/internal/utilities/utils"
	"net/http"

	"github.com/go-chi/chi/v5"
)

func GetStaffProfile(
	router chi.Router,
	authSvc authsvc.Auth,
) {
	router.Get("/staff/profile", func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		destructedToken, err := utils.GetTokenDetail(r)
		if err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				err.Error(),
			)
			return
		}

		staffProfile, err := authSvc.GetStaffProfile(destructedToken.Email)
		if err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				err.Error(),
			)
			return
		}

		response.RespondJsonSuccess(ctx, w, staffProfile)
	})
}

func GetUserProfile(
	router chi.Router,
	authSvc authsvc.Auth,
) {
	router.Get("/user/profile", func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		destructedToken, err := utils.GetTokenDetail(r)
		if err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				err.Error(),
			)
			return
		}

		userProfile, err := authSvc.GetUserProfile(destructedToken.Email)
		if err != nil {
			response.ResponseWithErrorCode(
				ctx,
				w,
				err,
				response.ErrBadRequest.Code,
				response.ErrBadRequest.HttpCode,
				err.Error(),
			)
			return
		}

		response.RespondJsonSuccess(ctx, w, userProfile)
	})
}
