// Copyright (C) 2019-2023 Nicola Murino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package httpd

import (
	"fmt"
	"net/http"

	"github.com/go-chi/render"

	"github.com/drakkan/sftpgo/v2/internal/common"
	"github.com/drakkan/sftpgo/v2/internal/dataprovider"
	"github.com/drakkan/sftpgo/v2/internal/logger"
)

func getMetadataChecks(w http.ResponseWriter, r *http.Request) {
	r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
	claims, err := getTokenClaims(r)
	if err != nil || claims.Username == "" {
		sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
		return
	}
	render.JSON(w, r, common.ActiveMetadataChecks.Get(claims.Role))
}

func startMetadataCheck(w http.ResponseWriter, r *http.Request) {
	r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
	claims, err := getTokenClaims(r)
	if err != nil || claims.Username == "" {
		sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
		return
	}

	user, err := dataprovider.GetUserWithGroupSettings(getURLParam(r, "username"), claims.Role)
	if err != nil {
		sendAPIResponse(w, r, err, "", getRespStatus(err))
		return
	}
	if !common.ActiveMetadataChecks.Add(user.Username, user.Role) {
		sendAPIResponse(w, r, err, fmt.Sprintf("Another check is already in progress for user %q", user.Username),
			http.StatusConflict)
		return
	}
	go doMetadataCheck(user) //nolint:errcheck

	sendAPIResponse(w, r, err, "Check started", http.StatusAccepted)
}

func doMetadataCheck(user dataprovider.User) error {
	defer common.ActiveMetadataChecks.Remove(user.Username)

	err := user.CheckMetadataConsistency()
	if err != nil {
		logger.Warn(logSender, "", "error checking metadata for user %q: %v", user.Username, err)
		return err
	}
	logger.Debug(logSender, "", "metadata check completed for user: %q", user.Username)
	return nil
}
