package entity

import (
	"errors"
	"time"

	"backend/pkg/member/domain/member"

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

// Account represents a user account with authentication credentials.
// It stores login information, hashed passwords, and platform-specific data.
type Account struct {
	ID       bson.ObjectID   `bson:"_id,omitempty" json:"id,omitempty"`
	LoginID  string          `bson:"login_id"` // Unique login identifier (email, phone, username)
	Token    string          `bson:"token"`    // Hashed password or platform-specific token
	Platform member.Platform `bson:"platform"` // Platform type: 1. platform 2. google 3. line 4. apple
	UpdateAt *int64          `bson:"update_at,omitempty" json:"update_at,omitempty"`
	CreateAt *int64          `bson:"create_at,omitempty" json:"create_at,omitempty"`
}

// CollectionName returns the MongoDB collection name for Account entities.
func (a *Account) CollectionName() string {
	return "account"
}

// Validate validates the Account entity
func (a *Account) Validate() error {
	if a.LoginID == "" {
		return errors.New("login_id is required")
	}
	if a.Token == "" {
		return errors.New("token is required")
	}
	if !a.Platform.IsValid() {
		return errors.New("invalid platform")
	}
	return nil
}

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

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

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