package domain

import (
	"math"
	"net/url"
)

type PlayersSyncedEventPayload struct {
	serverKey   string
	serverURL   *url.URL
	versionCode string
	numPlayers  int
}

const playersSyncedEventPayloadModelName = "PlayersSyncedEventPayload"

func NewPlayersSyncedEventPayload(
	serverKey string,
	serverURL *url.URL,
	versionCode string,
	numPlayers int,
) (PlayersSyncedEventPayload, error) {
	if serverKey == "" {
		return PlayersSyncedEventPayload{}, ValidationError{
			Model: playersSyncedEventPayloadModelName,
			Field: "serverKey",
			Err:   ErrRequired,
		}
	}

	if serverURL == nil {
		return PlayersSyncedEventPayload{}, ValidationError{
			Model: playersSyncedEventPayloadModelName,
			Field: "serverURL",
			Err:   ErrNil,
		}
	}

	if versionCode == "" {
		return PlayersSyncedEventPayload{}, ValidationError{
			Model: playersSyncedEventPayloadModelName,
			Field: "versionCode",
			Err:   ErrRequired,
		}
	}

	if err := validateIntInRange(numPlayers, 0, math.MaxInt); err != nil {
		return PlayersSyncedEventPayload{}, ValidationError{
			Model: playersSyncedEventPayloadModelName,
			Field: "numPlayers",
			Err:   err,
		}
	}

	return PlayersSyncedEventPayload{
		serverKey:   serverKey,
		serverURL:   serverURL,
		versionCode: versionCode,
		numPlayers:  numPlayers,
	}, nil
}

func (p PlayersSyncedEventPayload) ServerKey() string {
	return p.serverKey
}

func (p PlayersSyncedEventPayload) ServerURL() *url.URL {
	return p.serverURL
}

func (p PlayersSyncedEventPayload) VersionCode() string {
	return p.versionCode
}

func (p PlayersSyncedEventPayload) NumPlayers() int {
	return p.numPlayers
}
