// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package handler

import (
	"bytes"
	"cmp"
	"net/http"
	"slices"

	"github.com/erigontech/erigon/cl/beacon/beaconhttp"
	"github.com/erigontech/erigon/cl/clparams"
	"github.com/erigontech/erigon/cl/cltypes"
	"github.com/erigontech/erigon/cl/utils"
)

func (a *ApiHandler) getSpec(w http.ResponseWriter, r *http.Request) (*beaconhttp.BeaconResponse, error) {
	outSpec := struct {
		*clparams.BeaconChainConfig
		*clparams.NetworkConfig
		MinEpochsForBlockRequests uint64 `json:"MIN_EPOCHS_FOR_BLOCK_REQUESTS,string"`
	}{
		BeaconChainConfig:         a.beaconChainCfg,
		NetworkConfig:             a.netConfig,
		MinEpochsForBlockRequests: a.beaconChainCfg.MinEpochsForBlockRequests(),
	}

	return newBeaconResponse(outSpec), nil
}

func (a *ApiHandler) getDepositContract(w http.ResponseWriter, r *http.Request) (*beaconhttp.BeaconResponse, error) {
	return newBeaconResponse(struct {
		ChainId         uint64 `json:"chain_id,string"`
		DepositContract string `json:"address"`
	}{ChainId: a.beaconChainCfg.DepositChainID, DepositContract: a.beaconChainCfg.DepositContractAddress}), nil

}

func (a *ApiHandler) getForkSchedule(w http.ResponseWriter, r *http.Request) (*beaconhttp.BeaconResponse, error) {
	response := make([]cltypes.Fork, 0, len(a.beaconChainCfg.ForkVersionSchedule))
	// create first response (unordered and incomplete)
	for currentVersion, entry := range a.beaconChainCfg.ForkVersionSchedule {
		response = append(response, cltypes.Fork{
			CurrentVersion: currentVersion,
			Epoch:          entry.Epoch,
		})
	}
	// Sort the responses by epoch
	slices.SortFunc(response, func(a, b cltypes.Fork) int {
		if a.Epoch == b.Epoch {
			return bytes.Compare(a.CurrentVersion[:], b.CurrentVersion[:])
		}
		return cmp.Compare(a.Epoch, b.Epoch)
	})
	previousVersion := utils.Uint32ToBytes4(uint32(a.beaconChainCfg.GenesisForkVersion))
	for i := range response {
		response[i].PreviousVersion = previousVersion
		previousVersion = response[i].CurrentVersion
	}
	return newBeaconResponse(response), nil
}
