package entity

import "time"

// User represents a user within a specific Tenant (Company).
type User struct {
	ID           string                 `json:"id"`
	TenantID     string                 `json:"tenant_id"` // Link to Company
	Name         string                 `json:"name"`
	Email        string                 `json:"email"`
	PasswordHash string                 `json:"-"`
	Roles        []Role                 `json:"roles"`
	Status       string                 `json:"status"` // "ACTIVE", "INACTIVE"
	Metadata     map[string]interface{} `json:"metadata"`
	CreatedAt    time.Time              `json:"created_at"`
	UpdatedAt    time.Time              `json:"updated_at"`
}

// NewUser creates a new User instance.
func NewUser(id, tenantID, name, email string) *User {
	return &User{
		ID:        id,
		TenantID:  tenantID,
		Name:      name,
		Email:     email,
		Status:    "ACTIVE",
		Roles:     []Role{},
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}
}

func (u *User) AssignRole(role Role) {
	u.Roles = append(u.Roles, role)
	u.UpdatedAt = time.Now()
}

func (u *User) HasPermission(permissionCode string) bool {
	for _, role := range u.Roles {
		for _, perm := range role.Permissions {
			if perm.Code == permissionCode {
				return true
			}
		}
	}
	return false
}
