package keeper_test

import (
	gocontext "context"
	"time"

	sdk "git.cw.tr/mukan-network/mukan-sdk/types"
	"git.cw.tr/mukan-network/mukan-sdk/types/query"
	"git.cw.tr/mukan-network/mukan-sdk/x/slashing/testutil"
	slashingtypes "git.cw.tr/mukan-network/mukan-sdk/x/slashing/types"
)

func (s *KeeperTestSuite) TestGRPCQueryParams() {
	queryClient := s.queryClient
	require := s.Require()

	paramsResp, err := queryClient.Params(gocontext.Background(), &slashingtypes.QueryParamsRequest{})

	require.NoError(err)
	require.Equal(testutil.TestParams(), paramsResp.Params)
}

func (s *KeeperTestSuite) TestGRPCSigningInfo() {
	queryClient, ctx, keeper := s.queryClient, s.ctx, s.slashingKeeper
	require := s.Require()

	infoResp, err := queryClient.SigningInfo(gocontext.Background(), &slashingtypes.QuerySigningInfoRequest{ConsAddress: ""})
	require.Error(err)
	require.ErrorContains(err, "invalid request")
	require.Nil(infoResp)

	signingInfo := slashingtypes.NewValidatorSigningInfo(
		consAddr,
		0,
		int64(0),
		time.Unix(2, 0),
		false,
		int64(0),
	)

	require.NoError(keeper.SetValidatorSigningInfo(ctx, consAddr, signingInfo))
	info, err := keeper.GetValidatorSigningInfo(ctx, consAddr)
	require.NoError(err)

	infoResp, err = queryClient.SigningInfo(gocontext.Background(),
		&slashingtypes.QuerySigningInfoRequest{ConsAddress: consAddr.String()})
	require.NoError(err)
	require.Equal(info, infoResp.ValSigningInfo)
}

func (s *KeeperTestSuite) TestGRPCSigningInfos() {
	queryClient, ctx, keeper := s.queryClient, s.ctx, s.slashingKeeper
	require := s.Require()

	// set two validator signing information
	consAddr1 := sdk.ConsAddress("addr1_______________")
	consAddr2 := sdk.ConsAddress("addr2_______________")
	signingInfo := slashingtypes.NewValidatorSigningInfo(
		consAddr1,
		0,
		int64(0),
		time.Unix(2, 0),
		false,
		int64(0),
	)

	require.NoError(keeper.SetValidatorSigningInfo(ctx, consAddr1, signingInfo))
	signingInfo.Address = string(consAddr2)
	require.NoError(keeper.SetValidatorSigningInfo(ctx, consAddr2, signingInfo))

	var signingInfos []slashingtypes.ValidatorSigningInfo

	require.NoError(keeper.IterateValidatorSigningInfos(ctx, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
		signingInfos = append(signingInfos, info)
		return false
	}))

	// verify all values are returned without pagination
	infoResp, err := queryClient.SigningInfos(gocontext.Background(),
		&slashingtypes.QuerySigningInfosRequest{Pagination: nil})
	require.NoError(err)
	require.Equal(signingInfos, infoResp.Info)

	infoResp, err = queryClient.SigningInfos(gocontext.Background(),
		&slashingtypes.QuerySigningInfosRequest{Pagination: &query.PageRequest{Limit: 1, CountTotal: true}})
	require.NoError(err)
	require.Len(infoResp.Info, 1)
	require.Equal(signingInfos[0], infoResp.Info[0])
	require.NotNil(infoResp.Pagination.NextKey)
	require.Equal(uint64(2), infoResp.Pagination.Total)
}
