package internal

import (
	"encoding/json"
	"net/http"
)

func (a *adminHandler) addRecursor(r *http.Request) (*RestResponse, error) {
	var recursorHttpInput RecursorRow

	if err := json.NewDecoder(r.Body).Decode(&recursorHttpInput); err != nil {
		return ErrorResponse(http.StatusUnprocessableEntity, err), nil
	}

	ipAddr, port, ok := recursorHttpInput.ValidIp()
	if !ok {
		return BasicResponse(false, "The ip address provided is invalid."), nil
	}

	if err := a.Storage.AddRecursors(ipAddr, port, recursorHttpInput.TimeoutMs, recursorHttpInput.Weight); err != nil {
		return nil, err
	}

	return BasicResponse(true, recursorHttpInput), nil
}

func (a *adminHandler) getRecursors(r *http.Request) (*RestResponse, error) {
	recursors, err := a.Storage.GetRecursors()
	if err != nil {
		return nil, err
	}

	return BasicResponse(true, recursors), nil
}

func (a *adminHandler) getRecursor(r *http.Request) (*RestResponse, error) {
	return BasicResponse(false, "Nothing here yet"), nil
}

func (a *adminHandler) deleteRecursor(r *http.Request) (*RestResponse, error) {
	return BasicResponse(false, "Nothing here yet"), nil
}
