// Copyright (C) 2023 Marius Schellenberger

package types

import (
	"net/http"
	"strconv"
)

// Error interface is a custom api error
type Error interface {
	Error() string
	JSON() string
	Code() int
}

// Err implements the Error interface
type Err struct {
	err  string
	code int
}

// Error returns the error string
func (a Err) Error() string {
	return a.err
}

// Error returns the error string as JSON format
func (a Err) JSON() string {
	return `{"error":"` + a.err + `","code":` + strconv.Itoa(a.code) + `}`
}

// Code returns the HTTP status code
func (a Err) Code() int {
	return a.code
}

func newError(err string, code int) Error {
	return Err{
		err:  err,
		code: code,
	}
}

var (
	ErrInvalidAPIRoute    = newError("invalid api route", http.StatusNotFound)
	ErrInvalid            = newError("invalid data", http.StatusBadRequest)
	ErrBadreq             = newError("bad request", http.StatusBadRequest)
	ErrForbidden          = newError("forbidden", http.StatusForbidden)
	ErrNotFound           = newError("not found", http.StatusNotFound)
	ErrKeyNotFound        = newError("key not found", http.StatusNotFound)
	ErrReqNotFound        = newError("key request not found", http.StatusNotFound)
	ErrTooManyApprovals   = newError("too many approvals pending", http.StatusConflict)
	ErrKeyRequestRejected = newError("key request rejected", http.StatusForbidden)
	ErrMNA                = newError("method not allowed", http.StatusMethodNotAllowed)
	ErrISE                = newError("internal server error", http.StatusInternalServerError)
)
