package http_router

import (
	"context"
	"encoding/json"
	"log/slog"
	"net/http"

	"git-molva.ru/Molva/molva-backend/services/api_gateway/internal/constants"
	"github.com/gorilla/mux"

	rmodel "git-molva.ru/Molva/molva-backend/services/api_gateway/internal/request_model"
)

func (h *handler) getProfileAgentHandler(w http.ResponseWriter, r *http.Request) {
	const handlerName = "getAgentProfileHandler"

	var (
		vars    = mux.Vars(r)
		agentId = vars["agent_id"]
	)

	result, err := h.agentService.GetProfile(r.Context(), &rmodel.ProfileGetRequest{
		Id: agentId,
	})
	if err != nil {
		h.handleAgentError(w, err)
		h.logger.Error("error getting agent profile",
			slog.String("error", err.Error()),
			slog.String("handler", handlerName),
		)

		return
	}

	w.Header().Set("Content-Type", "application/json")

	if err := json.NewEncoder(w).Encode(result); err != nil {
		http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError)
		h.logger.Error("error encoding response",
			slog.String("error", err.Error()),
			slog.String("handler", handlerName),
		)
	}
}

func (h *handler) updateProfileAgentHandler(w http.ResponseWriter, r *http.Request) {
	const handlerName = "setAgentProfileHandler"

	var (
		vars    = mux.Vars(r)
		agentId = vars["agent_id"]
	)

	var request rmodel.ProfileUpdateRequest

	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		http.Error(w, constants.ErrBadRequest.Error(), http.StatusBadRequest)
		h.logger.Error("error unmarshalling request: ",
			slog.String("error", err.Error()),
			slog.String("handler", handlerName),
		)

		return
	}

	request.Id = agentId

	if _, err := h.agentService.UpdateProfile(r.Context(), &request); err != nil {
		h.handleAgentError(w, err)
		h.logger.Error("error updating agent profile",
			slog.String("error", err.Error()),
			slog.String("handler", handlerName),
		)

		return
	}

	go h.createSetProfileEvent(
		context.Background(),
		agentId,
		true,
		handlerName,
	)

	w.WriteHeader(http.StatusNoContent)
}

func (h *handler) getProfileDisributorHandler(w http.ResponseWriter, r *http.Request) {
	const handlerName = "getProfileDisributorHandler"

	var (
		vars   = mux.Vars(r)
		distId = vars["distributor_id"]
	)

	result, err := h.distributorService.GetProfile(r.Context(), &rmodel.ProfileGetRequest{
		Id: distId,
	})
	if err != nil {
		h.handleDistributorError(w, err)
		h.logger.Error("error getting distributor profile",
			slog.String("error", err.Error()),
			slog.String("handler", handlerName),
		)

		return
	}

	w.Header().Set("Content-Type", "application/json")

	if err := json.NewEncoder(w).Encode(result); err != nil {
		http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError)
		h.logger.Error("error encoding response",
			slog.String("error", err.Error()),
			slog.String("handler", handlerName),
		)
	}
}

func (h *handler) updateProfileDistributorHandler(w http.ResponseWriter, r *http.Request) {
	const handlerName = "updateProfileDistributorHandler"

	var (
		vars   = mux.Vars(r)
		distId = vars["distributor_id"]
	)

	var request rmodel.ProfileUpdateRequest

	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		http.Error(w, constants.ErrBadRequest.Error(), http.StatusBadRequest)
		h.logger.Error("error while unmarshalling request: ",
			slog.String("error", err.Error()),
			slog.String("handler", handlerName),
		)

		return
	}

	request.Id = distId

	if _, err := h.distributorService.UpdateProfile(r.Context(), &request); err != nil {
		h.handleDistributorError(w, err)
		h.logger.Error("error updating distributor profile",
			slog.String("error", err.Error()),
			slog.String("handler", handlerName),
		)

		return
	}

	go h.createSetProfileEvent(
		context.Background(),
		distId,
		false,
		handlerName,
	)

	w.WriteHeader(http.StatusNoContent)
}
