package mail

import (
	"errors"
	"net/http"

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

	"github.com/ultisuite/ulti-backend/internal/api/apiresponse"
	"github.com/ultisuite/ulti-backend/internal/api/apivalidate"
	"github.com/ultisuite/ulti-backend/internal/api/middleware"
	"github.com/ultisuite/ulti-backend/internal/api/query"
)

func (h *Handler) ListSignatures(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	params, err := query.ParseListRequest(r)
	if err != nil {
		apivalidate.WriteQueryError(w, r, err)
		return
	}

	result, err := h.svc.ListSignatures(r.Context(), claims.Sub, params)
	if err != nil {
		h.logger.Error("list signatures", "error", err)
		apivalidate.WriteInternal(w, r)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, result)
}

func (h *Handler) GetSignature(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	signatureID := chi.URLParam(r, "signatureID")
	if d := validateSignatureUUID(signatureID); d != nil {
		apivalidate.WriteNotFound(w, r, "not found")
		return
	}

	signature, err := h.svc.GetSignature(r.Context(), claims.Sub, signatureID)
	if err != nil {
		if errors.Is(err, ErrNotFound) {
			apivalidate.WriteNotFound(w, r, "not found")
			return
		}
		h.logger.Error("get signature", "error", err)
		apivalidate.WriteInternal(w, r)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, signature)
}

func (h *Handler) CreateSignature(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())

	var req createSignatureRequest
	if err := apivalidate.DecodeJSON(w, r, maxSignatureRequestBody, &req); err != nil {
		return
	}
	if verr := validateCreateSignature(&req); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	id, err := h.svc.CreateSignature(r.Context(), claims.Sub, &req)
	if err != nil {
		h.logger.Error("create signature", "error", err)
		apivalidate.WriteInternal(w, r)
		return
	}
	apiresponse.WriteJSON(w, http.StatusCreated, map[string]string{"id": id})
}

func (h *Handler) UpdateSignature(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	signatureID := chi.URLParam(r, "signatureID")
	if d := validateSignatureUUID(signatureID); d != nil {
		apivalidate.WriteNotFound(w, r, "not found")
		return
	}

	var req updateSignatureRequest
	if err := apivalidate.DecodeJSON(w, r, maxSignatureRequestBody, &req); err != nil {
		return
	}
	if verr := validateUpdateSignature(&req); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}

	if err := h.svc.UpdateSignature(r.Context(), claims.Sub, signatureID, &req); err != nil {
		if errors.Is(err, ErrNotFound) {
			apivalidate.WriteNotFound(w, r, "not found")
			return
		}
		h.logger.Error("update signature", "error", err)
		apivalidate.WriteInternal(w, r)
		return
	}
	w.WriteHeader(http.StatusNoContent)
}

func (h *Handler) DeleteSignature(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	signatureID := chi.URLParam(r, "signatureID")
	if d := validateSignatureUUID(signatureID); d != nil {
		apivalidate.WriteNotFound(w, r, "not found")
		return
	}

	if err := h.svc.DeleteSignature(r.Context(), claims.Sub, signatureID); err != nil {
		if errors.Is(err, ErrNotFound) {
			apivalidate.WriteNotFound(w, r, "not found")
			return
		}
		h.logger.Error("delete signature", "error", err)
		apivalidate.WriteInternal(w, r)
		return
	}
	w.WriteHeader(http.StatusNoContent)
}

func validateSignatureUUID(id string) *apivalidate.FieldDetail {
	return validateAccountUUID(id)
}
