package client

import (
	"context"

	"google.golang.org/grpc"
	gamev1 "hyapp.local/api/proto/game/v1"
)

// GameClient 抽象 gateway 对 game-service App 与回调 RPC 的依赖。
type GameClient interface {
	ListGames(ctx context.Context, req *gamev1.ListGamesRequest) (*gamev1.ListGamesResponse, error)
	ListRecentGames(ctx context.Context, req *gamev1.ListRecentGamesRequest) (*gamev1.ListGamesResponse, error)
	ListExploreWinners(ctx context.Context, req *gamev1.ListExploreWinnersRequest) (*gamev1.ListExploreWinnersResponse, error)
	GetBridgeScript(ctx context.Context, req *gamev1.GetBridgeScriptRequest) (*gamev1.GetBridgeScriptResponse, error)
	LaunchGame(ctx context.Context, req *gamev1.LaunchGameRequest) (*gamev1.LaunchGameResponse, error)
	GetDiceConfig(ctx context.Context, req *gamev1.GetDiceConfigRequest) (*gamev1.DiceConfigResponse, error)
	MatchDice(ctx context.Context, req *gamev1.MatchDiceRequest) (*gamev1.DiceMatchResponse, error)
	CreateDiceMatch(ctx context.Context, req *gamev1.CreateDiceMatchRequest) (*gamev1.DiceMatchResponse, error)
	JoinDiceMatch(ctx context.Context, req *gamev1.JoinDiceMatchRequest) (*gamev1.DiceMatchResponse, error)
	GetDiceMatch(ctx context.Context, req *gamev1.GetDiceMatchRequest) (*gamev1.DiceMatchResponse, error)
	RollDiceMatch(ctx context.Context, req *gamev1.RollDiceMatchRequest) (*gamev1.DiceMatchResponse, error)
	CancelDiceMatch(ctx context.Context, req *gamev1.CancelDiceMatchRequest) (*gamev1.DiceMatchResponse, error)
	GetRoomRPSConfig(ctx context.Context, req *gamev1.GetRoomRPSConfigRequest) (*gamev1.RoomRPSConfigResponse, error)
	ListRoomRPSChallenges(ctx context.Context, req *gamev1.ListRoomRPSChallengesRequest) (*gamev1.ListRoomRPSChallengesResponse, error)
	CreateRoomRPSChallenge(ctx context.Context, req *gamev1.CreateRoomRPSChallengeRequest) (*gamev1.RoomRPSChallengeResponse, error)
	AcceptRoomRPSChallenge(ctx context.Context, req *gamev1.AcceptRoomRPSChallengeRequest) (*gamev1.RoomRPSChallengeResponse, error)
	GetRoomRPSChallenge(ctx context.Context, req *gamev1.GetRoomRPSChallengeRequest) (*gamev1.RoomRPSChallengeResponse, error)
	HandleCallback(ctx context.Context, req *gamev1.CallbackRequest) (*gamev1.CallbackResponse, error)
}

type grpcGameClient struct {
	app      gamev1.GameAppServiceClient
	callback gamev1.GameCallbackServiceClient
}

func NewGRPCGameClient(conn *grpc.ClientConn) GameClient {
	return &grpcGameClient{
		app:      gamev1.NewGameAppServiceClient(conn),
		callback: gamev1.NewGameCallbackServiceClient(conn),
	}
}

func (c *grpcGameClient) ListGames(ctx context.Context, req *gamev1.ListGamesRequest) (*gamev1.ListGamesResponse, error) {
	return c.app.ListGames(ctx, req)
}

func (c *grpcGameClient) ListRecentGames(ctx context.Context, req *gamev1.ListRecentGamesRequest) (*gamev1.ListGamesResponse, error) {
	return c.app.ListRecentGames(ctx, req)
}

func (c *grpcGameClient) ListExploreWinners(ctx context.Context, req *gamev1.ListExploreWinnersRequest) (*gamev1.ListExploreWinnersResponse, error) {
	// Explore Square 是 app 首页播报数据，gateway 不做本地缓存，直接读取 game-service 的获胜事实。
	return c.app.ListExploreWinners(ctx, req)
}

func (c *grpcGameClient) GetBridgeScript(ctx context.Context, req *gamev1.GetBridgeScriptRequest) (*gamev1.GetBridgeScriptResponse, error) {
	return c.app.GetBridgeScript(ctx, req)
}

