package entity

import "time"

// Role type alias
type RoleString string

const (
	// RoleSuperAdmin is the platform administrator
	RoleSuperAdmin = "superadmin"
	// RoleAdmin is the company administrator (formerly admin)
	RoleAdmin = "admin"
	// RoleRecruiter is a recruiter within a company
	RoleRecruiter = "recruiter"
	// RoleCandidate is a job seeker (formerly candidate)
	RoleCandidate = "candidate"

	// User Status
	UserStatusActive              = "active"
	UserStatusInactive            = "inactive"
	UserStatusForceChangePassword = "force_change_password"
)

// 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"`
	Phone        *string                `json:"phone,omitempty"`
	Bio          *string                `json:"bio,omitempty"`
	Address      *string                `json:"address,omitempty"`
	City         *string                `json:"city,omitempty"`
	State        *string                `json:"state,omitempty"`
	ZipCode      *string                `json:"zip_code,omitempty"`
	BirthDate    *time.Time             `json:"birth_date,omitempty"`
	Education    *string                `json:"education,omitempty"`
	Experience   *string                `json:"experience,omitempty"`
	Skills       []string               `json:"skills,omitempty"`
	Objective    *string                `json:"objective,omitempty"`
	Title        *string                `json:"title,omitempty"`
	PasswordHash string                 `json:"-"`
	AvatarUrl    string                 `json:"avatar_url"`
	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:    UserStatusActive,
		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
}
