package entity

import (
	"errors"
	"time"

	"backend/pkg/member/domain/member"

	"go.mongodb.org/mongo-driver/v2/bson"
)

type AccountUID struct {
	ID       bson.ObjectID      `bson:"_id,omitempty" json:"id,omitempty"`
	LoginID  string             `bson:"login_id"`
	UID      string             `bson:"uid"`
	Type     member.AccountType `bson:"type"`
	UpdateAt *int64             `bson:"update_at,omitempty" json:"update_at,omitempty"`
	CreateAt *int64             `bson:"create_at,omitempty" json:"create_at,omitempty"`
}

func (a *AccountUID) CollectionName() string {
	return "account_uid_binding"
}

// Validate validates the AccountUID entity
func (a *AccountUID) Validate() error {
	if a.LoginID == "" {
		return errors.New("login_id is required")
	}
	if a.UID == "" {
		return errors.New("uid is required")
	}
	if !a.Type.IsValid() {
		return errors.New("invalid account type")
	}
	return nil
}

// SetTimestamps sets the create and update timestamps
func (a *AccountUID) SetTimestamps() {
	now := time.Now().UTC().UnixNano()
	if a.CreateAt == nil {
		a.CreateAt = &now
	}
	a.UpdateAt = &now
}

// IsNew returns true if this is a new binding (no ID set)
func (a *AccountUID) IsNew() bool {
	return a.ID.IsZero()
}

// GetIDString returns the ID as a hex string
func (a *AccountUID) GetIDString() string {
	return a.ID.Hex()
}