func (c *grpcGameClient) LaunchGame(ctx context.Context, req *gamev1.LaunchGameRequest) (*gamev1.LaunchGameResponse, error) {
	return c.app.LaunchGame(ctx, req)
}

func (c *grpcGameClient) GetDiceConfig(ctx context.Context, req *gamev1.GetDiceConfigRequest) (*gamev1.DiceConfigResponse, error) {
	return c.app.GetDiceConfig(ctx, req)
}

func (c *grpcGameClient) MatchDice(ctx context.Context, req *gamev1.MatchDiceRequest) (*gamev1.DiceMatchResponse, error) {
	return c.app.MatchDice(ctx, req)
}

func (c *grpcGameClient) CreateDiceMatch(ctx context.Context, req *gamev1.CreateDiceMatchRequest) (*gamev1.DiceMatchResponse, error) {
	return c.app.CreateDiceMatch(ctx, req)
}

func (c *grpcGameClient) JoinDiceMatch(ctx context.Context, req *gamev1.JoinDiceMatchRequest) (*gamev1.DiceMatchResponse, error) {
	return c.app.JoinDiceMatch(ctx, req)
}

func (c *grpcGameClient) GetDiceMatch(ctx context.Context, req *gamev1.GetDiceMatchRequest) (*gamev1.DiceMatchResponse, error) {
	return c.app.GetDiceMatch(ctx, req)
}

func (c *grpcGameClient) RollDiceMatch(ctx context.Context, req *gamev1.RollDiceMatchRequest) (*gamev1.DiceMatchResponse, error) {
	return c.app.RollDiceMatch(ctx, req)
}

func (c *grpcGameClient) CancelDiceMatch(ctx context.Context, req *gamev1.CancelDiceMatchRequest) (*gamev1.DiceMatchResponse, error) {
	return c.app.CancelDiceMatch(ctx, req)
}

func (c *grpcGameClient) GetRoomRPSConfig(ctx context.Context, req *gamev1.GetRoomRPSConfigRequest) (*gamev1.RoomRPSConfigResponse, error) {
	// gateway 只调用 App RPC，不碰 Admin RPC；App 配置读取必须和客户端权限、app_code 元信息走同一条 game-service 通道。
	return c.app.GetRoomRPSConfig(ctx, req)
}

func (c *grpcGameClient) ListRoomRPSChallenges(ctx context.Context, req *gamev1.ListRoomRPSChallengesRequest) (*gamev1.ListRoomRPSChallengesResponse, error) {
	// 列表是 App 补拉房间实时状态的入口，gateway 不做本地聚合，避免和 game-service 的过期/结算补偿状态不一致。
	return c.app.ListRoomRPSChallenges(ctx, req)
}

func (c *grpcGameClient) CreateRoomRPSChallenge(ctx context.Context, req *gamev1.CreateRoomRPSChallengeRequest) (*gamev1.RoomRPSChallengeResponse, error) {
	// 发起挑战必须进入 game-service 事务链路，那里才能同时处理礼物托管、订单幂等和房间 IM outbox。
	return c.app.CreateRoomRPSChallenge(ctx, req)
}

func (c *grpcGameClient) AcceptRoomRPSChallenge(ctx context.Context, req *gamev1.AcceptRoomRPSChallengeRequest) (*gamev1.RoomRPSChallengeResponse, error) {
	// 应战是并发敏感操作，gateway 只透传当前登录用户和手势，锁单和钱包托管由 game-service 完成。
	return c.app.AcceptRoomRPSChallenge(ctx, req)
}

func (c *grpcGameClient) GetRoomRPSChallenge(ctx context.Context, req *gamev1.GetRoomRPSChallengeRequest) (*gamev1.RoomRPSChallengeResponse, error) {
	// 详情用于 IM 后补拉和断线恢复，必须读取 game-service 的订单事实，不能复用 HTTP 层缓存。
	return c.app.GetRoomRPSChallenge(ctx, req)
}

func (c *grpcGameClient) HandleCallback(ctx context.Context, req *gamev1.CallbackRequest) (*gamev1.CallbackResponse, error) {
	return c.callback.HandleCallback(ctx, req)
}
