package domain

import (
	"math"
	"net/url"
)

type TribesSyncedEventPayload struct {
	serverKey   string
	serverURL   *url.URL
	versionCode string
	numTribes   int
}

const tribesSyncedEventPayloadModelName = "TribesSyncedEventPayload"

func NewTribesSyncedEventPayload(
	serverKey string,
	serverURL *url.URL,
	versionCode string,
	numTribes int,
) (TribesSyncedEventPayload, error) {
	if serverKey == "" {
		return TribesSyncedEventPayload{}, ValidationError{
			Model: tribesSyncedEventPayloadModelName,
			Field: "serverKey",
			Err:   ErrRequired,
		}
	}

	if serverURL == nil {
		return TribesSyncedEventPayload{}, ValidationError{
			Model: tribesSyncedEventPayloadModelName,
			Field: "serverURL",
			Err:   ErrNil,
		}
	}

	if versionCode == "" {
		return TribesSyncedEventPayload{}, ValidationError{
			Model: tribesSyncedEventPayloadModelName,
			Field: "versionCode",
			Err:   ErrRequired,
		}
	}

	if err := validateIntInRange(numTribes, 0, math.MaxInt); err != nil {
		return TribesSyncedEventPayload{}, ValidationError{
			Model: tribesSyncedEventPayloadModelName,
			Field: "numTribes",
			Err:   err,
		}
	}

	return TribesSyncedEventPayload{
		serverKey:   serverKey,
		serverURL:   serverURL,
		versionCode: versionCode,
		numTribes:   numTribes,
	}, nil
}

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

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

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

func (p TribesSyncedEventPayload) NumTribes() int {
	return p.numTribes
}
