package value_objects

import (
	"errors"
	"regexp"
)

var (
	ErrInvalidPartyID = errors.New("invalid party ID")
	partyIDRegex      = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
)

// PartyID represents a unique party identifier
type PartyID struct {
	value string
}

// NewPartyID creates a new PartyID
func NewPartyID(value string) (PartyID, error) {
	if value == "" {
		return PartyID{}, ErrInvalidPartyID
	}
	if !partyIDRegex.MatchString(value) {
		return PartyID{}, ErrInvalidPartyID
	}
	if len(value) > 255 {
		return PartyID{}, ErrInvalidPartyID
	}
	return PartyID{value: value}, nil
}

// MustNewPartyID creates a new PartyID, panics on error
func MustNewPartyID(value string) PartyID {
	id, err := NewPartyID(value)
	if err != nil {
		panic(err)
	}
	return id
}

// String returns the string representation
func (id PartyID) String() string {
	return id.value
}

// IsZero checks if the PartyID is zero
func (id PartyID) IsZero() bool {
	return id.value == ""
}

// Equals checks if two PartyIDs are equal
func (id PartyID) Equals(other PartyID) bool {
	return id.value == other.value
}
