package main

import (
	"net/http"
)

func (api *api) internalServerError(w http.ResponseWriter, r *http.Request, err error) {
	api.logger.Errorw("internal server error", "method", r.Method, "path", r.URL.Path, "error", err.Error())
	errorJSON(w, http.StatusInternalServerError, "internal server error")
}

func (api *api) badRequestError(w http.ResponseWriter, r *http.Request, err error) {
	api.logger.Warnw("bad request", "method", r.Method, "path", r.URL.Path, "error", err.Error())
	errorJSON(w, http.StatusInternalServerError, err.Error())
}

func (api *api) notFoundError(w http.ResponseWriter, r *http.Request, err error) {
	api.logger.Warnw("not found", "method", r.Method, "path", r.URL.Path, "error", err.Error())
	errorJSON(w, http.StatusNotFound, "resource not found")
}

func (api *api) conflictError(w http.ResponseWriter, r *http.Request, err error) {
	api.logger.Errorw("conflict", "method", r.Method, "path", r.URL.Path, "error", err.Error())
	errorJSON(w, http.StatusConflict, "resource conflict")
}

func (api *api) unauthorizedError(w http.ResponseWriter, r *http.Request, err error) {
	api.logger.Warnw("unauthorized", "method", r.Method, "path", r.URL.Path, "error", err.Error())
	errorJSON(w, http.StatusUnauthorized, "unauthorized")
}

func (api *api) unauthorizedBasicError(w http.ResponseWriter, r *http.Request, err error) {
	api.logger.Warnw("unauthorized", "method", r.Method, "path", r.URL.Path, "error", err.Error())
	w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
	errorJSON(w, http.StatusUnauthorized, "basic unauthorized")
}

func (api *api) forbiddenError(w http.ResponseWriter, r *http.Request, err error) {
	api.logger.Warnw("forbidden", "method", r.Method, "path", r.URL.Path, "error", err.Error())
	errorJSON(w, http.StatusForbidden, "forbidden")
}

func (api *api) rateLimitExceededError(w http.ResponseWriter, r *http.Request, retryAfter string) {
	api.logger.Warnw("rate limit exceeded", "method", r.Method, "path", r.URL.Path)
	w.Header().Set("Retry-After", retryAfter)
	errorJSON(w, http.StatusTooManyRequests, "too many requests, retry after "+retryAfter)
}
